Skip to content

Commit

Permalink
improved url_for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 28, 2014
1 parent 108cd9d commit 686d41d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

5.47 2014-09-27
5.47 2014-09-28
- Improved url_for performance.

5.46 2014-09-26
- PAUSE lost the previous release.
Expand Down
25 changes: 12 additions & 13 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -72,9 +72,8 @@ sub has_custom_name { !!shift->{custom} }

sub has_websocket {
my $self = shift;
return 1 if $self->is_websocket;
return undef unless my $parent = $self->parent;
return $parent->has_websocket;
return $self->{has_websocket} if exists $self->{has_websocket};
return $self->{has_websocket} = grep { $_->is_websocket } @{$self->_chain};
}

sub is_endpoint { $_[0]->inline ? undef : !@{$_[0]->children} }
Expand Down Expand Up @@ -137,11 +136,7 @@ sub render {
return $parent->render($path, $values);
}

sub root {
my $root = my $parent = shift;
$root = $parent while $parent = $parent->parent;
return $root;
}
sub root { shift->_chain->[0] }

sub route {
my $self = shift;
Expand Down Expand Up @@ -178,10 +173,7 @@ sub to {
}

sub to_string {
my $self = shift;
my $pattern = $self->parent ? $self->parent->to_string : '';
$pattern .= $self->pattern->pattern if $self->pattern->pattern;
return $pattern;
join '', map { $_->pattern->pattern // '' } @{shift->_chain};
}

sub under { shift->_generate_route(under => @_) }
Expand All @@ -200,6 +192,12 @@ sub websocket {
return $route;
}

sub _chain {
my @chain = (my $parent = shift);
unshift @chain, $parent while $parent = $parent->parent;
return \@chain;
}

sub _generate_route {
my ($self, $methods, @args) = @_;

Expand Down Expand Up @@ -388,7 +386,8 @@ Check if this route has a custom name.
my $bool = $r->has_websocket;
Check if this route has a WebSocket ancestor.
Check if this route has a WebSocket ancestor and cache the result for future
checks.
=head2 is_endpoint
Expand Down

0 comments on commit 686d41d

Please sign in to comment.