Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve encoding performance with a cache
  • Loading branch information
kraih committed Nov 25, 2012
1 parent c9f0b6c commit 158366e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/Mojo/Util.pm
Expand Up @@ -37,6 +37,9 @@ $REVERSE{$ENTITIES{$_}} //= $_
for sort { @{[$a =~ /[A-Z]/g]} <=> @{[$b =~ /[A-Z]/g]} }
sort grep {/;/} keys %ENTITIES;

# Encoding cache
my %ENCODING;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_escape),
Expand Down Expand Up @@ -88,11 +91,11 @@ sub decamelize {
sub decode {
my ($encoding, $bytes) = @_;
return undef
unless eval { $bytes = Encode::decode($encoding, $bytes, 1); 1 };
unless eval { $bytes = _encoding($encoding)->decode("$bytes", 1); 1 };
return $bytes;
}
sub encode { Encode::encode(shift, shift) }
sub encode { _encoding($_[0])->encode("$_[1]") }
sub get_line {
Expand Down Expand Up @@ -370,6 +373,8 @@ sub _encode {
return exists $REVERSE{$_[0]} ? "&$REVERSE{$_[0]}" : "&#@{[ord($_[0])]};";
}

sub _encoding { $ENCODING{$_[0]} //= Encode::find_encoding($_[0]) }

sub _hmac {
my ($hash, $string, $secret) = @_;

Expand Down

0 comments on commit 158366e

Please sign in to comment.