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 E66B61F9F3 for ; Mon, 5 Apr 2021 10:27:52 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 4/5] lei_to_mail: improve comments and reduce LoC Date: Mon, 5 Apr 2021 10:27:51 +0000 Message-Id: <20210405102752.6249-5-e@80x24.org> In-Reply-To: <20210405102752.6249-1-e@80x24.org> References: <20210405102752.6249-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We don't need to waste LoC on corner cases, single-use internal subs, or restoring SIG{__WARN__} when a process exits. All that extra code contributes to memory use and startup time, especially for users who can't use FD passing. --- lib/PublicInbox/LeiToMail.pm | 42 +++++++++++++++--------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm index 2e736070..9411313b 100644 --- a/lib/PublicInbox/LeiToMail.pm +++ b/lib/PublicInbox/LeiToMail.pm @@ -138,18 +138,11 @@ sub eml2mboxcl2 { sub git_to_mail { # git->cat_async callback my ($bref, $oid, $type, $size, $arg) = @_; - if ($type ne 'blob') { - if ($type eq 'missing') { - warn "missing $oid\n"; - } else { - warn "unexpected type=$type for $oid\n"; - } - } + return warn("W: $oid is $type (!= blob)\n") if $type ne 'blob'; + return warn("E: $oid is empty\n") unless $size; my ($write_cb, $smsg) = @$arg; - if ($smsg->{blob} ne $oid) { - die "BUG: expected=$smsg->{blob} got=$oid"; - } - $write_cb->($bref, $smsg) if $size > 0; + die "BUG: expected=$smsg->{blob} got=$oid" if $smsg->{blob} ne $oid; + $write_cb->($bref, $smsg); } sub reap_compress { # dwaitpid callback @@ -159,7 +152,7 @@ sub reap_compress { # dwaitpid callback $lei->fail("@$cmd failed", $? >> 8); } -sub _post_augment_mbox { # open a compressor process +sub _post_augment_mbox { # open a compressor process from top-level process my ($self, $lei) = @_; my $zsfx = $self->{zsfx} or return; my $cmd = PublicInbox::MboxReader::zsfx2cmd($zsfx, undef, $lei); @@ -173,12 +166,6 @@ sub _post_augment_mbox { # open a compressor process $lei->{1} = $pp; } -sub dup_src ($) { - my ($in) = @_; - open my $dup, '+>>&', $in or die "dup: $!"; - $dup; -} - # --augment existing output destination, with deduplication sub _augment { # MboxReader eml_cb my ($eml, $lei) = @_; @@ -309,7 +296,7 @@ sub _imap_write_cb ($$) { my $lse = $lei->{lse}; # may be undef sub { # for git_to_mail my ($bref, $smsg, $eml) = @_; - $mic // return $lei->fail; # dst may be undef-ed in last run + $mic // return $lei->fail; # mic may be undef-ed in last run if ($dedupe) { $eml //= PublicInbox::Eml->new($$bref); # copy bref return if $dedupe->is_dup($eml, $smsg->{blob}); @@ -488,9 +475,12 @@ sub _do_augment_mbox { truncate($out, 0) or die "truncate($dst): $!"; return; } - my $zsfx = $self->{zsfx}; - my $rd = $zsfx ? PublicInbox::MboxReader::zsfxcat($out, $zsfx, $lei) - : dup_src($out); + my $rd; + if (my $zsfx = $self->{zsfx}) { + $rd = PublicInbox::MboxReader::zsfxcat($out, $zsfx, $lei); + } else { + open($rd, '+>>&', $out) or die "dup: $!"; + } my $dedupe; if ($opt->{augment}) { $dedupe = $lei->{dedupe}; @@ -535,6 +525,7 @@ sub post_augment { $m->($self, $lei, @args); } +# called by every single l2m worker process sub do_post_auth { my ($self) = @_; my $lei = $self->{lei}; @@ -542,7 +533,7 @@ sub do_post_auth { pkt_do($lei->{pkt_op_p}, 'incr_start_query') or die "incr_start_query: $!"; my $aug; - if (lock_free($self)) { + if (lock_free($self)) { # all workers do_augment my $mod = $self->{-wq_nr_workers}; my $shard = $self->{-wq_worker_nr}; if (my $net = $lei->{net}) { @@ -551,7 +542,7 @@ sub do_post_auth { $self->{shard_info} = [ $mod, $shard ]; } $aug = '+'; # incr_post_augment - } elsif ($self->{-wq_worker_nr} == 0) { + } elsif ($self->{-wq_worker_nr} == 0) { # 1st worker do_augment $aug = '.'; # do_post_augment } if ($aug) { @@ -561,6 +552,7 @@ sub do_post_auth { pkt_do($lei->{pkt_op_p}, $aug) == 1 or die "do_post_augment trigger: $!"; } + # done augmenting, connect the compressor pipe for each worker if (my $zpipe = delete $lei->{zpipe}) { $lei->{1} = $zpipe->[1]; close $zpipe->[0]; @@ -581,6 +573,7 @@ sub lock_free { $_[0]->{base_type} =~ /\A(?:maildir|imap|jmap)\z/ ? 1 : 0; } +# wakes up the MUA when complete so it can refresh messages list sub poke_dst { my ($self) = @_; if ($self->{base_type} eq 'maildir') { @@ -599,7 +592,6 @@ sub wq_atexit_child { my ($self) = @_; delete $self->{wcb}; $self->{lei}->{ale}->git->async_wait_all; - $SIG{__WARN__} = 'DEFAULT'; } # called in top-level lei-daemon when LeiAuth is done