]> Sergey Matveev's repositories - public-inbox.git/commitdiff
ds: cleanup Errno imports and favor constant comparisons
authorEric Wong <e@80x24.org>
Mon, 3 Jun 2019 09:11:00 +0000 (09:11 +0000)
committerEric Wong <e@80x24.org>
Mon, 10 Jun 2019 05:05:15 +0000 (05:05 +0000)
Stop importing unused constants, and favor integer comparisons
of `$!' over `$!{EFOO}' hash lookups.  Integer comparisons are
slightly faster, even:

Benchmark: timing 30 iterations of cmp_eq, cmp_ne, hash_hit, hash_miss...
    cmp_eq:  1 wallclock secs ( 1.61 usr +  0.00 sys =  1.61 CPU) @ 18.63/s (n=30)
    cmp_ne:  2 wallclock secs ( 1.57 usr +  0.00 sys =  1.57 CPU) @ 19.11/s (n=30)
  hash_hit:  4 wallclock secs ( 3.85 usr +  0.00 sys =  3.85 CPU) @  7.79/s (n=30)
 hash_miss:  4 wallclock secs ( 3.74 usr +  0.00 sys =  3.74 CPU) @  8.02/s (n=30)

#!/usr/bin/perl -w
use Benchmark qw(:all);
use Errno qw(EAGAIN EINTR);
my ($r, $w);
pipe($r, $w) or die 'pipe';
require IO::Handle;
$r->blocking(0);
my $buf;
my $n = 30000;
timethese(30, {
hash_hit => sub {
sysread($r, $buf, 1);
for (0..$n) {
next if $!{EAGAIN};
die 'FAIL';
}
}
,
'cmp_eq' => sub {
sysread($r, $buf, 1);
for (0..$n) {
next if $! == EAGAIN;
die 'FAIL';
}
},
hash_miss => sub {
sysread($r, $buf, 1);
for (0..$n) {
die 'FAIL' if $!{EINTR};
}
},
'cmp_ne' => sub {
sysread($r, $buf, 1);
for (0..$n) {
die 'FAIL' if $! == EINTR;
}
},
});

lib/PublicInbox/DS.pm

index 172a9f52ad53b99e5ba394c2b5aef0e70c363450..39f1922f55ea5e36b630a61d3692b5b1882587d5 100644 (file)
@@ -33,8 +33,7 @@ use fields ('sock',              # underlying socket
             'event_watch',       # bitmask of events the client is interested in (POLLIN,OUT,etc.)
             );
 
-use Errno  qw(EINPROGRESS EWOULDBLOCK EISCONN ENOTSOCK
-              EPIPE EAGAIN EBADF ECONNRESET ENOPROTOOPT);
+use Errno  qw(EPIPE EAGAIN ECONNRESET EINVAL);
 use Carp   qw(croak confess);
 
 use constant DebugLevel => 0;
@@ -462,7 +461,7 @@ sub new {
         }
 retry:
         if (epoll_ctl($Epoll, EPOLL_CTL_ADD, $fd, $ev)) {
-            if ($!{EINVAL} && ($ev & $EPOLLEXCLUSIVE)) {
+            if ($! == EINVAL && ($ev & $EPOLLEXCLUSIVE)) {
                 $EPOLLEXCLUSIVE = 0; # old kernel
                 $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP;
                 goto retry;
@@ -730,7 +729,7 @@ sub read {
     my $res = sysread($sock, $buf, $req_bytes, 0);
     DebugLevel >= 2 && $self->debugmsg("sysread = %d; \$! = %d", $res, $!);
 
-    if (! $res && $! != EWOULDBLOCK) {
+    if (! $res && $! != EAGAIN) {
         # catches 0=conn closed or undef=error
         DebugLevel >= 2 && $self->debugmsg("Fd \#%d read hit the end of the road.", $self->{fd});
         return undef;