Skip to content

Instantly share code, notes, and snippets.

@mala
Created May 3, 2010 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mala/388419 to your computer and use it in GitHub Desktop.
Save mala/388419 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use Benchmark qw(:all);
use Data::Dumper;
use HTTP::Response;
use HTTP::Headers::Fast;
{
package HTTP::Headers::Fast;
my %CACHE;
sub new_from_string {
my ( $class, $str ) = @_;
return bless {}, $class unless defined $str;
my ( %self, $field, $value, $f );
for ( split /\r?\n/, $str ) {
if ( defined $field ) {
if ( ord == 9 || ord == 32 ) {
$value .= "\n$_";
next;
}
$f = $CACHE{$field} ||= _standardize_field_name($field);
if ( defined $self{$f} ) {
_header_push_no_return( \%self, $f, $value );
}
else { $self{$f} = $value }
}
( $field, $value ) = split /[ \t]*: ?/, $_, 2;
}
if ( defined $field ) {
$f = $CACHE{$field} ||= _standardize_field_name($field);
if ( defined $self{$f} ) {
_header_push_no_return( \%self, $f, $value );
}
else { $self{$f} = $value }
}
bless \%self, $class;
}
}
{
package HTTP::Response;
sub parse2 {
my $class = shift;
my ( $sl, $str ) = split /\r?\n/, ${ $_[0] }, 2;
my %res;
if ( $sl =~ /^\d{3} / ) {
@res{ '_rc', '_msg' } = split( ' ', $sl, 2 );
}
else {
@res{ '_protocol', '_rc', '_msg' } = split( ' ', $sl, 3 );
}
$res{_headers} = HTTP::Headers::Fast->new_from_string($str);
$res{_content} = ${ $_[1] };
bless \%res, 'HTTP::Response';
}
}
my $header =<<'END';
HTTP/1.1 200 OK
Date: Mon, 03 May 2010 17:34:05 GMT
Server: hi
Status: 200 OK
X-Transaction: 1272908045-29861-29017
X-RateLimit-Limit: 150
Etag: "93ac529570386caa165a0de5e10c3bdc"-gzip
Last-Modified: Mon, 03 May 2010 17:34:05 GMT
X-RateLimit-Remaining: 148
X-Runtime: 0.06693
Content-Type: application/rss+xml; charset=utf-8
Pragma: no-cache
X-RateLimit-Class: api
X-Revision: DEV
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
X-RateLimit-Reset: 1272910971
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 1796
Connection: close
END
# Set-Cookie: lang=en; path=/
# Set-Cookie: _twitter_sess=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; domain=.twitter.com; path=/
my $body = '01234567890123456789' x 200;
my $res = HTTP::Response->parse($header . $body);
my $res2 = HTTP::Response->parse2(\$header, \$body);
warn Dumper $res2;
my $header_body = $header. $body;
cmpthese 1000, {
parse => sub { HTTP::Response->parse($header_body) },
parse2 => sub { HTTP::Response->parse2(\$header, \$body) },
};
__END__
Rate parse parse2
parse 1176/s -- -65%
parse2 3333/s 183% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment