From 858ab5cfe5fffa6c5a4221a523db3682be8fae06 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 26 Jun 2019 08:11:10 +0000 Subject: ds: share lazy rbuf handling between HTTP and NNTP Doing this for HTTP cuts the memory usage of 10K idle-after-one-request HTTP clients from 92 MB to 47 MB. The savings over the equivalent NNTP change in commit 6f173864f5acac89769a67739b8c377510711d49, ("nntp: lazily allocate and stash rbuf") seems down to the size of HTTP requests and the fact HTTP is a client-sends-first protocol where as NNTP is server-sends-first. --- lib/PublicInbox/DS.pm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'lib/PublicInbox/DS.pm') diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index a8700bc5..28240843 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -28,6 +28,7 @@ use 5.010_001; use PublicInbox::Syscall qw(:epoll); use fields ('sock', # underlying socket + 'rbuf', # scalarref, usually undef 'wbuf', # arrayref of coderefs or GLOB refs 'wbuf_off', # offset into first element of wbuf to start writing at ); @@ -412,16 +413,27 @@ next_buf: 1; # all done } -sub do_read ($$$$) { +sub rbuf_idle ($$) { + my ($self, $rbuf) = @_; + if ($$rbuf eq '') { # who knows how long till we can read again + delete $self->{rbuf}; + } else { + $self->{rbuf} = $rbuf; + } +} + +sub do_read ($$$;$) { my ($self, $rbuf, $len, $off) = @_; - my $r = sysread($self->{sock}, $$rbuf, $len, $off); + my $r = sysread($self->{sock}, $$rbuf, $len, $off // 0); return ($r == 0 ? $self->close : $r) if defined $r; # common for clients to break connections without warning, # would be too noisy to log here: if (ref($self) eq 'IO::Socket::SSL') { my $ev = PublicInbox::TLS::epollbit() or return $self->close; + rbuf_idle($self, $rbuf); watch($self, $ev | EPOLLONESHOT); } elsif ($! == EAGAIN) { + rbuf_idle($self, $rbuf); watch($self, EPOLLIN | EPOLLONESHOT); } else { $self->close; -- cgit v1.2.3-24-ge0c7