threads / patch / 53967

patchmidx: use buffered I/O to talk to pack-objects

Subject: [PATCH] midx: use buffered I/O to talk to pack-objects

## tl;dr

14 messages between Aug 2, 2020 and Aug 13, 2020. Diffs are folded; open one to read it.

replies: 13people: 6as markdown or json

René Scharfe· Aug 2, 2020, 14:38 UTC · lore

Like f0bca72dc77 (send-pack: use buffered I/O to talk to pack-objects, 2016-06-08), significantly reduce the number of system calls and simplify the code for sending object IDs to pack-objects by using stdio's buffering and handling errors after the loop.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 midx.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
Show changes to midx.c +11 −4
diff --git a/midx.c b/midx.c
index 6d1584ca51d..742638c3e51 100644
--- a/midx.c
+++ b/midx.c
@@ -1383,6 +1383,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 	uint32_t i;
 	unsigned char *include_pack;
 	struct child_process cmd = CHILD_PROCESS_INIT;
+	FILE *cmd_in;
 	struct strbuf base_name = STRBUF_INIT;
 	struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);

@@ -1435,6 +1436,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 		goto cleanup;
 	}

+	cmd_in = xfdopen(cmd.in, "w");
+
 	for (i = 0; i < m->num_objects; i++) {
 		struct object_id oid;
 		uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
@@ -1443,10 +1446,15 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 			continue;

 		nth_midxed_object_oid(&oid, m, i);
-		xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
-		xwrite(cmd.in, "\n", 1);
+		fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
+	}
+
+	if (fclose(cmd_in)) {
+		error_errno(_("could not close stdin of pack-objects"));
+		result = 1;
+		finish_command(&cmd);
+		goto cleanup;
 	}
-	close(cmd.in);

 	if (finish_command(&cmd)) {
 		error(_("could not finish pack-objects"));
--
2.28.0
Chris Torek· Aug 2, 2020, 16:11 UTC · re: René Scharfe · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

On Sun, Aug 2, 2020 at 7:40 AM René Scharfe <l.s.r@web.de> wrote:
Show 21 quoted lines
> @@ -1443,10 +1446,15 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
>                         continue;
>
>                 nth_midxed_object_oid(&oid, m, i);
> -               xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
> -               xwrite(cmd.in, "\n", 1);
> +               fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
> +       }
> +
> +       if (fclose(cmd_in)) {
> +               error_errno(_("could not close stdin of pack-objects"));
> +               result = 1;
> +               finish_command(&cmd);
> +               goto cleanup;
>         }
> -       close(cmd.in);
>
>         if (finish_command(&cmd)) {
>                 error(_("could not finish pack-objects"));
> --
> 2.28.0

Here, we don't have any explicit errno checking, but of course error_errno() uses errno. This too needs an ferror() (or fflush()) test before the final fclose(), and then we just need to use plain error(). Otherwise you'll need the clumsier test-after-each-fprintf() and an explicit final fflush()-and-test.

Chris
Johannes Sixt· Aug 3, 2020, 18:10 UTC · re: Chris Torek · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

Am 02.08.20 um 18:11 schrieb Chris Torek:
Show 29 quoted lines
> On Sun, Aug 2, 2020 at 7:40 AM René Scharfe <l.s.r@web.de> wrote:
>> @@ -1443,10 +1446,15 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
>>                         continue;
>>
>>                 nth_midxed_object_oid(&oid, m, i);
>> -               xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
>> -               xwrite(cmd.in, "\n", 1);
>> +               fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
>> +       }
>> +
>> +       if (fclose(cmd_in)) {
>> +               error_errno(_("could not close stdin of pack-objects"));
>> +               result = 1;
>> +               finish_command(&cmd);
>> +               goto cleanup;
>>         }
>> -       close(cmd.in);
>>
>>         if (finish_command(&cmd)) {
>>                 error(_("could not finish pack-objects"));
>> --
>> 2.28.0
> 
> Here, we don't have any explicit errno checking, but
> of course error_errno() uses errno.  This too needs
> an ferror() (or fflush()) test before the final fclose(),
> and then we just need to use plain error().  Otherwise
> you'll need the clumsier test-after-each-fprintf() and
> an explicit final fflush()-and-test.

We need this explicit test after each fprintf anyway because SIGPIPE may be ignored, and then writing fails with EPIPE. On Windows, this is doubly important because we do not have SIGPIPE at all (and always see EPIPE), but we see EPIPE only on the first failed write; subsequent writes produce EINVAL.

-- Hannes
René Scharfe· Aug 3, 2020, 22:27 UTC · re: Johannes Sixt · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

Am 03.08.20 um 20:10 schrieb Johannes Sixt:
Show 30 quoted lines
> Am 02.08.20 um 18:11 schrieb Chris Torek:
>> On Sun, Aug 2, 2020 at 7:40 AM René Scharfe <l.s.r@web.de> wrote:
>>> @@ -1443,10 +1446,15 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
>>>                         continue;
>>>
>>>                 nth_midxed_object_oid(&oid, m, i);
>>> -               xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
>>> -               xwrite(cmd.in, "\n", 1);
>>> +               fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
>>> +       }
>>> +
>>> +       if (fclose(cmd_in)) {
>>> +               error_errno(_("could not close stdin of pack-objects"));
>>> +               result = 1;
>>> +               finish_command(&cmd);
>>> +               goto cleanup;
>>>         }
>>> -       close(cmd.in);
>>>
>>>         if (finish_command(&cmd)) {
>>>                 error(_("could not finish pack-objects"));
>>> --
>>> 2.28.0
>>
>> Here, we don't have any explicit errno checking, but
>> of course error_errno() uses errno.  This too needs
>> an ferror() (or fflush()) test before the final fclose(),
>> and then we just need to use plain error().  Otherwise
>> you'll need the clumsier test-after-each-fprintf() and
>> an explicit final fflush()-and-test.

OK, the implicit fflush() called by fclose() and thus fclose() itself can succeed even if the error indicator is set, in particular if that fflush() has nothing to do. So we need to check ferror() before calling fclose().

If ferror() tells us there was an error, errno might contain some random error code, but not necessarily the root cause. Thus we better keep quiet about it and only use error() to tell the user we failed to talk to our child but we don't know why.

We could fflush() explicitly before fclose(), but fclose() reports any failure of its implicit fflush() anyway , so we don't gain anything by doing so.

Did I get that right?
Show 5 quoted lines
> We need this explicit test after each fprintf anyway because SIGPIPE may
> be ignored, and then writing fails with EPIPE. On Windows, this is
> doubly important because we do not have SIGPIPE at all (and always see
> EPIPE), but we see EPIPE only on the first failed write; subsequent
> writes produce EINVAL.

Why is this important? The current code doesn't care about it, at least. It does care about EINTR, though.

René
René Scharfe· Aug 4, 2020, 04:31 UTC · re: René Scharfe · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

Am 04.08.20 um 00:27 schrieb René Scharfe:
Show 24 quoted lines
> Am 03.08.20 um 20:10 schrieb Johannes Sixt:
>> Am 02.08.20 um 18:11 schrieb Chris Torek:
>>> On Sun, Aug 2, 2020 at 7:40 AM René Scharfe <l.s.r@web.de> wrote:
>>>> @@ -1443,10 +1446,15 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
>>>>                         continue;
>>>>
>>>>                 nth_midxed_object_oid(&oid, m, i);
>>>> -               xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
>>>> -               xwrite(cmd.in, "\n", 1);
>>>> +               fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
>>>> +       }
>>>> +
>>>> +       if (fclose(cmd_in)) {
>>>> +               error_errno(_("could not close stdin of pack-objects"));
>>>> +               result = 1;
>>>> +               finish_command(&cmd);
>>>> +               goto cleanup;
>>>>         }
>>>> -       close(cmd.in);
>>>>
>>>>         if (finish_command(&cmd)) {
>>>>                 error(_("could not finish pack-objects"));
>>>> --
>>>> 2.28.0
Show 8 quoted lines
>> We need this explicit test after each fprintf anyway because SIGPIPE may
>> be ignored, and then writing fails with EPIPE. On Windows, this is
>> doubly important because we do not have SIGPIPE at all (and always see
>> EPIPE), but we see EPIPE only on the first failed write; subsequent
>> writes produce EINVAL.
>
> Why is this important?  The current code doesn't care about it, at
> least.  It does care about EINTR, though.

Ah, that's the point, right? You want to *ignore* EPIPE, because the failed pack-objects process at the other end will have died with a (hopefully) useful error message already.

OK, so we also need a fprintf() wrapper that retries on EINTR, ignores EPIPE and exits early if the error indicator is set?

Somehow staying with write(2) and its friends and just adding strbuf based buffering looks attractive to me now. :-/

René
Junio C Hamano· Aug 4, 2020, 04:37 UTC · re: René Scharfe · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

René Scharfe <l.s.r@web.de> writes:
> Somehow staying with write(2) and its friends and just adding strbuf
> based buffering looks attractive to me now. :-/
Indeed :-/
Derrick Stolee· Aug 3, 2020, 12:39 UTC · re: René Scharfe · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

On 8/2/2020 10:38 AM, René Scharfe wrote:
> Like f0bca72dc77 (send-pack: use buffered I/O to talk to pack-objects,
> 2016-06-08), significantly reduce the number of system calls and
> simplify the code for sending object IDs to pack-objects by using
> stdio's buffering and handling errors after the loop.
Good find. Thanks for doing this important cleanup.

Outside of Chris's other feedback, this looks like an obviously correct transformation.

Thanks, -Stolee

René Scharfe· Aug 11, 2020, 16:08 UTC · re: Derrick Stolee · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

Am 03.08.20 um 14:39 schrieb Derrick Stolee:
Show 10 quoted lines
> On 8/2/2020 10:38 AM, René Scharfe wrote:
>> Like f0bca72dc77 (send-pack: use buffered I/O to talk to pack-objects,
>> 2016-06-08), significantly reduce the number of system calls and
>> simplify the code for sending object IDs to pack-objects by using
>> stdio's buffering and handling errors after the loop.
>
> Good find. Thanks for doing this important cleanup.
>
> Outside of Chris's other feedback, this looks like an obviously
> correct transformation.

I spent a surprising amount of time trying to find a solution that is easy to use and allows precise error handling. But now I get second thoughts. The main selling point of buffering is better performance, which is achieved by reducing the number of system calls. How much better actually?

So I get this in my Git repo clone without this patch:
  $ strace --summary-only --trace=write git multi-pack-index repack --no-progress
  % time     seconds  usecs/call     calls    errors syscall
  ------ ----------- ----------- --------- --------- ----------------
  100.00    2.237478           2    921650           write
  ------ ----------- ----------- --------- --------- ----------------
  100.00    2.237478                921650           total
And here's the same with the patch:
  % time     seconds  usecs/call     calls    errors syscall
  ------ ----------- ----------- --------- --------- ----------------
  100.00    0.013293           2      4613           write
  ------ ----------- ----------- --------- --------- ----------------
  100.00    0.013293                  4613           total

Awesome, right? write(2) calls are down by a factor of almost 200 and the time spent on them is reduced significantly, as advertised. Let's ask hyperfine for a second opinion though. Without this patch:

  Benchmark #1: git multi-pack-index repack --no-progress
    Time (mean ± σ):      1.652 s ±  0.206 s    [User: 1.383 s, System: 0.317 s]
    Range (min … max):    1.426 s …  1.890 s    10 runs
And the same with this patch applied:
    Time (mean ± σ):      1.635 s ±  0.199 s    [User: 1.363 s, System: 0.204 s]
    Range (min … max):    1.430 s …  1.871 s    10 runs

OK, so system time is down by ca. 50%, but the total duration is basically unchanged. It seems strace added quite some overhead to our measurement above.

Anyway, now I wonder if adding our own buffer on top if the OS-internal pipe buffer is actually worth it. The numbers above are from Debian testing , by the way. Perhaps buffering still pays off on operating systems with slower pipes..

René
Derrick Stolee· Aug 11, 2020, 17:14 UTC · re: René Scharfe · lore

Re: [PATCH] midx: use buffered I/O to talk to pack-objects

On 8/11/2020 12:08 PM, René Scharfe wrote:
Show 56 quoted lines
> Am 03.08.20 um 14:39 schrieb Derrick Stolee:
>> On 8/2/2020 10:38 AM, René Scharfe wrote:
>>> Like f0bca72dc77 (send-pack: use buffered I/O to talk to pack-objects,
>>> 2016-06-08), significantly reduce the number of system calls and
>>> simplify the code for sending object IDs to pack-objects by using
>>> stdio's buffering and handling errors after the loop.
>>
>> Good find. Thanks for doing this important cleanup.
>>
>> Outside of Chris's other feedback, this looks like an obviously
>> correct transformation.
> 
> I spent a surprising amount of time trying to find a solution that is
> easy to use and allows precise error handling.  But now I get second
> thoughts.  The main selling point of buffering is better performance,
> which is achieved by reducing the number of system calls.  How much
> better actually?
> 
> So I get this in my Git repo clone without this patch:
> 
>   $ strace --summary-only --trace=write git multi-pack-index repack --no-progress
>   % time     seconds  usecs/call     calls    errors syscall
>   ------ ----------- ----------- --------- --------- ----------------
>   100.00    2.237478           2    921650           write
>   ------ ----------- ----------- --------- --------- ----------------
>   100.00    2.237478                921650           total
> 
> And here's the same with the patch:
> 
>   % time     seconds  usecs/call     calls    errors syscall
>   ------ ----------- ----------- --------- --------- ----------------
>   100.00    0.013293           2      4613           write
>   ------ ----------- ----------- --------- --------- ----------------
>   100.00    0.013293                  4613           total
> 
> Awesome, right?  write(2) calls are down by a factor of almost 200 and
> the time spent on them is reduced significantly, as advertised.  Let's
> ask hyperfine for a second opinion though.  Without this patch:
> 
>   Benchmark #1: git multi-pack-index repack --no-progress
>     Time (mean ± σ):      1.652 s ±  0.206 s    [User: 1.383 s, System: 0.317 s]
>     Range (min … max):    1.426 s …  1.890 s    10 runs
> 
> And the same with this patch applied:
> 
>     Time (mean ± σ):      1.635 s ±  0.199 s    [User: 1.363 s, System: 0.204 s]
>     Range (min … max):    1.430 s …  1.871 s    10 runs
> 
> OK, so system time is down by ca. 50%, but the total duration is
> basically unchanged.  It seems strace added quite some overhead to our
> measurement above.
> 
> Anyway, now I wonder if adding our own buffer on top if the
> OS-internal pipe buffer is actually worth it.  The numbers above are
> from Debian testing , by the way.  Perhaps buffering still pays off on
> operating systems with slower pipes..

For what it's worth, I took your patch and applied it on Git for Windows and tested 'git multi-pack-index repack' on my copy of the Git repo (which includes Git for Windows and microsoft/git for a total of 1.7 million objects) and saw the time improve from 22.3s to 16.6s!

The "Enumerating objects" progress bar was visibly faster when I was watching the command.

I was not expecting such a huge speed bump, seeing how the objects are being repacked, so this command includes complicated processes like delta compression an zlib compression.

Thanks! This is definitely worth the speed boost on Windows.
-Stolee
René Scharfe· Aug 12, 2020, 16:52 UTC · re: René Scharfe · lore

[PATCH v2] midx: use buffered I/O to talk to pack-objects

Like f0bca72dc77 (send-pack: use buffered I/O to talk to pack-objects, 2016-06-08), significantly reduce the number of system calls and simplify the code for sending object IDs to pack-objects by using stdio's buffering.

Helped-by: Chris Torek <chris.torek@gmail.com>
Helped-by: Johannes Sixt <j6t@kdbg.org>
Encouraged-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
---
Change since v1:
- Removed error handling to match the original code.
 midx.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Show changes to midx.c +5 −4
diff --git a/midx.c b/midx.c
index a5fb797edee..0f95c3d8526 100644
--- a/midx.c
+++ b/midx.c
@@ -1383,6 +1383,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 	uint32_t i;
 	unsigned char *include_pack;
 	struct child_process cmd = CHILD_PROCESS_INIT;
+	FILE *cmd_in;
 	struct strbuf base_name = STRBUF_INIT;
 	struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);

@@ -1435,6 +1436,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 		goto cleanup;
 	}

+	cmd_in = xfdopen(cmd.in, "w");
+
 	for (i = 0; i < m->num_objects; i++) {
 		struct object_id oid;
 		uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
@@ -1443,10 +1446,9 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 			continue;

 		nth_midxed_object_oid(&oid, m, i);
-		xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
-		xwrite(cmd.in, "\n", 1);
+		fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
 	}
-	close(cmd.in);
+	fclose(cmd_in);

 	if (finish_command(&cmd)) {
 		error(_("could not finish pack-objects"));
--
2.28.0
Junio C Hamano· Aug 12, 2020, 20:28 UTC · re: René Scharfe · lore

Re: [PATCH v2] midx: use buffered I/O to talk to pack-objects

René Scharfe <l.s.r@web.de> writes:
>  		nth_midxed_object_oid(&oid, m, i);
> -		xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
> -		xwrite(cmd.in, "\n", 1);
> +		fprintf(cmd_in, "%s\n", oid_to_hex(&oid));

I do think it is silly to send an object name and terminating LF in two different system calls per object.

The original uses xwrite() so that it does not have to worry about having to restart interrupted system calls and such. Do we need to do that ourselves now or does the stdio layer take care of it for us?

Show 8 quoted lines
>  	}
> -	close(cmd.in);
> +	fclose(cmd_in);
>
>  	if (finish_command(&cmd)) {
>  		error(_("could not finish pack-objects"));
> --
> 2.28.0
Junio C Hamano· Aug 12, 2020, 20:31 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] midx: use buffered I/O to talk to pack-objects

Junio C Hamano <gitster@pobox.com> writes:
Show 12 quoted lines
> René Scharfe <l.s.r@web.de> writes:
>
>>  		nth_midxed_object_oid(&oid, m, i);
>> -		xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
>> -		xwrite(cmd.in, "\n", 1);
>> +		fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
>
> I do think it is silly to send an object name and terminating LF in
> two different system calls per object.
>
> The original uses xwrite() so that it does not have to worry about
> having to restart interrupted system calls and such.

Oops. There is not much in "and such". xwrite() only restarts but the caller must deal with truncated write, which the original does not do.

Jeff King· Aug 13, 2020, 09:11 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] midx: use buffered I/O to talk to pack-objects

On Wed, Aug 12, 2020 at 01:31:58PM -0700, Junio C Hamano wrote:
Show 18 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > René Scharfe <l.s.r@web.de> writes:
> >
> >>  		nth_midxed_object_oid(&oid, m, i);
> >> -		xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
> >> -		xwrite(cmd.in, "\n", 1);
> >> +		fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
> >
> > I do think it is silly to send an object name and terminating LF in
> > two different system calls per object.
> >
> > The original uses xwrite() so that it does not have to worry about
> > having to restart interrupted system calls and such.
> 
> Oops.  There is not much in "and such".  xwrite() only restarts but
> the caller must deal with truncated write, which the original does
> not do.

Yeah, so I think the new code should be an improvement there, because it will eventually retry the write when the buffer fills, or when we eventually flush on close (my "everything written by the time we hit fflush()" in the previous mail really should have been "...by the time we hit fclose()"). Maybe another reason to use stdio. :)

The original also doesn't handle "real" errors at all (stuff besides EINTR, etc). Nor does the rewrite. I guess it's unlikely to see a write error here that doesn't also involve pack-objects returning a non-zero exit code.

-Peff
Jeff King· Aug 13, 2020, 09:06 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] midx: use buffered I/O to talk to pack-objects

On Wed, Aug 12, 2020 at 01:28:22PM -0700, Junio C Hamano wrote:
Show 14 quoted lines
> René Scharfe <l.s.r@web.de> writes:
> 
> >  		nth_midxed_object_oid(&oid, m, i);
> > -		xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
> > -		xwrite(cmd.in, "\n", 1);
> > +		fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
> 
> I do think it is silly to send an object name and terminating LF in
> two different system calls per object.
> 
> The original uses xwrite() so that it does not have to worry about
> having to restart interrupted system calls and such.  Do we need to
> do that ourselves now or does the stdio layer take care of it for
> us?

I think we're OK in this instance because we are not expecting writes to happen at any given moment. We might do a partial write() for any given fprintf(), but we're OK as long as by the fflush() at the end everything is written.

For more general cases where we do care about ordering (e.g., if we were interleaving writes and reads with something like cat-file), we'd need to fflush() after each write. And I'd expect fflush() to retry across partial write() or EINTR anyway as a quality-of-implementation thing. Which doesn't make it true, but glibc empirically seems to behave that way, and I think we'd be better off adding a transparent wrapper to fflush() on any system that doesn't.

-Peff

← back to recent threads