about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-06-24 23:40:00 +0000
committerEric Wong <e@80x24.org>2016-06-24 23:40:42 +0000
commit659b836f531caf75ec2ef6d64117386e655909e5 (patch)
tree8804437ecc0a5c35be2aa97c9426e412b35bfa0c /lib/PublicInbox
parentb732b131dfa1c22b49e8694699723f05cdafad4a (diff)
downloadpublic-inbox-659b836f531caf75ec2ef6d64117386e655909e5.tar.gz
We want to avoid sending 10 or 20-byte gzip headers as
separate TCP packets to reduce syscalls and avoid wasting
bandwidth.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/Mbox.pm23
1 files changed, 10 insertions, 13 deletions
diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm
index 63ec605f..1c97f959 100644
--- a/lib/PublicInbox/Mbox.pm
+++ b/lib/PublicInbox/Mbox.pm
@@ -110,7 +110,7 @@ use warnings;
 
 sub new {
         my ($class, $ctx, $cb) = @_;
-        my $buf;
+        my $buf = '';
         bless {
                 buf => \$buf,
                 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
@@ -121,19 +121,11 @@ sub new {
         }, $class;
 }
 
-sub _flush_buf {
-        my ($self) = @_;
-        my $ret = $self->{buf};
-        $ret = $$ret;
-        ${$self->{buf}} = undef;
-        $ret;
-}
-
 # called by Plack::Util::foreach or similar
 sub getline {
         my ($self) = @_;
+        my $ctx = $self->{ctx} or return;
         my $res;
-        my $ctx = $self->{ctx};
         my $ibx = $ctx->{-inbox};
         my $gz = $self->{gz};
         do {
@@ -141,8 +133,12 @@ sub getline {
                         my $msg = eval { $ibx->msg_by_mid($smsg->mid) } or next;
                         $msg = Email::Simple->new($msg);
                         $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
-                        my $ret = _flush_buf($self);
-                        return $ret if $ret;
+                        my $bref = $self->{buf};
+                        if (length($$bref) >= 8192) {
+                                my $ret = $$bref; # copy :<
+                                ${$self->{buf}} = '';
+                                return $ret;
+                        }
                 }
                 $res = $self->{cb}->($self->{opts});
                 $self->{msgs} = $res->{msgs};
@@ -150,7 +146,8 @@ sub getline {
                 $self->{opts}->{offset} += $res;
         } while ($res);
         $gz->close;
-        _flush_buf($self);
+        delete $self->{ctx};
+        ${delete $self->{buf}};
 }
 
 sub close {} # noop