Skip to content

Commit

Permalink
a few small performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 3, 2016
1 parent 5097e10 commit cd41372
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.53 2016-03-03
- Improved Mojo::IOLoop performance slightly.

6.52 2016-03-02
- Added is_accepting method to Mojo::IOLoop::Server.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Asset/File.pm
Expand Up @@ -26,7 +26,7 @@ has handle => sub {
my $name = $path // $base;
until ($handle->open($name, O_APPEND | O_CREAT | O_EXCL | O_RDWR)) {
croak qq{Can't open file "$name": $!} if defined $path || $! != $!{EEXIST};
$name = "$base." . md5_sum(time . $$ . rand 999);
$name = "$base." . md5_sum(time . $$ . rand);
}
$self->path($name);

Expand Down
12 changes: 6 additions & 6 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -129,9 +129,9 @@ sub server {
sub singleton { state $loop = shift->SUPER::new }

sub start {
my $self = shift;
my $self = _instance(shift);
croak 'Mojo::IOLoop already running' if $self->is_running;
_instance($self)->reactor->start;
$self->reactor->start;
}

sub stop { _instance(shift)->reactor->stop }
Expand All @@ -152,10 +152,10 @@ sub timer { shift->_timer(timer => @_) }

sub _id {
my $self = shift;
my $id;
do { $id = md5_sum 'c' . steady_time . rand 999 }
while $self->{in}{$id} || $self->{out}{$id} || $self->{acceptors}{$id};
return $id;
while (my $id = md5_sum 'c' . steady_time . rand) {
next if $self->{in}{$id} || $self->{out}{$id} || $self->{acceptors}{$id};
return $id;
}
}

sub _in { scalar keys %{shift->{in} || {}} }
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -116,9 +116,9 @@ sub watch {

sub _id {
my $self = shift;
my $id;
do { $id = md5_sum 't' . steady_time . rand 999 } while $self->{timers}{$id};
return $id;
while (my $id = md5_sum 't' . steady_time . rand) {
return $id unless $self->{timers}{$id};
}
}

sub _next {
Expand Down
30 changes: 14 additions & 16 deletions lib/Mojo/Util.pm
Expand Up @@ -64,8 +64,20 @@ our @EXPORT_OK = (
# DEPRECATED in Clinking Beer Mugs!
push @EXPORT_OK, 'xss_escape';

sub b64_decode { decode_base64 $_[0] }
sub b64_encode { encode_base64 $_[0], $_[1] }
# Aliases
monkey_patch(__PACKAGE__, 'b64_decode', \&decode_base64);
monkey_patch(__PACKAGE__, 'b64_encode', \&encode_base64);
monkey_patch(__PACKAGE__, 'hmac_sha1_sum', \&hmac_sha1_hex);
monkey_patch(__PACKAGE__, 'md5_bytes', \&md5);
monkey_patch(__PACKAGE__, 'md5_sum', \&md5_hex);
monkey_patch(__PACKAGE__, 'sha1_bytes', \&sha1);
monkey_patch(__PACKAGE__, 'sha1_sum', \&sha1_hex);

# Use a monotonic clock if possible
monkey_patch(__PACKAGE__, 'steady_time',
MONOTONIC
? sub () { Time::HiRes::clock_gettime(Time::HiRes::CLOCK_MONOTONIC()) }
: \&Time::HiRes::time);

sub camelize {
my $str = shift;
Expand Down Expand Up @@ -122,18 +134,13 @@ sub files {
return sort @files;
}

sub hmac_sha1_sum { hmac_sha1_hex @_ }

sub html_unescape {
my $str = shift;
$str
=~ s/&(?:\#((?:[0-9]{1,7}|x[0-9a-fA-F]{1,6}));|(\w+;))/_decode($1, $2)/ge;
return $str;
}

sub md5_bytes { md5 @_ }
sub md5_sum { md5_hex @_ }

# Declared in Mojo::Base to avoid circular require problems
sub monkey_patch { Mojo::Base::_monkey_patch(@_) }

Expand Down Expand Up @@ -241,9 +248,6 @@ sub secure_compare {
return $r == 0;
}

sub sha1_bytes { sha1 @_ }
sub sha1_sum { sha1_hex @_ }

sub slurp {
my $path = shift;

Expand Down Expand Up @@ -272,12 +276,6 @@ sub squish {
return $str;
}

sub steady_time () {
MONOTONIC
? Time::HiRes::clock_gettime(Time::HiRes::CLOCK_MONOTONIC())
: Time::HiRes::time;
}

sub tablify {
my $rows = shift;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -69,7 +69,7 @@ sub _content {
sub _csrf_token {
my $c = shift;
return $c->session->{csrf_token}
||= hmac_sha1_sum($$ . steady_time . rand 999, $c->app->secrets->[0]);
||= hmac_sha1_sum($$ . steady_time . rand, $c->app->secrets->[0]);
}

sub _current_route {
Expand Down

0 comments on commit cd41372

Please sign in to comment.