about summary refs log tree commit homepage
path: root/lib/PublicInbox/NNTP.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-06-24 02:52:50 +0000
committerEric Wong <e@80x24.org>2019-06-24 05:26:27 +0000
commit6f173864f5acac89769a67739b8c377510711d49 (patch)
treeb20c9be3e0d6479dc78a81a76204069dfa1c2b8c /lib/PublicInbox/NNTP.pm
parent3667c96f84a39c8fbfaf4cef37f58a9db06bb4b3 (diff)
downloadpublic-inbox-6f173864f5acac89769a67739b8c377510711d49.tar.gz
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.
Diffstat (limited to 'lib/PublicInbox/NNTP.pm')
-rw-r--r--lib/PublicInbox/NNTP.pm10
1 files 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;