Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 9, 2012
1 parent 76cb4b9 commit b96e94f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 91 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Cache.pm
Expand Up @@ -15,9 +15,9 @@ sub set {
my ($self, $key, $value) = @_;

# Cache with size limit
my $keys = $self->max_keys;
my $cache = $self->{cache} ||= {};
my $stack = $self->{stack} ||= [];
my $keys = $self->max_keys;
delete $cache->{shift @$stack} while @$stack >= $keys;
push @$stack, $key;
$cache->{$key} = $value;
Expand Down
47 changes: 20 additions & 27 deletions lib/Mojo/Cookie.pm
Expand Up @@ -24,37 +24,30 @@ sub _tokenize {
while ($string) {

# Name
if ($string =~ s/^\s*([^\=\;\,]+)\s*\=?\s*//) {
my $name = $1;

# "expires" is a special case, thank you Netscape...
$string =~ s/^([^\;\,]+\,?[^\;\,]+)/"$1"/ if $name =~ /^expires$/i;

# Value
my $value;
$value = unquote $1
if $string =~ s/^("(?:\\\\|\\"|[^"])+"|[^\;\,]+)\s*//;

# Token
push @token, [$name, $value];

# Separator
$string =~ s/^\s*\;\s*//;
if ($string =~ s/^\s*\,\s*//) {
push @tree, [@token];
@token = ();
}
}
last unless $string =~ s/^\s*([^\=\;\,]+)\s*\=?\s*//;
my $name = $1;

# Bad format
else {last}
# "expires" is a special case, thank you Netscape...
$string =~ s/^([^\;\,]+\,?[^\;\,]+)/"$1"/ if $name =~ /^expires$/i;

}
# Value
my $value;
$value = unquote $1
if $string =~ s/^("(?:\\\\|\\"|[^"])+"|[^\;\,]+)\s*//;

# Token
push @token, [$name, $value];

# No separator
push @tree, [@token] if @token;
# Separator
$string =~ s/^\s*\;\s*//;
if ($string =~ s/^\s*\,\s*//) {
push @tree, [@token];
@token = ();
}
}

return @tree;
# Take care of final token
return @token ? (@tree, \@token) : @tree;
}

1;
Expand Down
26 changes: 9 additions & 17 deletions lib/Mojo/Cookie/Request.pm
Expand Up @@ -11,30 +11,22 @@ sub parse {

# Walk tree
my @cookies;
for my $knot ($self->_tokenize($string)) {
for my $token (@{$knot}) {
my ($name, $value) = @{$token};

# Garbage (RFC 2965)
next if $name =~ /^\$/;

# Name and value
push @cookies, Mojo::Cookie::Request->new;
$cookies[-1]->name($name);
$cookies[-1]->value($value //= '');
}
for my $token (map {@$_} $self->_tokenize($string)) {
my ($name, $value) = @$token;
next if $name =~ /^\$/;
push @cookies,
Mojo::Cookie::Request->new(name => $name, value => $value // '');
}

return \@cookies;
}

sub to_string {
my $self = shift;
return '' unless my $cookie = $self->name;
$cookie .= '=';
my $value = $self->value;
$cookie .= $value =~ /[,;"]/ ? quote($value) : $value if defined $value;
return $cookie;
return '' unless my $name = $self->name;
my $value = $self->value // '';
$value = $value =~ /[,;"]/ ? quote($value) : $value;
return "$name=$value";
}

1;
Expand Down
19 changes: 9 additions & 10 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -32,15 +32,14 @@ sub parse {

# Walk tree
my @cookies;
for my $knot ($self->_tokenize($string)) {
for my $i (0 .. $#{$knot}) {
my ($name, $value) = @{$knot->[$i]};
for my $token ($self->_tokenize($string)) {
for my $i (0 .. $#$token) {
my ($name, $value) = @{$token->[$i]};

# This will only run once
if (!$i) {
push @cookies, Mojo::Cookie::Response->new;
$cookies[-1]->name($name);
$cookies[-1]->value($value //= '');
push @cookies,
Mojo::Cookie::Response->new(name => $name, value => $value // '');
}

# Attributes
Expand All @@ -59,10 +58,10 @@ sub to_string {
my $self = shift;

# Name and value
return '' unless my $cookie = $self->name;
$cookie .= '=';
my $value = $self->value;
$cookie .= $value =~ /[,;"]/ ? quote($value) : $value if defined $value;
return '' unless my $name = $self->name;
my $value = $self->value // '';
$value = $value =~ /[,;"]/ ? quote($value) : $value;
my $cookie = "$name=$value";

# Domain
if (my $domain = $self->domain) { $cookie .= "; Domain=$domain" }
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -177,9 +177,6 @@ sub stream {
my $self = shift;
$self = $self->singleton unless ref $self;

# Make sure garbage gets collected
$self->_cleaner;

# Connect stream with reactor
my $stream = shift;
return $self->_stream($stream, $self->_id) if blessed $stream;
Expand Down Expand Up @@ -275,6 +272,9 @@ sub _remove {
sub _stream {
my ($self, $stream, $id) = @_;

# Make sure garbage gets collected
$self->_cleaner;

# Connect stream with reactor
$self->{connections}->{$id}->{stream} = $stream;
weaken $stream->reactor($self->reactor)->{reactor};
Expand Down
20 changes: 7 additions & 13 deletions lib/Mojo/Loader.pm
Expand Up @@ -36,29 +36,23 @@ sub search {
my ($self, $namespace) = @_;

# Scan
my $modules = [];
my %found;
my (@modules, %found);
for my $directory (exists $INC{'blib.pm'} ? grep {/blib/} @INC : @INC) {
my $path = catdir $directory, (split /::/, $namespace);
next unless (-e $path && -d $path);

# Get files
opendir(my $dir, $path);
my @files = grep /\.pm$/, readdir($dir);
closedir($dir);
next unless -d (my $path = catdir $directory, (split /::/, $namespace));

# Check files
for my $file (@files) {
opendir(my $dir, $path);
for my $file (grep /\.pm$/, readdir($dir)) {
next if -d catfile splitdir($path), $file;

# Module found
my $class = "$namespace\::" . fileparse $file, qr/\.pm/;
push @$modules, $class unless $found{$class};
$found{$class} ||= 1;
push @modules, $class unless $found{$class}++;
}
closedir $dir;
}

return $modules;
return \@modules;
}

1;
Expand Down
32 changes: 13 additions & 19 deletions lib/Mojo/Util.pm
Expand Up @@ -307,17 +307,10 @@ sub camelize {
my $string = shift;
return $string if $string =~ /^[A-Z]/;

# Module parts
my @parts;
for my $part (split /-/, $string) {
next unless $part;

# Camel case words
my @words = split /_/, $part;
@words = map { ucfirst lc } @words;
push @parts, join '', @words;
}
return join '::', @parts;
# Camel case words
return join '::', map {
join '', map { ucfirst lc } split /_/, $_
} split /-/, $string;
}

sub decamelize {
Expand All @@ -328,12 +321,12 @@ sub decamelize {
my @parts;
for my $part (split /\:\:/, $string) {

# Camel case words
# Snake case words
my @words;
push @words, $1 while ($part =~ s/([A-Z]{1}[^A-Z]*)//);
@words = map {lc} @words;
push @words, lc $1 while $part =~ s/([A-Z]{1}[^A-Z]*)//;
push @parts, join '_', @words;
}

return join '-', @parts;
}

Expand Down Expand Up @@ -388,17 +381,18 @@ sub hmac_md5_sum { _hmac(0, @_) }
sub hmac_sha1_sum { _hmac(1, @_) }

sub html_escape {
my $string = shift;
my $string = shift;

my $escaped = '';
for (1 .. length $string) {
for my $i (0 .. (length($string) - 1)) {

# Escape entities
my $char = substr $string, 0, 1, '';
my $char = substr $string, $i, 1;
my $num = unpack 'U', $char;
my $named = $REVERSE_ENTITIES{$num};
$char = "&$named;" if $named;
if (my $named = $REVERSE_ENTITIES{$num}) { $char = "&$named;" }
$escaped .= $char;
}

return $escaped;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Types.pm
Expand Up @@ -51,7 +51,7 @@ sub detect {

sub type {
my ($self, $ext, $type) = @_;
return $self->types->{$ext || ''} unless $type;
return $self->types->{$ext} unless $type;
$self->types->{$ext} = $type;
return $self;
}
Expand Down

0 comments on commit b96e94f

Please sign in to comment.