]> Sergey Matveev's repositories - public-inbox.git/commitdiff
GitCatFile: use offset for read instead of appending
authorEric Wong <e@80x24.org>
Sun, 23 Aug 2015 02:40:19 +0000 (02:40 +0000)
committerEric Wong <e@80x24.org>
Sun, 23 Aug 2015 02:40:39 +0000 (02:40 +0000)
There is no need to perform string appends when the
"read" and "sysread" functions take an offset argument
to append to the given buffer.

This avoid needless string creation.

lib/PublicInbox/GitCatFile.pm

index 8bc6a238d2ae2c8f09c5062091e2ae140245d0c6..9bffce2aa2a727db21e1e42431dafc66310133c2 100644 (file)
@@ -68,17 +68,17 @@ sub cat_file {
 
        my $size = $1;
        my $bytes_left = $size;
-       my $buf;
+       my $offset = 0;
        my $rv = '';
 
        while ($bytes_left) {
-               my $read = read($in, $buf, $bytes_left);
-               defined($read) or die "read pipe failed: $!\n";
-               $rv .= $buf;
+               my $read = read($in, $rv, $bytes_left, $offset);
+               defined($read) or die "sysread pipe failed: $!\n";
                $bytes_left -= $read;
+               $offset += $read;
        }
 
-       my $read = read($in, $buf, 1);
+       my $read = read($in, my $buf, 1);
        defined($read) or die "read pipe failed: $!\n";
        if ($read != 1 || $buf ne "\n") {
                die "newline missing after blob\n";