* [PATCH 25/57] http|nntp: favor "$! == EFOO" over $!{EFOO} checks
@ 2019-06-24 2:52 5% ` Eric Wong
0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2019-06-24 2:52 UTC (permalink / raw)
To: meta
Integer comparisions of "$!" are faster than hash lookups.
See commit 6fa2b29fcd0477d126ebb7db7f97b334f74bbcbc
("ds: cleanup Errno imports and favor constant comparisons")
for benchmarks.
---
lib/PublicInbox/HTTP.pm | 7 +++----
lib/PublicInbox/HTTPD/Async.pm | 3 ++-
lib/PublicInbox/NNTP.pm | 4 ++--
lib/PublicInbox/Qspawn.pm | 7 +++++--
4 files changed, 12 insertions(+), 9 deletions(-)
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);
};
--
EW
^ permalink raw reply related [relevance 5%]
* [PATCH 0/9] ds: Diet Socket
@ 2019-06-10 5:18 7% Eric Wong
2019-06-10 5:18 6% ` [PATCH 2/9] ds: cleanup Errno imports and favor constant comparisons Eric Wong
0 siblings, 1 reply; 3+ results
From: Eric Wong @ 2019-06-10 5:18 UTC (permalink / raw)
To: meta
Getting more aggressive with changes to PublicInbox::DS.
I'm thinking patches 8-9 will be important to make TLS
support easier.
On a side note: while reading the code to IO::KQueue;
I've noticed it doesn't take advantage at all of the
syscall reduction kevent() can potentially provide over
over the use of epoll_ctl+epoll_wait :<
Eric Wong (9):
ds: simplify write buffer accounting
ds: cleanup Errno imports and favor constant comparisons
ds: reduce Errno imports and drop ->close reason
ds: remove {fd} field
ds: remove steal_socket method
nntp: use sysread to append to existing buffer
ds: remove read method, here, too
ds: do not distinguish between POLLHUP and POLLERR
ds: stop caring about event flags set by epoll/poll/kqueue
lib/PublicInbox/DS.pm | 235 +++++++++------------------------
lib/PublicInbox/EvCleanup.pm | 8 +-
lib/PublicInbox/HTTP.pm | 11 +-
lib/PublicInbox/HTTPD/Async.pm | 4 +-
lib/PublicInbox/NNTP.pm | 44 +++---
5 files changed, 93 insertions(+), 209 deletions(-)
--
EW
^ permalink raw reply [relevance 7%]
* [PATCH 2/9] ds: cleanup Errno imports and favor constant comparisons
2019-06-10 5:18 7% [PATCH 0/9] ds: Diet Socket Eric Wong
@ 2019-06-10 5:18 6% ` Eric Wong
0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2019-06-10 5:18 UTC (permalink / raw)
To: meta
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 | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index 172a9f52..39f1922f 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -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;
--
EW
^ permalink raw reply related [relevance 6%]
Results 1-3 of 3 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2019-06-10 5:18 7% [PATCH 0/9] ds: Diet Socket Eric Wong
2019-06-10 5:18 6% ` [PATCH 2/9] ds: cleanup Errno imports and favor constant comparisons Eric Wong
2019-06-24 2:52 [PATCH 00/57] ds: shrink, TLS support, buffer writes to FS Eric Wong
2019-06-24 2:52 5% ` [PATCH 25/57] http|nntp: favor "$! == EFOO" over $!{EFOO} checks Eric Wong
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
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).