about summary refs log tree commit homepage
path: root/lib/PublicInbox/HTTP.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-02-29 00:29:03 +0000
committerEric Wong <e@80x24.org>2016-02-29 00:35:18 +0000
commite3fd8551e919b95a78f1f0ab0cfff12033291eeb (patch)
tree9d60bb96e20003a94f6fe03fd4bf8a315fa02993 /lib/PublicInbox/HTTP.pm
parentf3bb7c21292cf91a203a1afd0833b84e3c1b1e20 (diff)
downloadpublic-inbox-e3fd8551e919b95a78f1f0ab0cfff12033291eeb.tar.gz
Checking the time is nearly free on modern systems with
vDSO/vsyscall/similar while sprintf is always expensive.
Diffstat (limited to 'lib/PublicInbox/HTTP.pm')
-rw-r--r--lib/PublicInbox/HTTP.pm11
1 files changed, 9 insertions, 2 deletions
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);