]> Sergey Matveev's repositories - public-inbox.git/commitdiff
http: avoid needless time2str calls
authorEric Wong <e@80x24.org>
Mon, 29 Feb 2016 00:29:03 +0000 (00:29 +0000)
committerEric Wong <e@80x24.org>
Mon, 29 Feb 2016 00:35:18 +0000 (00:35 +0000)
Checking the time is nearly free on modern systems with
vDSO/vsyscall/similar while sprintf is always expensive.

lib/PublicInbox/HTTP.pm

index a472388d72e87949dae98535b49aa36979b8dc19..14971f4377baa57e231f2b6e8aa2de12489c8302 100644 (file)
@@ -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);