From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, RP_MATCHES_RCVD shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: meta@public-inbox.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id D9542633820 for ; Mon, 29 Feb 2016 01:41:11 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 3/8] http: avoid needless time2str calls Date: Mon, 29 Feb 2016 01:41:02 +0000 Message-Id: <20160229014107.7396-4-e@80x24.org> In-Reply-To: <20160229014107.7396-1-e@80x24.org> References: <20160229014107.7396-1-e@80x24.org> List-Id: 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(-) diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index a472388..14971f4 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); -- EW