Skip to content

Commit

Permalink
optimize static path rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 19, 2014
1 parent 8874fc0 commit 8e12216
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

5.31 2014-08-19
- Improved Mojolicious::Static to allow custom content types.
- Improved url_for performance.

5.30 2014-08-17
- Improved Mojolicious::Static to only handle GET and HEAD requests.
Expand Down
24 changes: 13 additions & 11 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -62,21 +62,20 @@ sub render {
# Placeholders can only be optional without a format
my $optional = !(my $format = $values->{format});

my $str = '';
my $defaults = $self->defaults;
my $str = '';
for my $token (reverse @{$self->tree}) {
my ($op, $value) = @$token[0, 1];
my $fragment = '';

# Slash
if ($op eq 'slash') { $fragment = '/' unless $optional }

# Text
elsif ($op eq 'text') { ($fragment, $optional) = ($value, 0) }
if ($op eq 'text') { ($fragment, $optional) = ($value, 0) }

# Slash
elsif ($op eq 'slash') { $fragment = '/' unless $optional }

# Placeholder
else {
my $default = $defaults->{$value};
my $default = $self->defaults->{$value};
$fragment = $values->{$value} // $default // '';
if (!defined $default || ($default ne $fragment)) { $optional = 0 }
elsif ($optional) { $fragment = '' }
Expand Down Expand Up @@ -198,18 +197,21 @@ sub _tokenize {
# Quote end
elsif ($char eq $quote_end) { ($inside, $quoted) = (0, 0) }

# Slash
# Slash (first slash is text for optimizations)
elsif ($char eq '/') {
push @tree, ['slash'];
push @tree, @tree ? ['slash'] : ['text', '/'];
$inside = 0;
}

# Placeholder, relaxed or wildcard
elsif ($inside) { $tree[-1][-1] .= $char }

# Text
# Text (optimize text followed by slash followed by text)
elsif ($tree[-1][0] eq 'text') { $tree[-1][-1] .= $char }
else { push @tree, ['text', $char] }
elsif ($tree[-2] && $tree[-2][0] eq 'text' && $tree[-1][0] eq 'slash') {
pop @tree && ($tree[-1][-1] .= "/$char");
}
else { push @tree, ['text', $char] }
}

return $self->pattern($pattern)->tree(\@tree);
Expand Down

0 comments on commit 8e12216

Please sign in to comment.