From: Eric Wong <e@80x24.org>
Date: Mon, 24 Jun 2019 02:52:26 +0000 (+0000)
Subject: http|nntp: favor "$! == EFOO" over $!{EFOO} checks
X-Git-Tag: v1.2.0~156^2~34
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=7c89df780b7b160fe85b8a355455d06ec8499205;p=public-inbox.git

http|nntp: favor "$! == EFOO" over $!{EFOO} checks

Integer comparisions of "$!" are faster than hash lookups.

See commit 6fa2b29fcd0477d126ebb7db7f97b334f74bbcbc
("ds: cleanup Errno imports and favor constant comparisons")
for benchmarks.
---

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 773d77ba..4738e156 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -27,6 +27,7 @@ use constant {
 	CHUNK_ZEND => -3,    # \r\n
 	CHUNK_MAX_HDR => 256,
 };
+use Errno qw(EAGAIN);
 
 my $pipelineq = [];
 my $pipet;
@@ -82,11 +83,9 @@ sub event_step { # called by PublicInbox::DS
 		return rbuf_process($self);
 	}
 
-	return $self->watch_in1 if $!{EAGAIN};
-
 	# common for clients to break connections without warning,
 	# would be too noisy to log here:
-	return $self->close;
+	$! == EAGAIN ? $self->watch_in1 : $self->close;
 }
 
 sub rbuf_process {
@@ -359,7 +358,7 @@ sub write_err {
 sub recv_err {
 	my ($self, $r, $len) = @_;
 	return $self->close if (defined $r && $r == 0);
-	if ($!{EAGAIN}) {
+	if ($! == EAGAIN) {
 		$self->{input_left} = $len;
 		return $self->watch_in1;
 	}
diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm
index 3eb7f75a..9cc41f17 100644
--- a/lib/PublicInbox/HTTPD/Async.pm
+++ b/lib/PublicInbox/HTTPD/Async.pm
@@ -11,6 +11,7 @@ use warnings;
 use base qw(PublicInbox::DS);
 use fields qw(cb cleanup);
 require PublicInbox::EvCleanup;
+use Errno qw(EAGAIN);
 
 sub new {
 	my ($class, $io, $cb, $cleanup) = @_;
@@ -57,7 +58,7 @@ sub main_cb ($$$) {
 			}
 			# fall through to close below...
 		} elsif (!defined $r) {
-			return restart_read($self) if $!{EAGAIN};
+			return restart_read($self) if $! == EAGAIN;
 		}
 
 		# Done! Error handling will happen in $fh->close
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 98f88410..fbdf1364 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -25,6 +25,7 @@ use constant {
 	r430 => '430 No article with that message-id',
 };
 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
+use Errno qw(EAGAIN);
 
 my @OVERVIEW = qw(Subject From Date Message-ID References Xref);
 my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n";
@@ -946,8 +947,7 @@ sub event_step {
 		my $off = length($$rbuf);
 		$r = sysread($self->{sock}, $$rbuf, LINE_MAX, $off);
 		unless (defined $r) {
-			return $self->watch_in1 if $!{EAGAIN};
-			return $self->close;
+			return $! == EAGAIN ? $self->watch_in1 : $self->close;
 		}
 		return $self->close if $r == 0;
 	}
diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm
index 943ee801..f2630a0f 100644
--- a/lib/PublicInbox/Qspawn.pm
+++ b/lib/PublicInbox/Qspawn.pm
@@ -29,6 +29,9 @@ use warnings;
 use PublicInbox::Spawn qw(popen_rd);
 require Plack::Util;
 
+# n.b.: we get EAGAIN with public-inbox-httpd, and EINTR on other PSGI servers
+use Errno qw(EAGAIN EINTR);
+
 my $def_limiter;
 
 # declares a command to spawn (but does not spawn it).
@@ -131,7 +134,7 @@ sub psgi_qx {
 		} elsif (defined $r) {
 			$r ? $qx->write($buf) : $end->();
 		} else {
-			return if $!{EAGAIN} || $!{EINTR}; # loop again
+			return if $! == EAGAIN || $! == EINTR; # loop again
 			$end->();
 		}
 	};
@@ -193,7 +196,7 @@ sub psgi_return {
 	my $buf = '';
 	my $rd_hdr = sub {
 		my $r = sysread($rpipe, $buf, 1024, length($buf));
-		return if !defined($r) && ($!{EINTR} || $!{EAGAIN});
+		return if !defined($r) && $! == EAGAIN || $! == EINTR;
 		$parse_hdr->($r, \$buf);
 	};