]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPdeflate.pm
nntp: reduce memory overhead of zlib
[public-inbox.git] / lib / PublicInbox / NNTPdeflate.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # RFC 8054 NNTP COMPRESS DEFLATE implementation
5 #
6 # RSS usage for 10K idle-but-did-something NNTP clients on 64-bit:
7 #   TLS + DEFLATE[a] :  1.8 GB  (MemLevel=9, 1.2 GB with MemLevel=8)
8 #   TLS + DEFLATE[b] :  ~300MB
9 #   TLS only         :  <200MB
10 #   plain            :   <50MB
11 #
12 # [a] - initial implementation using per-client Deflate contexts and buffer
13 #
14 # [b] - memory-optimized implementation using a global deflate context.
15 #       It's less efficient in terms of compression, but way more
16 #       efficient in terms of server memory usage.
17 package PublicInbox::NNTPdeflate;
18 use strict;
19 use warnings;
20 use 5.010_001;
21 use base qw(PublicInbox::NNTP);
22 use Compress::Raw::Zlib;
23 use Hash::Util qw(unlock_hash); # dependency of fields for perl 5.10+, anyways
24
25 my %IN_OPT = (
26         -Bufsize => PublicInbox::NNTP::LINE_MAX,
27         -WindowBits => -15, # RFC 1951
28         -AppendOutput => 1,
29 );
30
31 # global deflate context and buffer
32 my $zbuf = \(my $buf = '');
33 my $zout = Compress::Raw::Zlib::Deflate->new(
34         # nnrpd (INN) and Compress::Raw::Zlib favor MemLevel=9,
35         # but the zlib C library and git use MemLevel=8 as the default.
36         # FIXME: sometimes clients fail with 8, so we use 9
37         # -MemLevel => 9,
38
39         # needs more testing, nothing obviously different in terms of memory
40         -Bufsize => 65536,
41
42         -WindowBits => -15, # RFC 1951
43         -AppendOutput => 1,
44 );
45
46 sub enable {
47         my ($class, $self) = @_;
48         unlock_hash(%$self);
49         bless $self, $class;
50         $self->{zin} = [ Compress::Raw::Zlib::Inflate->new(%IN_OPT), '' ];
51 }
52
53 # overrides PublicInbox::NNTP::compressed
54 sub compressed { 1 }
55
56 # SUPER is PublicInbox::DS::do_read, so $_[1] may be a reference or not
57 sub do_read ($$$$) {
58         my ($self, $rbuf, $len, $off) = @_;
59
60         my $zin = $self->{zin} or return; # closed
61         my $deflated = \($zin->[1]);
62         my $r = $self->SUPER::do_read($deflated, $len) or return;
63
64         # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned
65         # -ConsumeInput is true, so $deflated is automatically emptied
66         my $err = $zin->[0]->inflate($deflated, $rbuf);
67         if ($err == Z_OK) {
68                 $r = length($$rbuf) and return $r;
69                 # nothing ready, yet, get more, later
70                 $self->requeue;
71         } else {
72                 delete $self->{zin};
73                 $self->close;
74         }
75         0;
76 }
77
78 # override PublicInbox::DS::msg_more
79 sub msg_more ($$) {
80         my $self = $_[0];
81
82         # $_[1] may be a reference or not for ->deflate
83         my $err = $zout->deflate($_[1], $zbuf);
84         $err == Z_OK or die "->deflate failed $err";
85         1;
86 }
87
88 sub zflush ($) {
89         my ($self) = @_;
90
91         my $deflated = $zbuf;
92         $zbuf = \(my $next = '');
93
94         my $err = $zout->flush($deflated, Z_FULL_FLUSH);
95         $err == Z_OK or die "->flush failed $err";
96
97         # We can still let the lower socket layer do buffering:
98         PublicInbox::DS::msg_more($self, $$deflated);
99 }
100
101 # compatible with PublicInbox::DS::write, so $_[1] may be a reference or not
102 sub write ($$) {
103         my $self = $_[0];
104         return PublicInbox::DS::write($self, $_[1]) if ref($_[1]) eq 'CODE';
105
106         my $deflated = $zbuf;
107         $zbuf = \(my $next = '');
108
109         # $_[1] may be a reference or not for ->deflate
110         my $err = $zout->deflate($_[1], $deflated);
111         $err == Z_OK or die "->deflate failed $err";
112         $err = $zout->flush($deflated, Z_FULL_FLUSH);
113         $err == Z_OK or die "->flush failed $err";
114
115         # We can still let the socket layer do buffering:
116         PublicInbox::DS::write($self, $deflated);
117 }
118
119 1;