From: Eric Wong Date: Mon, 24 Jun 2019 02:52:50 +0000 (+0000) Subject: nntp: lazily allocate and stash rbuf X-Git-Tag: v1.2.0~156^2~10 X-Git-Url: http://www.git.stargrave.org/?p=public-inbox.git;a=commitdiff_plain;h=6f173864f5acac89769a67739b8c377510711d49 nntp: lazily allocate and stash rbuf Allocating a per-client buffer up front is unnecessary and wastes a hash slot. For the majority of (non-malicious) clients, we won't need to store rbuf in a long-lived object associated with a client socket at all. This saves around 10M on 64-bit with 20K connected-but-idle clients. --- diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm index 6acfcc1b..10a2e158 100644 --- a/lib/PublicInbox/NNTP.pm +++ b/lib/PublicInbox/NNTP.pm @@ -107,7 +107,6 @@ sub new ($$$) { $self->{nntpd} = $nntpd; push @$wbuf, \&greet; $self->{wbuf} = $wbuf; - $self->{rbuf} = ''; update_idle_time($self); $expt ||= PublicInbox::EvCleanup::later(*expire_old); $self; @@ -964,7 +963,7 @@ sub event_step { # otherwise we can be buffering infinitely w/o backpressure use constant LINE_MAX => 512; # RFC 977 section 2.3 - my $rbuf = \($self->{rbuf}); + my $rbuf = $self->{rbuf} // (\(my $x = '')); my $r = 1; if (index($$rbuf, "\n") < 0) { @@ -984,6 +983,11 @@ sub event_step { return $self->close if $r < 0; my $len = bytes::length($$rbuf); return $self->close if ($len >= LINE_MAX); + if ($len) { + $self->{rbuf} = $rbuf; + } else { + delete $self->{rbuf}; + } update_idle_time($self); # maybe there's more pipelined data, or we'll have @@ -1002,7 +1006,7 @@ sub not_idle_long ($$) { # for graceful shutdown in PublicInbox::Daemon: sub busy { my ($self, $now) = @_; - ($self->{rbuf} ne '' || $self->{wbuf} || not_idle_long($self, $now)); + ($self->{rbuf} || $self->{wbuf} || not_idle_long($self, $now)); } 1;