midx: use buffered I/O to talk to pack-objects
PATCH 1/1 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 and handling errors after the loop.
--- midx.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
midx.c +11 -3
@@ -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
Quoted 20 lines · René Scharfe
midx.c +8
-4@@ -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" )); --
Quoted 1 line
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
Am 02.08.20 um 18:11 schrieb Chris Torek:
Quoted 18 lines · René Scharfe
Quoted 17 lines
midx.c +8
-3@@ -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);
Quoted 11 lines
Quoted 4 lines
midx.c +0
-1if (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 anferror()(orfflush()) test before the finalfclose(), and then we just need to use plainerror(). 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
Am 03.08.20 um 20:10 schrieb Johannes Sixt:
Quoted 30 lines
Am 02.08.20 um 18:11 schrieb Chris Torek:
Quoted 29 lines · René Scharfe
Quoted 20 lines
midx.c +8
-4@@ -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" )); --Quoted 1 line
2.28.0
Here, we don't have any explicit errno checking, but of course
error_errno()uses errno. This too needs anferror()(orfflush()) test before the finalfclose(), and then we just need to use plainerror(). 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?
Quoted 5 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é
Am 04.08.20 um 00:27 schrieb René Scharfe:
Quoted 31 lines
Am 03.08.20 um 20:10 schrieb Johannes Sixt:
Quoted 19 lines
Am 02.08.20 um 18:11 schrieb Chris Torek:
Quoted 18 lines · René Scharfe
Quoted 17 lines
midx.c +8
-3@@ -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);Quoted 10 lines
Quoted 4 lines
Quoted 4 lines
midx.c +0
-1if (finish_command (&cmd)) {error (_ ("could not finish pack-objects" )); --2.28.0
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.
Quoted 2 lines
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é
Quoted 2 lines · René Scharfe
Somehow staying with write(2) and its friends and just adding strbuf based buffering looks attractive to me now. :-/
Indeed :-/
Quoted 4 lines · René Scharfe
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
Am 03.08.20 um 14:39 schrieb Derrick Stolee:
Quoted 10 lines · René Scharfe
Quoted 4 lines
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 runsAnd 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 runsOK, 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é
Quoted 48 lines · René Scharfe
Am 03.08.20 um 14:39 schrieb Derrick Stolee:
Quoted 10 lines · René Scharfe
Quoted 4 lines
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 totalAnd 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 totalAwesome, 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 runsAnd 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
Quoted 8 lines
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
PATCH v2 1/1 midx: use buffered I/O to talk to pack-objects
In reply to René ScharfeLike 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.
---
Change since v1:
- Removed error handling to match the original code.
midx.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
midx.c +5 -3
@@ -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
midx.c int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, · line 1449
Quoted 4 lines · René Scharfe
midx.c +1
-2nth_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?
Quoted 8 lines
midx.c +1
-2} -close (cmd.in); +fclose (cmd_in);if (finish_command (&cmd)) {error (_ ("could not finish pack-objects" )); --2.28.0
Quoted 10 lines · Junio C Hamano
midx.c int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, · line 1449
Quoted 4 lines · René Scharfe
midx.c +1
-2nth_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.
Quoted 2 lines
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.
Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Junio C Hamano
midx.c int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, · line 1449
Quoted 4 lines · René Scharfe
midx.c +1
-2nth_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.
Quoted 6 lines
Quoted 2 lines
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
Quoted 10 lines · Junio C Hamano
midx.c int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, · line 1449
Quoted 4 lines · René Scharfe
midx.c +1
-2nth_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.
Quoted 4 lines
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