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 EE9DE1F4BC for ; Mon, 24 Jun 2019 02:52:59 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 06/57] ds: lazy initialize wbuf_off Date: Mon, 24 Jun 2019 02:52:07 +0000 Message-Id: <20190624025258.25592-7-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: Since Perl 5.10+, "fields" makes a restricted hash; not a compile-time-defined array (struct) with fixed offsets as it did in Perl <= 5.8. Thus in-use fields cost memory, and since the write buffer offset is rarely needed; stop relying on it. --- lib/PublicInbox/DS.pm | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 26c5251b..154fd4dd 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -403,7 +403,6 @@ sub new { unless $sock && $fd; $self->{wbuf} = []; - $self->{wbuf_off} = 0; my $ev = $self->{event_watch} = POLLERR|POLLHUP|POLLNVAL; @@ -501,7 +500,7 @@ sub write { # now-dead object does its second write. that is this case. we # just lie and say it worked. it'll be dead soon and won't be # hurt by this lie. - return 1 unless $self->{sock}; + my $sock = $self->{sock} or return 1; my $bref; @@ -548,9 +547,9 @@ sub write { die "Write error: $@ <$bref>"; } - my $to_write = $len - $self->{wbuf_off}; - my $written = syswrite($self->{sock}, $$bref, $to_write, - $self->{wbuf_off}); + my $off = $self->{wbuf_off} // 0; + my $to_write = $len - $off; + my $written = syswrite($sock, $$bref, $to_write, $off); if (! defined $written) { if ($! == EAGAIN) { @@ -570,11 +569,11 @@ sub write { } # since connection has stuff to write, it should now be # interested in pending writes: - $self->{wbuf_off} += $written; + $self->{wbuf_off} = $off + $written; $self->watch_write(1); return 0; } elsif ($written == $to_write) { - $self->{wbuf_off} = 0; + delete $self->{wbuf_off}; $self->watch_write(0); # this was our only write, so we can return immediately -- EW