user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
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	[thread overview]
Message-ID: <20210405102752.6249-5-e@80x24.org> (raw)
In-Reply-To: <20210405102752.6249-1-e@80x24.org>

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

  parent reply	other threads:[~2021-04-05 10:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-05 10:27 [PATCH 0/5] lei_to_mail fixes Eric Wong
2021-04-05 10:27 ` [PATCH 1/5] lei_to_mail: trim down imports Eric Wong
2021-04-05 10:27 ` [PATCH 2/5] lei_tag: fix comments w.r.t support levels Eric Wong
2021-04-05 10:27 ` [PATCH 3/5] lei: maildir: move shard support to MdirReader Eric Wong
2021-04-05 10:27 ` Eric Wong [this message]
2021-04-05 10:27 ` [PATCH 5/5] lei q: fix auth IMAP --output with remote mboxrd Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210405102752.6249-5-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).