From e3fd8551e919b95a78f1f0ab0cfff12033291eeb Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 29 Feb 2016 00:29:03 +0000 Subject: http: avoid needless time2str calls Checking the time is nearly free on modern systems with vDSO/vsyscall/similar while sprintf is always expensive. --- lib/PublicInbox/HTTP.pm | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index a472388d..14971f43 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -17,7 +17,6 @@ use HTTP::Parser::XS qw(parse_http_request); # supports pure Perl fallback use HTTP::Status qw(status_message); use HTTP::Date qw(time2str); use IO::File; -my $null_io = IO::File->new('/dev/null', '<'); use constant { CHUNK_START => -1, # [a-f0-9]+\r\n CHUNK_END => -2, # \r\n @@ -25,6 +24,14 @@ use constant { CHUNK_MAX_HDR => 256, }; +my $null_io = IO::File->new('/dev/null', '<'); +my $http_date; +my $prev = 0; +sub http_date () { + my $now = time; + $now == $prev ? $http_date : ($http_date = time2str($prev = $now)); +} + sub new ($$$) { my ($class, $sock, $addr, $httpd) = @_; my $self = fields::new($class); @@ -148,7 +155,7 @@ sub response_header_write { ($conn =~ /\bkeep-alive\b/i); $h .= 'Connection: ' . ($alive ? 'keep-alive' : 'close'); - $h .= "\r\nDate: " . time2str(time) . "\r\n\r\n"; + $h .= "\r\nDate: " . http_date() . "\r\n\r\n"; if (($len || $chunked) && $env->{REQUEST_METHOD} ne 'HEAD') { more($self, $h); -- cgit v1.2.3-24-ge0c7