Skip to content

Commit

Permalink
improve Mojo::URL performance significantly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 29, 2017
1 parent 7f48299 commit a7cdf6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

7.38 2017-07-26
7.38 2017-07-29
- Added extra attribute to Mojolicious::Static. (jabberwok)
- Improve Mojo::URL performance significantly.

7.37 2017-07-21
- Added slugify method to Mojo::ByteStream. (Grinnz)
Expand Down
20 changes: 15 additions & 5 deletions lib/Mojo/Util.pm
Expand Up @@ -55,8 +55,8 @@ my $EXPIRES_RE = qr/(\w+\W+\d+\W+\w+\W+\d+\W+\d+:\d+:\d+\W*\w+)/;
# HTML entities
my $ENTITY_RE = qr/&(?:\#((?:[0-9]{1,7}|x[0-9a-fA-F]{1,6}));|(\w+[;=]?))/;

# Encoding cache
my %CACHE;
# Encoding and pattern cache
my (%ENCODING, %PATTERN);

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
Expand Down Expand Up @@ -316,8 +316,18 @@ sub unquote {

sub url_escape {
my ($str, $pattern) = @_;
if ($pattern) { $str =~ s/([$pattern])/sprintf '%%%02X', ord $1/ge }
else { $str =~ s/([^A-Za-z0-9\-._~])/sprintf '%%%02X', ord $1/ge }

if ($pattern) {
unless (exists $PATTERN{$pattern}) {
(my $quoted = $pattern) =~ s!([/\$\[])!\\$1!g;

This comment has been minimized.

Copy link
@truist

truist Oct 12, 2017

@kraih just FYI that something about this change broke existing code of mine; I had been using:

return Mojo::Util::url_escape(@_, '^A-Za-z0-9\-._~\/\?\=');

...but after this change, I had to un-escape the last three characters to avoid errors about unmatched [ and undefined objects:

return Mojo::Util::url_escape(@_, '^A-Za-z0-9\-._~/?=');

This comment has been minimized.

Copy link
@truist

truist Oct 12, 2017

For context: truist/gallery@e2c656f

$PATTERN{$pattern}
= eval "sub { \$_[0] =~ s/([$quoted])/sprintf '%%%02X', ord \$1/ge }"
or croak $@;
}
$PATTERN{$pattern}->($str);
}
else { $str =~ s/([^A-Za-z0-9\-._~])/sprintf '%%%02X', ord $1/ge }

return $str;
}

Expand Down Expand Up @@ -361,7 +371,7 @@ sub _adapt {
}

sub _encoding {
$CACHE{$_[0]} //= find_encoding($_[0]) // croak "Unknown encoding '$_[0]'";
$ENCODING{$_[0]} //= find_encoding($_[0]) // croak "Unknown encoding '$_[0]'";
}

sub _entity {
Expand Down

0 comments on commit a7cdf6f

Please sign in to comment.