From 2ac2023fa416e31189708c355db8728abbd9ef2c Mon Sep 17 00:00:00 2001
From: Eric Wong <e@80x24.org>
Date: Sun, 28 Feb 2016 11:28:33 +0000
Subject: [PATCH] reduce calls to close unless error checks are needed

We can rely on timely auto-destruction based on reference
counting; reducing the chance of redundant close(2) calls
which may hit the wront FD.

We do care about certain close calls (e.g. writing to a buffered
IO handle) if we require error-checking for write-integrity.  In
other cases, let things go out-of-scope so it can be freed
automatically after use.
---
 lib/PublicInbox/Config.pm      |  1 -
 lib/PublicInbox/Feed.pm        |  2 --
 lib/PublicInbox/Git.pm         | 10 ++--------
 lib/PublicInbox/ProcessPipe.pm |  5 ++---
 lib/PublicInbox/SearchIdx.pm   |  2 --
 lib/PublicInbox/Spawn.pm       |  1 -
 6 files changed, 4 insertions(+), 17 deletions(-)

diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm
index 844f666e..b5116388 100644
--- a/lib/PublicInbox/Config.pm
+++ b/lib/PublicInbox/Config.pm
@@ -101,7 +101,6 @@ sub try_cat {
 	if (open(my $fh, '<', $path)) {
 		local $/;
 		$rv = <$fh>;
-		close $fh;
 	}
 	$rv;
 }
diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index a5828a8e..54cbf23c 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -255,7 +255,6 @@ sub each_recent_blob {
 		}
 	}
 
-	close $log; # we may EPIPE here
 	# for pagination
 	($first_commit, $last_commit);
 }
@@ -269,7 +268,6 @@ sub get_feedopts {
 	my %rv;
 	if (open my $fh, '<', "$ctx->{git_dir}/description") {
 		chomp($rv{description} = <$fh>);
-		close $fh;
 	} else {
 		$rv{description} = '($GIT_DIR/description missing)';
 	}
diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm
index 57d17d33..0f92dd9a 100644
--- a/lib/PublicInbox/Git.pm
+++ b/lib/PublicInbox/Git.pm
@@ -29,8 +29,6 @@ sub _bidi_pipe {
 	my @cmd = ('git', "--git-dir=$self->{git_dir}", qw(cat-file), $batch);
 	my $redir = { 0 => fileno($out_r), 1 => fileno($in_w) };
 	$self->{$pid} = spawn(\@cmd, undef, $redir);
-	close $out_r or fail($self, "close failed: $!");
-	close $in_w or fail($self, "close failed: $!");
 	$out_w->autoflush(1);
 	$self->{$out} = $out_w;
 	$self->{$in} = $in_r;
@@ -100,13 +98,9 @@ sub check {
 
 sub _destroy {
 	my ($self, $in, $out, $pid) = @_;
-	my $p = $self->{$pid} or return;
-	$self->{$pid} = undef;
+	my $p = delete $self->{$pid} or return;
 	foreach my $f ($in, $out) {
-		my $fh = $self->{$f};
-		defined $fh or next;
-		close $fh;
-		$self->{$f} = undef;
+		delete $self->{$f};
 	}
 	waitpid $p, 0;
 }
diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm
index eade524c..e088c105 100644
--- a/lib/PublicInbox/ProcessPipe.pm
+++ b/lib/PublicInbox/ProcessPipe.pm
@@ -15,13 +15,12 @@ sub READ { sysread($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
 
 sub READLINE { readline($_[0]->{fh}) }
 
-sub CLOSE { close($_[0]->{fh}) }
+sub CLOSE { delete($_[0]->{fh}) }
 
 sub FILENO { fileno($_[0]->{fh}) }
 
 sub DESTROY {
-	my $fh = delete($_[0]->{fh});
-	close $fh if $fh;
+	delete($_[0]->{fh});
 	waitpid($_[0]->{pid}, 0);
 }
 
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 1d0d926f..415decd1 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -348,7 +348,6 @@ sub rlog {
 			$latest = $1;
 		}
 	}
-	close $log;
 	$latest;
 }
 
@@ -446,7 +445,6 @@ sub _read_git_config_perm {
 	my @cmd = qw(config core.sharedRepository);
 	my $fh = PublicInbox::Git->new($self->{git_dir})->popen(@cmd);
 	my $perm = <$fh>;
-	close $fh;
 	chomp $perm if defined $perm;
 	$perm;
 }
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 51ad2692..02c5446f 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -171,7 +171,6 @@ sub popen_rd {
 	$r->blocking($blocking) if defined $blocking;
 	$opts->{1} = fileno($w);
 	my $pid = spawn($cmd, $env, $opts);
-	close $w;
 	return ($r, $pid) if wantarray;
 	my $ret = gensym;
 	tie *$ret, 'PublicInbox::ProcessPipe', $pid, $r;
-- 
2.50.0