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 E731E1F9E0; Wed, 29 Apr 2020 20:33:12 +0000 (UTC) Date: Wed, 29 Apr 2020 20:33:11 +0000 From: Eric Wong To: meta@public-inbox.org Subject: Re: [PATCH] git: various minor speedups Message-ID: <20200429203311.GA20984@dcvr> References: <20200428084858.16048-1-e@yhbt.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200428084858.16048-1-e@yhbt.net> List-Id: Eric Wong wrote: > Best off all, there's less code :> And even less... > --- a/lib/PublicInbox/Git.pm > +++ b/lib/PublicInbox/Git.pm > @@ -125,59 +125,49 @@ sub _bidi_pipe { > $self->{$in} = $in_r; > } > > -sub read_cat_in_full ($$$) { > - my ($self, $in, $left) = @_; > - my $offset = 0; > - my $buf = ''; > - while ($left > 0) { > - my $r = read($in, $buf, $left, $offset); > - defined($r) or fail($self, "read failed: $!"); > - $r == 0 and fail($self, 'exited unexpectedly'); > - $left -= $r; > - $offset += $r; > - } > - my $r = read($in, my $lf, 1); > - defined($r) or fail($self, "read failed: $!"); > - fail($self, 'newline missing after blob') if ($r != 1 || $lf ne "\n"); > +sub read_cat_in_full ($$) { > + my ($self, $left) = @_; > + ++$left; # for final "\n" added by git > + my $r = read($self->{in}, my $buf, $left) == $left or > + fail($self, 'short read'); > + chop($buf) eq "\n" or fail($self, 'newline missing after blob'); > \$buf; > } No need to save `$r', and `$len' makes a better name for a single read() than `$left' with the new code. Will squash this in: diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index f1911534..057135ef 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -126,10 +126,9 @@ sub _bidi_pipe { } sub read_cat_in_full ($$) { - my ($self, $left) = @_; - ++$left; # for final "\n" added by git - my $r = read($self->{in}, my $buf, $left) == $left or - fail($self, 'short read'); + my ($self, $len) = @_; + ++$len; # for final "\n" added by git + read($self->{in}, my $buf, $len) == $len or fail($self, 'short read'); chop($buf) eq "\n" or fail($self, 'newline missing after blob'); \$buf; }