Skip to content

Commit

Permalink
improve url_for performance significantly (closes #914)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 23, 2016
1 parent 6375642 commit eac8210
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
6.48 2016-02-23
- Added files function to Mojo::Util.
- Updated jQuery to version 2.2.1.
- Improved url_for performance significantly.
- Fixed bug where the results of the list_files method in Mojo::Home would
include directories.

Expand Down
8 changes: 1 addition & 7 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -53,13 +53,7 @@ sub is_hidden {
return !!($h->{$method} || index($method, '_') == 0 || $method !~ /[a-z]/);
}

sub lookup {
my ($self, $name) = @_;
my $reverse = $self->{reverse} ||= {};
return $reverse->{$name} if exists $reverse->{$name};
return undef unless my $route = $self->find($name);
return $reverse->{$name} = $route;
}
sub lookup { ($_[0]{reverse} //= $_[0]->_index)->{$_[1]} }

sub match {
my ($self, $c) = @_;
Expand Down
30 changes: 15 additions & 15 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -36,21 +36,7 @@ sub delete { shift->_generate_route(DELETE => @_) }

sub detour { shift->partial(1)->to(@_) }

sub find {
my ($self, $name) = @_;

my @children = (@{$self->children});
my $candidate;
while (my $child = shift @children) {

# Custom names have priority
$candidate = $child->has_custom_name ? return $child : $child
if $child->name eq $name;
push @children, @{$child->children};
}

return $candidate;
}
sub find { shift->_index->{shift()} }

sub get { shift->_generate_route(GET => @_) }

Expand Down Expand Up @@ -221,6 +207,20 @@ sub _generate_route {
return defined $name ? $route->name($name) : $route;
}

sub _index {
my $self = shift;

my %index;
my @children = (@{$self->children});
while (my $child = shift @children) {
if ($child->has_custom_name) { $index{$child->name} = $child }
else { $index{$child->name} ||= $child }
push @children, @{$child->children};
}

return \%index;
}

1;

=encoding utf8
Expand Down
5 changes: 0 additions & 5 deletions t/mojolicious/routes.t
Expand Up @@ -228,11 +228,6 @@ is $r->lookup('fast'), $fast, 'fast route found';
my $faster = $r->route('/faster')->name('fast');
is $r->find('fast'), $faster, 'faster route found';
is $r->lookup('fast'), $fast, 'fast route found';
is $r->find('fastest'), undef, 'fastest route not found';
is $r->lookup('fastest'), undef, 'fastest route not found';
my $fastest = $r->route('/fastest');
is $r->find('fastest'), $fastest, 'fastest route found';
is $r->lookup('fastest'), $fastest, 'fastest route found';

# Make sure stash stays clean
my $c = Mojolicious::Controller->new;
Expand Down

1 comment on commit eac8210

@rewolfe
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on this change does not match the change. Also, the way lookup worked changed.

Please sign in to comment.