unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/8] http: avoid needless time2str calls
Date: Mon, 29 Feb 2016 01:41:02 +0000	[thread overview]
Message-ID: <20160229014107.7396-4-e@80x24.org> (raw)
In-Reply-To: <20160229014107.7396-1-e@80x24.org>

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


  parent reply	other threads:[~2016-02-29  1:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-29  1:40 [PATCH 0/8] another round of HTTP-related fixes Eric Wong
2016-02-29  1:41 ` [PATCH 1/8] http: error check for sysseek on input Eric Wong
2016-02-29  1:41 ` [PATCH 2/8] http: document event_write usage Eric Wong
2016-02-29  1:41 ` Eric Wong [this message]
2016-02-29  1:41 ` [PATCH 4/8] distinguish error messages intended for users vs developers Eric Wong
2016-02-29  1:41 ` [PATCH 5/8] favor procedural calls for most private functions Eric Wong
2016-02-29  1:41 ` [PATCH 6/8] git-http-backend: stricter parsing of CRLF Eric Wong
2016-02-29  1:41 ` [PATCH 7/8] spawnpp: use env(1) for mod_perl compatibility Eric Wong
2016-02-29  1:41 ` [PATCH 8/8] git-http-backend: fixes for mod_perl Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160229014107.7396-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).