user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH] favor readline() and print() as functions
Date: Sun, 17 May 2020 19:48:14 +0000	[thread overview]
Message-ID: <20200517194814.8670-1-e@yhbt.net> (raw)

In our inbox-writing code paths, ->getline as an OO method may
be confused with the various definitions of `getline' used by
the PSGI interface.  It's also easier to do: "perldoc -f readline"
than to figure out which class "->getline" belongs to (IO::Handle)
and lookup documentation for that.

->print is less confusing than the "readline" vs "getline"
mismatch, but we can still make it clear we're using a real
file handle and not a mock interface.

Finally, functions are a bit faster than their OO counterparts.
---
 lib/PublicInbox/Import.pm         |  8 ++++----
 lib/PublicInbox/SearchIdxShard.pm |  2 +-
 lib/PublicInbox/V2Writable.pm     |  2 +-
 script/public-inbox-convert       | 14 +++++++-------
 4 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index 792570c8..a9013504 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -160,7 +160,7 @@ sub progress {
 	my ($self, $msg) = @_;
 	return unless $self->{pid};
 	print { $self->{out} } "progress $msg\n" or wfail;
-	$self->{in}->getline eq "progress $msg\n" or die
+	readline($self->{in}) eq "progress $msg\n" or die
 		"progress $msg not received\n";
 	undef;
 }
@@ -554,7 +554,7 @@ sub replace_oids {
 			push @buf, "reset $tmp\n";
 		} elsif (/^commit (?:.+)/) {
 			if (@buf) {
-				$w->print(@buf) or wfail;
+				print $w @buf or wfail;
 				@buf = ();
 			}
 			push @buf, "commit $tmp\n";
@@ -591,7 +591,7 @@ sub replace_oids {
 				rewrite_commit($self, \@oids, \@buf, $mime);
 				$nreplace++;
 			}
-			$w->print(@buf, "\n") or wfail;
+			print $w @buf, "\n" or wfail;
 			@buf = ();
 		} elsif ($_ eq "done\n") {
 			$done = 1;
@@ -604,7 +604,7 @@ sub replace_oids {
 	}
 	close $rd or die "close fast-export failed: $?";
 	if (@buf) {
-		$w->print(@buf) or wfail;
+		print $w @buf or wfail;
 	}
 	die 'done\n not seen from fast-export' unless $done;
 	chomp(my $cmt = $self->get_mark(":$mark")) if $nreplace;
diff --git a/lib/PublicInbox/SearchIdxShard.pm b/lib/PublicInbox/SearchIdxShard.pm
index e754b038..c1f52d8b 100644
--- a/lib/PublicInbox/SearchIdxShard.pm
+++ b/lib/PublicInbox/SearchIdxShard.pm
@@ -53,7 +53,7 @@ sub shard_worker_loop ($$$$$) {
 	my ($self, $v2w, $r, $shard, $bnote) = @_;
 	$0 = "pi-v2-shard[$shard]";
 	$self->begin_txn_lazy;
-	while (my $line = $r->getline) {
+	while (my $line = readline($r)) {
 		$v2w->{current_info} = "[$shard] $line";
 		if ($line eq "commit\n") {
 			$self->commit_txn_lazy;
diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index c732b98a..513e9f23 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -606,7 +606,7 @@ sub barrier_wait {
 	my $bnote = $self->{bnote} or return;
 	my $r = $bnote->[0];
 	while (scalar keys %$barrier) {
-		defined(my $l = $r->getline) or die "EOF on barrier_wait: $!";
+		defined(my $l = readline($r)) or die "EOF on barrier_wait: $!";
 		$l =~ /\Abarrier (\d+)/ or die "bad line on barrier_wait: $l";
 		delete $barrier->{$1} or die "bad shard[$1] on barrier wait";
 	}
diff --git a/script/public-inbox-convert b/script/public-inbox-convert
index 4c220b36..7fb15adf 100755
--- a/script/public-inbox-convert
+++ b/script/public-inbox-convert
@@ -111,12 +111,12 @@ while (<$rd>) {
 		$state = 'commit';
 	} elsif (/^data ([0-9]+)/) {
 		my $len = $1;
-		$w->print($_) or $im->wfail;
+		print $w $_ or $im->wfail;
 		while ($len) {
 			my $n = read($rd, my $tmp, $len) or die "read: $!";
 			warn "$n != $len\n" if $n != $len;
 			$len -= $n;
-			$w->print($tmp) or $im->wfail;
+			print $w $tmp or $im->wfail;
 		}
 		next;
 	} elsif ($state eq 'commit') {
@@ -124,9 +124,9 @@ while (<$rd>) {
 			my ($mark, $path) = ($1, $2);
 			$D{$path} = $mark;
 			if ($last && $last ne 'm') {
-				$w->print("D $last\n") or $im->wfail;
+				print $w "D $last\n" or $im->wfail;
 			}
-			$w->print("M 100644 :$mark m\n") or $im->wfail;
+			print $w "M 100644 :$mark m\n" or $im->wfail;
 			$last = 'm';
 			next;
 		}
@@ -134,15 +134,15 @@ while (<$rd>) {
 			my $mark = delete $D{$1};
 			defined $mark or die "undeleted path: $1\n";
 			if ($last && $last ne 'd') {
-				$w->print("D $last\n") or $im->wfail;
+				print $w "D $last\n" or $im->wfail;
 			}
-			$w->print("M 100644 :$mark d\n") or $im->wfail;
+			print $w "M 100644 :$mark d\n" or $im->wfail;
 			$last = 'd';
 			next;
 		}
 	}
 	last if $_ eq "done\n";
-	$w->print($_) or $im->wfail;
+	print $w $_ or $im->wfail;
 }
 $w = $r = undef;
 close $rd or die "close fast-export: $!\n";

                 reply	other threads:[~2020-05-17 19:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20200517194814.8670-1-e@yhbt.net \
    --to=e@yhbt.net \
    --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).