Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimized most used Mojo::Header methods
  > perl -Mojo -e'my $h=Mojo::Headers->new; n { $h->content_type("foo") } 2000000'
  3.54158 wallclock secs ( 3.54 usr +  0.00 sys =  3.54 CPU) @ 564971.75/s (n=2000000)

  < perl -Mojo -e'my $h=Mojo::Headers->new; n { $h->content_type("foo") } 2000000'
  3.63856 wallclock secs ( 3.64 usr +  0.00 sys =  3.64 CPU) @ 549450.55/s (n=2000000)
  • Loading branch information
Jan Henning Thorsen committed Feb 10, 2016
1 parent 352f98d commit a1d482f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/Mojo/Headers.pm
Expand Up @@ -19,10 +19,15 @@ my %NORMALCASE = map { lc() => $_ } (
qw(Set-Cookie Status Strict-Transport-Security TE Trailer Transfer-Encoding),
qw(Upgrade User-Agent Vary WWW-Authenticate)
);
for my $header (values %NORMALCASE) {
my $name = lc $header;
$name =~ y/-/_/;
monkey_patch __PACKAGE__, $name, sub { shift->header($header => @_) };
for my $header (keys %NORMALCASE) {
my $method = $header;
$method =~ y/-/_/;
monkey_patch __PACKAGE__, $method, sub {
my $self = shift;
$self->{headers}{$header} = [@_] and return $self if @_;
return undef unless my $headers = $self->{headers}{$header};
return join ', ', @$headers;
};
}

sub add {
Expand Down

0 comments on commit a1d482f

Please sign in to comment.