From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 2A3971F4BF for ; Mon, 24 Jun 2019 02:58:09 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 49/57] nntp: lazily allocate and stash rbuf Date: Mon, 24 Jun 2019 02:52:50 +0000 Message-Id: <20190624025258.25592-50-e@80x24.org> In-Reply-To: <20190624025258.25592-1-e@80x24.org> References: <20190624025258.25592-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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. --- lib/PublicInbox/NNTP.pm | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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; -- EW