threads / patch / 58768

v4, 5 partssubmodule: parallelize diff

Subject: [PATCH v4 0/5] submodule: parallelize diff

## tl;dr

26 messages between Nov 8, 2022 and Jan 17, 2023. Diffs are folded; open one to read it.

replies: 25people: 7as markdown or json

Calvin Wan· Nov 8, 2022, 18:41 UTC · lore

Original cover letter for context: https://lore.kernel.org/git/20221011232604.839941-1-calvinwan@google.com/

Changes since v3

Renamed pipe_output_fn to duplicate_output_fn and now passes an additional "strbuf* out" parameter. Output is directly duplicated to that function rather than held in a separate variable. Slightly rewrote the tests to more accurately capture the expected output of duplicate_output_fn.

Removed a patch that added an option to hide child process output. Child process output is now reset in status_duplicate_output.

More style changes as suggested by Avar
Calvin Wan (5):
  run-command: add duplicate_output_fn to run_processes_parallel_opts
  submodule: strbuf variable rename
  submodule: move status parsing into function
  diff-lib: refactor match_stat_with_submodule
  diff-lib: parallelize run_diff_files for submodules
 Documentation/config/submodule.txt |  12 ++
 diff-lib.c                         | 103 +++++++++++--
 run-command.c                      |  13 +-
 run-command.h                      |  24 +++
 submodule.c                        | 229 +++++++++++++++++++++++++----
 submodule.h                        |   9 ++
 t/helper/test-run-command.c        |  21 +++
 t/t0061-run-command.sh             |  39 +++++
 t/t4027-diff-submodule.sh          |  19 +++
 t/t7506-status-submodule.sh        |  19 +++
 10 files changed, 441 insertions(+), 47 deletions(-)
-- 
2.38.1.431.g37b22c650d-goog
Glen Choo· Nov 23, 2022, 17:49 UTC · re: Calvin Wan · lore

Re: [PATCH v4 0/5] submodule: parallelize diff

Calvin Wan <calvinwan@google.com> writes:
Show 15 quoted lines
> Original cover letter for context:
> https://lore.kernel.org/git/20221011232604.839941-1-calvinwan@google.com/
>
> Changes since v3
>
> Renamed pipe_output_fn to duplicate_output_fn and now passes an
> additional "strbuf* out" parameter. Output is directly duplicated
> to that function rather than held in a separate variable.
> Slightly rewrote the tests to more accurately capture the expected
> output of duplicate_output_fn.
>
> Removed a patch that added an option to hide child process output.
> Child process output is now reset in status_duplicate_output.
>
> More style changes as suggested by Avar
For ease of navigation, here are the previous versions:

v3: https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/ v2: https://lore.kernel.org/git/20221011232604.839941-1-calvinwan@google.com/ v1: https://lore.kernel.org/git/20220922232947.631309-1-calvinwan@google.com/

Junio C Hamano· Jan 15, 2023, 09:31 UTC · re: Calvin Wan · lore

Re: [PATCH v4 0/5] submodule: parallelize diff

Calvin Wan <calvinwan@google.com> writes:
Show 21 quoted lines
> Original cover letter for context:
> https://lore.kernel.org/git/20221011232604.839941-1-calvinwan@google.com/
> ...
> Calvin Wan (5):
>   run-command: add duplicate_output_fn to run_processes_parallel_opts
>   submodule: strbuf variable rename
>   submodule: move status parsing into function
>   diff-lib: refactor match_stat_with_submodule
>   diff-lib: parallelize run_diff_files for submodules
>
>  Documentation/config/submodule.txt |  12 ++
>  diff-lib.c                         | 103 +++++++++++--
>  run-command.c                      |  13 +-
>  run-command.h                      |  24 +++
>  submodule.c                        | 229 +++++++++++++++++++++++++----
>  submodule.h                        |   9 ++
>  t/helper/test-run-command.c        |  21 +++
>  t/t0061-run-command.sh             |  39 +++++
>  t/t4027-diff-submodule.sh          |  19 +++
>  t/t7506-status-submodule.sh        |  19 +++
>  10 files changed, 441 insertions(+), 47 deletions(-)

While the topic is marked as "Needs review" in the recent "What's cooking" reports, merging this topic also breaks the "linux-leaks" job by causing many tests fail:

    t3040-subprojects-basic.sh
    t4010-diff-pathspec.sh
    t4015-diff-whitespace.sh
    t4027-diff-submodule.sh
    t7403-submodule-sync.sh
    t7409-submodule-detached-work-tree.sh
    t7416-submodule-dash-url.sh
    t7450-bad-git-dotfiles.sh
    t7506-status-submodule.sh

Two of the test scripts are touched by this topic, and their breakage could be caused by newly using other git subcommands that were known to be leaking (iow, not because this series introduced new leaks). It also is possible that they fail because this series added new leaks to the commands these two test scripts use. In either case, other tests that haven't been touched by this topic were definitely broken by new leaks introduced by the changes made by this series.

Anybody interested should be able to see the breakage themselves by checking out 'seen' and running

    SANTIZE=leak GIT_TEST_PASSING_SANITIZE_LEAK=true \
    make test

to see the tree with all in-flight topics are clean, and then by running the same test after merging this topic to 'seen'.

Thanks.
Calvin Wan· Jan 17, 2023, 19:31 UTC · re: Junio C Hamano · lore

Re: [PATCH v4 0/5] submodule: parallelize diff

Hi Junio
I've sent out a reroll to fix this. Thanks!

Passing leaks CI at: https://github.com/calvin-wan-google/git/actions/runs/3942292098 (linux-musl technically failed, but it looks like for other reasons)

On Sun, Jan 15, 2023 at 1:31 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 58 quoted lines
>
> Calvin Wan <calvinwan@google.com> writes:
>
> > Original cover letter for context:
> > https://lore.kernel.org/git/20221011232604.839941-1-calvinwan@google.com/
> > ...
> > Calvin Wan (5):
> >   run-command: add duplicate_output_fn to run_processes_parallel_opts
> >   submodule: strbuf variable rename
> >   submodule: move status parsing into function
> >   diff-lib: refactor match_stat_with_submodule
> >   diff-lib: parallelize run_diff_files for submodules
> >
> >  Documentation/config/submodule.txt |  12 ++
> >  diff-lib.c                         | 103 +++++++++++--
> >  run-command.c                      |  13 +-
> >  run-command.h                      |  24 +++
> >  submodule.c                        | 229 +++++++++++++++++++++++++----
> >  submodule.h                        |   9 ++
> >  t/helper/test-run-command.c        |  21 +++
> >  t/t0061-run-command.sh             |  39 +++++
> >  t/t4027-diff-submodule.sh          |  19 +++
> >  t/t7506-status-submodule.sh        |  19 +++
> >  10 files changed, 441 insertions(+), 47 deletions(-)
>
> While the topic is marked as "Needs review" in the recent "What's
> cooking" reports, merging this topic also breaks the "linux-leaks"
> job by causing many tests fail:
>
>     t3040-subprojects-basic.sh
>     t4010-diff-pathspec.sh
>     t4015-diff-whitespace.sh
>     t4027-diff-submodule.sh
>     t7403-submodule-sync.sh
>     t7409-submodule-detached-work-tree.sh
>     t7416-submodule-dash-url.sh
>     t7450-bad-git-dotfiles.sh
>     t7506-status-submodule.sh
>
> Two of the test scripts are touched by this topic, and their
> breakage could be caused by newly using other git subcommands that
> were known to be leaking (iow, not because this series introduced
> new leaks). It also is possible that they fail because this series
> added new leaks to the commands these two test scripts use.  In
> either case, other tests that haven't been touched by this topic
> were definitely broken by new leaks introduced by the changes made
> by this series.
>
> Anybody interested should be able to see the breakage themselves by
> checking out 'seen' and running
>
>     SANTIZE=leak GIT_TEST_PASSING_SANITIZE_LEAK=true \
>     make test
>
> to see the tree with all in-flight topics are clean, and then by
> running the same test after merging this topic to 'seen'.
>
> Thanks.
Calvin Wan· Nov 8, 2022, 18:41 UTC · lore

[PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

Add duplicate_output_fn as an optionally set function in run_process_parallel_opts. If set, output from each child process is copied and passed to the callback function whenever output from the child process is buffered to allow for separate parsing.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 run-command.c               | 13 +++++++++++--
 run-command.h               | 24 +++++++++++++++++++++++
 t/helper/test-run-command.c | 21 ++++++++++++++++++++
 t/t0061-run-command.sh      | 39 +++++++++++++++++++++++++++++++++++++
 4 files changed, 95 insertions(+), 2 deletions(-)
Show changes to 4 files +95 −2

run-command.c, run-command.h, t/helper/test-run-command.c, t/t0061-run-command.sh

diff --git a/run-command.c b/run-command.c
index c772acd743..b8f430eb03 100644
--- a/run-command.c
+++ b/run-command.c
@@ -1560,6 +1560,9 @@ static void pp_init(struct parallel_processes *pp,
 
 	if (!opts->get_next_task)
 		BUG("you need to specify a get_next_task function");
+	
+	if (opts->duplicate_output && opts->ungroup)
+		BUG("duplicate_output and ungroup are incompatible with each other");
 
 	CALLOC_ARRAY(pp->children, n);
 	if (!opts->ungroup)
@@ -1680,8 +1683,14 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
 	for (size_t i = 0; i < opts->processes; i++) {
 		if (pp->children[i].state == GIT_CP_WORKING &&
 		    pp->pfd[i].revents & (POLLIN | POLLHUP)) {
-			int n = strbuf_read_once(&pp->children[i].err,
-						 pp->children[i].process.err, 0);
+			struct strbuf buf = STRBUF_INIT;
+			int n = strbuf_read_once(&buf, pp->children[i].process.err, 0);
+			strbuf_addbuf(&pp->children[i].err, &buf);
+			if (opts->duplicate_output)
+				opts->duplicate_output(&buf, &pp->children[i].err,
+					  opts->data,
+					  pp->children[i].data);
+			strbuf_release(&buf);
 			if (n == 0) {
 				close(pp->children[i].process.err);
 				pp->children[i].state = GIT_CP_WAIT_CLEANUP;
diff --git a/run-command.h b/run-command.h
index e3e1ea01ad..dd6d6a86c2 100644
--- a/run-command.h
+++ b/run-command.h
@@ -440,6 +440,24 @@ typedef int (*start_failure_fn)(struct strbuf *out,
 				void *pp_cb,
 				void *pp_task_cb);
 
+/**
+ * This callback is called whenever output from a child process is buffered
+ * 
+ * "struct strbuf *process_out" contains the output from the child process
+ * 
+ * See run_processes_parallel() below for a discussion of the "struct
+ * strbuf *out" parameter.
+ *
+ * pp_cb is the callback cookie as passed into run_processes_parallel,
+ * pp_task_cb is the callback cookie as passed into get_next_task_fn.
+ *
+ * This function is incompatible with "ungroup"
+ */
+typedef void (*duplicate_output_fn)(struct strbuf *process_out,
+				    struct strbuf *out,
+				    void *pp_cb,
+				    void *pp_task_cb);
+
 /**
  * This callback is called on every child process that finished processing.
  *
@@ -493,6 +511,12 @@ struct run_process_parallel_opts
 	 */
 	start_failure_fn start_failure;
 
+	/**
+	 * duplicate_output: See duplicate_output_fn() above. This should be
+	 * NULL unless process specific output is needed
+	 */
+	duplicate_output_fn duplicate_output;
+
 	/**
 	 * task_finished: See task_finished_fn() above. This can be
 	 * NULL to omit any special handling.
diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
index 3ecb830f4a..40dd329e02 100644
--- a/t/helper/test-run-command.c
+++ b/t/helper/test-run-command.c
@@ -52,6 +52,21 @@ static int no_job(struct child_process *cp,
 	return 0;
 }
 
+static void duplicate_output(struct strbuf *process_out,
+			struct strbuf *out,
+			void *pp_cb,
+			void *pp_task_cb)
+{
+	struct string_list list = STRING_LIST_INIT_DUP;
+
+	string_list_split(&list, process_out->buf, '\n', -1);
+	for (size_t i = 0; i < list.nr; i++) {
+		if (strlen(list.items[i].string) > 0)
+			fprintf(stderr, "duplicate_output: %s\n", list.items[i].string);
+	}
+	string_list_clear(&list, 0);
+}
+
 static int task_finished(int result,
 			 struct strbuf *err,
 			 void *pp_cb,
@@ -439,6 +454,12 @@ int cmd__run_command(int argc, const char **argv)
 		opts.ungroup = 1;
 	}
 
+	if (!strcmp(argv[1], "--duplicate-output")) {
+		argv += 1;
+		argc -= 1;
+		opts.duplicate_output = duplicate_output;
+	}
+
 	jobs = atoi(argv[2]);
 	strvec_clear(&proc.args);
 	strvec_pushv(&proc.args, (const char **)argv + 3);
diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh
index 7b5423eebd..130aec7c68 100755
--- a/t/t0061-run-command.sh
+++ b/t/t0061-run-command.sh
@@ -134,6 +134,15 @@ test_expect_success 'run_command runs in parallel with more jobs available than
 	test_cmp expect actual
 '
 
+test_expect_success 'run_command runs in parallel with more jobs available than tasks --duplicate-output' '
+	test-tool run-command --duplicate-output run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+	test_must_be_empty out &&
+	test 4 = $(grep -c "duplicate_output: Hello" err) &&
+	test 4 = $(grep -c "duplicate_output: World" err) &&
+	sed "/duplicate_output/d" err > err1 &&
+	test_cmp expect err1
+'
+
 test_expect_success 'run_command runs ungrouped in parallel with more jobs available than tasks' '
 	test-tool run-command --ungroup run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
 	test_line_count = 8 out &&
@@ -145,6 +154,15 @@ test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
 	test_cmp expect actual
 '
 
+test_expect_success 'run_command runs in parallel with as many jobs as tasks --duplicate-output' '
+	test-tool run-command --duplicate-output run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+	test_must_be_empty out &&
+	test 4 = $(grep -c "duplicate_output: Hello" err) &&
+	test 4 = $(grep -c "duplicate_output: World" err) &&
+	sed "/duplicate_output/d" err > err1 &&
+	test_cmp expect err1
+'
+
 test_expect_success 'run_command runs ungrouped in parallel with as many jobs as tasks' '
 	test-tool run-command --ungroup run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
 	test_line_count = 8 out &&
@@ -156,6 +174,15 @@ test_expect_success 'run_command runs in parallel with more tasks than jobs avai
 	test_cmp expect actual
 '
 
+test_expect_success 'run_command runs in parallel with more tasks than jobs available --duplicate-output' '
+	test-tool run-command --duplicate-output run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+	test_must_be_empty out &&
+	test 4 = $(grep -c "duplicate_output: Hello" err) &&
+	test 4 = $(grep -c "duplicate_output: World" err) &&
+	sed "/duplicate_output/d" err > err1 &&
+	test_cmp expect err1
+'
+
 test_expect_success 'run_command runs ungrouped in parallel with more tasks than jobs available' '
 	test-tool run-command --ungroup run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
 	test_line_count = 8 out &&
@@ -176,6 +203,12 @@ test_expect_success 'run_command is asked to abort gracefully' '
 	test_cmp expect actual
 '
 
+test_expect_success 'run_command is asked to abort gracefully --duplicate-output' '
+	test-tool run-command --duplicate-output run-command-abort 3 false >out 2>err &&
+	test_must_be_empty out &&
+	test_cmp expect err
+'
+
 test_expect_success 'run_command is asked to abort gracefully (ungroup)' '
 	test-tool run-command --ungroup run-command-abort 3 false >out 2>err &&
 	test_must_be_empty out &&
@@ -191,6 +224,12 @@ test_expect_success 'run_command outputs ' '
 	test_cmp expect actual
 '
 
+test_expect_success 'run_command outputs --duplicate-output' '
+	test-tool run-command --duplicate-output run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+	test_must_be_empty out &&
+	test_cmp expect err
+'
+
 test_expect_success 'run_command outputs (ungroup) ' '
 	test-tool run-command --ungroup run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
 	test_must_be_empty out &&
-- 
2.38.1.431.g37b22c650d-goog
Jonathan Tan· Nov 28, 2022, 20:45 UTC · re: Calvin Wan · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

Calvin Wan <calvinwan@google.com> writes:
> +	if (opts->duplicate_output && opts->ungroup)
> +		BUG("duplicate_output and ungroup are incompatible with each other");
Thanks for spotting "ungroup" - that helps me to focus my review.
Show 14 quoted lines
> @@ -1680,8 +1683,14 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
>  	for (size_t i = 0; i < opts->processes; i++) {
>  		if (pp->children[i].state == GIT_CP_WORKING &&
>  		    pp->pfd[i].revents & (POLLIN | POLLHUP)) {
> -			int n = strbuf_read_once(&pp->children[i].err,
> -						 pp->children[i].process.err, 0);
> +			struct strbuf buf = STRBUF_INIT;
> +			int n = strbuf_read_once(&buf, pp->children[i].process.err, 0);
> +			strbuf_addbuf(&pp->children[i].err, &buf);
> +			if (opts->duplicate_output)
> +				opts->duplicate_output(&buf, &pp->children[i].err,
> +					  opts->data,
> +					  pp->children[i].data);
> +			strbuf_release(&buf);
[snip]
> Add duplicate_output_fn as an optionally set function in
> run_process_parallel_opts. If set, output from each child process is
> copied and passed to the callback function whenever output from the
> child process is buffered to allow for separate parsing.

Looking at this patch, since this new option is incompatible with "ungroup", I would have expected that the new functionality be in a place that already contains an "if (ungroup)", and thus would go into the "else" block. Looking at the code, it seems like a reasonable place would be in pp_collect_finished().

Is the reason this is not there because we only want the output of the child process, not anything that the callback functions might write to the out strbuf? If yes, is there a reason for that? If not, I think the code would be simpler if we did what I suggested. (Maybe this has already been discussed previously - if that is the case, the reason for doing it this way should be in the commit message.)

Show 35 quoted lines
> diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
> index 3ecb830f4a..40dd329e02 100644
> --- a/t/helper/test-run-command.c
> +++ b/t/helper/test-run-command.c
> @@ -52,6 +52,21 @@ static int no_job(struct child_process *cp,
>  	return 0;
>  }
>  
> +static void duplicate_output(struct strbuf *process_out,
> +			struct strbuf *out,
> +			void *pp_cb,
> +			void *pp_task_cb)
> +{
> +	struct string_list list = STRING_LIST_INIT_DUP;
> +
> +	string_list_split(&list, process_out->buf, '\n', -1);
> +	for (size_t i = 0; i < list.nr; i++) {
> +		if (strlen(list.items[i].string) > 0)
> +			fprintf(stderr, "duplicate_output: %s\n", list.items[i].string);
> +	}
> +	string_list_clear(&list, 0);
> +}
> +
>  static int task_finished(int result,
>  			 struct strbuf *err,
>  			 void *pp_cb,
> @@ -439,6 +454,12 @@ int cmd__run_command(int argc, const char **argv)
>  		opts.ungroup = 1;
>  	}
>  
> +	if (!strcmp(argv[1], "--duplicate-output")) {
> +		argv += 1;
> +		argc -= 1;
> +		opts.duplicate_output = duplicate_output;
> +	}

In the tests, can we also write things from the callback functions? Whether we think that callback output should be duplicated or not, we should test what happens to them.

Calvin Wan· Nov 30, 2022, 18:46 UTC · re: Jonathan Tan · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On Mon, Nov 28, 2022 at 12:45 PM Jonathan Tan <jonathantanmy@google.com> wrote:
> Looking at this patch, since this new option is incompatible with "ungroup",
> I would have expected that the new functionality be in a place that already
> contains an "if (ungroup)", and thus would go into the "else" block. Looking at
> the code, it seems like a reasonable place would be in pp_collect_finished().

The code lives in pp_buffer_stderr(), which if you go one level higher, you'll notice that the call to pp_buffer_stderr() is in the "else" block of an "if (ungroup)".

Show 6 quoted lines
> Is the reason this is not there because we only want the output of the child
> process, not anything that the callback functions might write to the out
> strbuf? If yes, is there a reason for that? If not, I think the code would
> be simpler if we did what I suggested. (Maybe this has already been discussed
> previously - if that is the case, the reason for doing it this way should be in
> the commit message.)

Yes, inside of pp_output(), you'll see that if the process is the output_owner, then "pp->children[i].err" is printed and reset, which is why the code lives before pp_output(). The caller already has access to the callback functions and knows what will be written to the out strbuf -- the goal of this patch is to provide access to all of the child output.

Show 39 quoted lines
>
> > diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
> > index 3ecb830f4a..40dd329e02 100644
> > --- a/t/helper/test-run-command.c
> > +++ b/t/helper/test-run-command.c
> > @@ -52,6 +52,21 @@ static int no_job(struct child_process *cp,
> >       return 0;
> >  }
> >
> > +static void duplicate_output(struct strbuf *process_out,
> > +                     struct strbuf *out,
> > +                     void *pp_cb,
> > +                     void *pp_task_cb)
> > +{
> > +     struct string_list list = STRING_LIST_INIT_DUP;
> > +
> > +     string_list_split(&list, process_out->buf, '\n', -1);
> > +     for (size_t i = 0; i < list.nr; i++) {
> > +             if (strlen(list.items[i].string) > 0)
> > +                     fprintf(stderr, "duplicate_output: %s\n", list.items[i].string);
> > +     }
> > +     string_list_clear(&list, 0);
> > +}
> > +
> >  static int task_finished(int result,
> >                        struct strbuf *err,
> >                        void *pp_cb,
> > @@ -439,6 +454,12 @@ int cmd__run_command(int argc, const char **argv)
> >               opts.ungroup = 1;
> >       }
> >
> > +     if (!strcmp(argv[1], "--duplicate-output")) {
> > +             argv += 1;
> > +             argc -= 1;
> > +             opts.duplicate_output = duplicate_output;
> > +     }
>
> In the tests, can we also write things from the callback functions? Whether we think that callback output should be
> duplicated or not, we should test what happens to them.
ack.
Elijah Newren· Nov 29, 2022, 05:11 UTC · re: Calvin Wan · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On Tue, Nov 8, 2022 at 11:11 AM Calvin Wan <calvinwan@google.com> wrote:
Show 7 quoted lines
>
> Add duplicate_output_fn as an optionally set function in
> run_process_parallel_opts. If set, output from each child process is
> copied and passed to the callback function whenever output from the
> child process is buffered to allow for separate parsing.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
[...]
Show 12 quoted lines
> diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
> index 3ecb830f4a..40dd329e02 100644
> --- a/t/helper/test-run-command.c
> +++ b/t/helper/test-run-command.c
> @@ -52,6 +52,21 @@ static int no_job(struct child_process *cp,
>         return 0;
>  }
>
> +static void duplicate_output(struct strbuf *process_out,
> +                       struct strbuf *out,
> +                       void *pp_cb,
> +                       void *pp_task_cb)
Should the last two parameters have an "UNUSED" annotation?
Calvin Wan· Nov 30, 2022, 18:47 UTC · re: Elijah Newren · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On Mon, Nov 28, 2022 at 9:11 PM Elijah Newren <newren@gmail.com> wrote:
Show 24 quoted lines
>
> On Tue, Nov 8, 2022 at 11:11 AM Calvin Wan <calvinwan@google.com> wrote:
> >
> > Add duplicate_output_fn as an optionally set function in
> > run_process_parallel_opts. If set, output from each child process is
> > copied and passed to the callback function whenever output from the
> > child process is buffered to allow for separate parsing.
> >
> > Signed-off-by: Calvin Wan <calvinwan@google.com>
> [...]
> > diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
> > index 3ecb830f4a..40dd329e02 100644
> > --- a/t/helper/test-run-command.c
> > +++ b/t/helper/test-run-command.c
> > @@ -52,6 +52,21 @@ static int no_job(struct child_process *cp,
> >         return 0;
> >  }
> >
> > +static void duplicate_output(struct strbuf *process_out,
> > +                       struct strbuf *out,
> > +                       void *pp_cb,
> > +                       void *pp_task_cb)
>
> Should the last two parameters have an "UNUSED" annotation?
Yes, good catch!
Glen Choo· Nov 29, 2022, 23:29 UTC · re: Calvin Wan · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

Calvin Wan <calvinwan@google.com> writes:
Show 17 quoted lines
> @@ -1680,8 +1683,14 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
>  	for (size_t i = 0; i < opts->processes; i++) {
>  		if (pp->children[i].state == GIT_CP_WORKING &&
>  		    pp->pfd[i].revents & (POLLIN | POLLHUP)) {
> -			int n = strbuf_read_once(&pp->children[i].err,
> -						 pp->children[i].process.err, 0);
> +			struct strbuf buf = STRBUF_INIT;
> +			int n = strbuf_read_once(&buf, pp->children[i].process.err, 0);
> +			strbuf_addbuf(&pp->children[i].err, &buf);
> +			if (opts->duplicate_output)
> +				opts->duplicate_output(&buf, &pp->children[i].err,
> +					  opts->data,
> +					  pp->children[i].data);
> +			strbuf_release(&buf);
>  			if (n == 0) {
>  				close(pp->children[i].process.err);
>  				pp->children[i].state = GIT_CP_WAIT_CLEANUP;

A common pattern is that optional behavior does not impose additional costs on the non-optional part. Here, we unconditionally do a strbuf_addbuf() even though we don't use the result in the "else" case.

So this might be more idiomatically written as:
        int n = strbuf_read_once(&pp->children[i].err,
        			 pp->children[i].process.err, 0);
 +      if (opts->duplicate_output) {
 +          struct strbuf buf = STRBUF_INIT;
 +          strbuf_addbuf(&buf, &pp->children[i].err);
 +        	opts->duplicate_output(&buf, &pp->children[i].err,
 +        		  opts->data,
 +        		  pp->children[i].data);
 +          strbuf_release(&buf);
 +      }

which also has the nice benefit of not touching the strbuf_read_once() line.

Ævar Arnfjörð Bjarmason· Nov 30, 2022, 09:53 UTC · re: Glen Choo · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On Tue, Nov 29 2022, Glen Choo wrote:
Show 39 quoted lines
> Calvin Wan <calvinwan@google.com> writes:
>
>> @@ -1680,8 +1683,14 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
>>  	for (size_t i = 0; i < opts->processes; i++) {
>>  		if (pp->children[i].state == GIT_CP_WORKING &&
>>  		    pp->pfd[i].revents & (POLLIN | POLLHUP)) {
>> -			int n = strbuf_read_once(&pp->children[i].err,
>> -						 pp->children[i].process.err, 0);
>> +			struct strbuf buf = STRBUF_INIT;
>> +			int n = strbuf_read_once(&buf, pp->children[i].process.err, 0);
>> +			strbuf_addbuf(&pp->children[i].err, &buf);
>> +			if (opts->duplicate_output)
>> +				opts->duplicate_output(&buf, &pp->children[i].err,
>> +					  opts->data,
>> +					  pp->children[i].data);
>> +			strbuf_release(&buf);
>>  			if (n == 0) {
>>  				close(pp->children[i].process.err);
>>  				pp->children[i].state = GIT_CP_WAIT_CLEANUP;
>
> A common pattern is that optional behavior does not impose additional
> costs on the non-optional part. Here, we unconditionally do a
> strbuf_addbuf() even though we don't use the result in the "else" case.
>
> So this might be more idiomatically written as:
>
>         int n = strbuf_read_once(&pp->children[i].err,
>         			 pp->children[i].process.err, 0);
>  +      if (opts->duplicate_output) {
>  +          struct strbuf buf = STRBUF_INIT;
>  +          strbuf_addbuf(&buf, &pp->children[i].err);
>  +        	opts->duplicate_output(&buf, &pp->children[i].err,
>  +        		  opts->data,
>  +        		  pp->children[i].data);
>  +          strbuf_release(&buf);
>  +      }
>
> which also has the nice benefit of not touching the strbuf_read_once()
> line.

We should also use "size_t n" there, not "int n", which is what it returns. It won't matter for overflow in this case, but let's not truncate for no good reason, it makes spotting actual overflows in compiler output harder.

And why does "&buf" exist at all? Why can't we just pass &pp->children[i].err, and if this callback cares about the last thing we read let's pass it an offset, so it can know what came from the strbuf_read_once() (I don't know if it actually cares about that either...).

That would avoid the copy entirely.
Phillip Wood· Nov 30, 2022, 10:26 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On 30/11/2022 09:53, Ævar Arnfjörð Bjarmason wrote:
Show 15 quoted lines
> 
> On Tue, Nov 29 2022, Glen Choo wrote:
> 
>> Calvin Wan <calvinwan@google.com> writes:
>>
>>> @@ -1680,8 +1683,14 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
>>>   	for (size_t i = 0; i < opts->processes; i++) {
>>>   		if (pp->children[i].state == GIT_CP_WORKING &&
>>>   		    pp->pfd[i].revents & (POLLIN | POLLHUP)) {
>>> -			int n = strbuf_read_once(&pp->children[i].err,
>>> -						 pp->children[i].process.err, 0);
>>> +			struct strbuf buf = STRBUF_INIT;
>>> +			int n = strbuf_read_once(&buf, pp->children[i].process.err, 0);
>>> +			strbuf_addbuf(&pp->children[i].err, &buf);
>>> +			if (opts->duplicate_output)
Shouldn't we be checking if n < 0 before we do this?
Show 27 quoted lines
>>> +				opts->duplicate_output(&buf, &pp->children[i].err,
>>> +					  opts->data,
>>> +					  pp->children[i].data);
>>> +			strbuf_release(&buf);
>>>   			if (n == 0) {
>>>   				close(pp->children[i].process.err);
>>>   				pp->children[i].state = GIT_CP_WAIT_CLEANUP;
>>
>> A common pattern is that optional behavior does not impose additional
>> costs on the non-optional part. Here, we unconditionally do a
>> strbuf_addbuf() even though we don't use the result in the "else" case.
>>
>> So this might be more idiomatically written as:
>>
>>          int n = strbuf_read_once(&pp->children[i].err,
>>          			 pp->children[i].process.err, 0);
>>   +      if (opts->duplicate_output) {
>>   +          struct strbuf buf = STRBUF_INIT;
>>   +          strbuf_addbuf(&buf, &pp->children[i].err);
>>   +        	opts->duplicate_output(&buf, &pp->children[i].err,
>>   +        		  opts->data,
>>   +        		  pp->children[i].data);
>>   +          strbuf_release(&buf);
>>   +      }
>>
> [...]
> And why does "&buf" exist at all? 
I was wondering that as too
Show 5 quoted lines
>Why can't we just pass
> &pp->children[i].err, and if this callback cares about the last thing we
> read let's pass it an offset, so it can know what came from the
> strbuf_read_once() (I don't know if it actually cares about that
> either...).
Or we could just pass a `const char*`, `size_t` pair.
> That would avoid the copy entirely.

Is the copying quadratic at the moment? - it looks like each call to strbuf_read_once() appends to the buffer and we copy the whole buffer each time.

Best Wishes
Phillip
Calvin Wan· Nov 30, 2022, 19:02 UTC · re: Phillip Wood · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On Wed, Nov 30, 2022 at 2:26 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 19 quoted lines
>
> On 30/11/2022 09:53, Ævar Arnfjörð Bjarmason wrote:
> >
> > On Tue, Nov 29 2022, Glen Choo wrote:
> >
> >> Calvin Wan <calvinwan@google.com> writes:
> >>
> >>> @@ -1680,8 +1683,14 @@ static void pp_buffer_stderr(struct parallel_processes *pp,
> >>>     for (size_t i = 0; i < opts->processes; i++) {
> >>>             if (pp->children[i].state == GIT_CP_WORKING &&
> >>>                 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
> >>> -                   int n = strbuf_read_once(&pp->children[i].err,
> >>> -                                            pp->children[i].process.err, 0);
> >>> +                   struct strbuf buf = STRBUF_INIT;
> >>> +                   int n = strbuf_read_once(&buf, pp->children[i].process.err, 0);
> >>> +                   strbuf_addbuf(&pp->children[i].err, &buf);
> >>> +                   if (opts->duplicate_output)
>
> Shouldn't we be checking if n < 0 before we do this?
Yes we should.
Show 38 quoted lines
>
> >>> +                           opts->duplicate_output(&buf, &pp->children[i].err,
> >>> +                                     opts->data,
> >>> +                                     pp->children[i].data);
> >>> +                   strbuf_release(&buf);
> >>>                     if (n == 0) {
> >>>                             close(pp->children[i].process.err);
> >>>                             pp->children[i].state = GIT_CP_WAIT_CLEANUP;
> >>
> >> A common pattern is that optional behavior does not impose additional
> >> costs on the non-optional part. Here, we unconditionally do a
> >> strbuf_addbuf() even though we don't use the result in the "else" case.
> >>
> >> So this might be more idiomatically written as:
> >>
> >>          int n = strbuf_read_once(&pp->children[i].err,
> >>                               pp->children[i].process.err, 0);
> >>   +      if (opts->duplicate_output) {
> >>   +          struct strbuf buf = STRBUF_INIT;
> >>   +          strbuf_addbuf(&buf, &pp->children[i].err);
> >>   +          opts->duplicate_output(&buf, &pp->children[i].err,
> >>   +                    opts->data,
> >>   +                    pp->children[i].data);
> >>   +          strbuf_release(&buf);
> >>   +      }
> >>
> > [...]
> > And why does "&buf" exist at all?
>
> I was wondering that as too
>
> >Why can't we just pass
> > &pp->children[i].err, and if this callback cares about the last thing we
> > read let's pass it an offset, so it can know what came from the
> > strbuf_read_once() (I don't know if it actually cares about that
> > either...).
>
> Or we could just pass a `const char*`, `size_t` pair.

Both you and Avar are correct that &buf isn't necessary. I think the offset idea works better so the function calls stay similar.

Show 7 quoted lines
>
> > That would avoid the copy entirely.
>
> Is the copying quadratic at the moment? - it looks like each call to
> strbuf_read_once() appends to the buffer and we copy the whole buffer
> each time.
>

Depends on how you're defining it, but doesn't really matter since it's going away anyways ;)

Phillip Wood· Nov 30, 2022, 10:28 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On 30/11/2022 09:53, Ævar Arnfjörð Bjarmason wrote:
Show 22 quoted lines
> 
> On Tue, Nov 29 2022, Glen Choo wrote:
> 
>> Calvin Wan <calvinwan@google.com> writes:
>> So this might be more idiomatically written as:
>>
>>          int n = strbuf_read_once(&pp->children[i].err,
>>          			 pp->children[i].process.err, 0);
>>   +      if (opts->duplicate_output) {
>>   +          struct strbuf buf = STRBUF_INIT;
>>   +          strbuf_addbuf(&buf, &pp->children[i].err);
>>   +        	opts->duplicate_output(&buf, &pp->children[i].err,
>>   +        		  opts->data,
>>   +        		  pp->children[i].data);
>>   +          strbuf_release(&buf);
>>   +      }
>>
>> which also has the nice benefit of not touching the strbuf_read_once()
>> line.
> 
> We should also use "size_t n" there, not "int n", which is what it
> returns.

It returns an ssize_t not size_t, lower down we test `n < 0` so we certainly should not be using an unsigned type.

Best Wishes
Phillip
Ævar Arnfjörð Bjarmason· Nov 30, 2022, 10:57 UTC · re: Phillip Wood · lore

Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts

On Wed, Nov 30 2022, Phillip Wood wrote:
Show 24 quoted lines
> On 30/11/2022 09:53, Ævar Arnfjörð Bjarmason wrote:
>> On Tue, Nov 29 2022, Glen Choo wrote:
>> 
>>> Calvin Wan <calvinwan@google.com> writes:
>>> So this might be more idiomatically written as:
>>>
>>>          int n = strbuf_read_once(&pp->children[i].err,
>>>          			 pp->children[i].process.err, 0);
>>>   +      if (opts->duplicate_output) {
>>>   +          struct strbuf buf = STRBUF_INIT;
>>>   +          strbuf_addbuf(&buf, &pp->children[i].err);
>>>   +        	opts->duplicate_output(&buf, &pp->children[i].err,
>>>   +        		  opts->data,
>>>   +        		  pp->children[i].data);
>>>   +          strbuf_release(&buf);
>>>   +      }
>>>
>>> which also has the nice benefit of not touching the strbuf_read_once()
>>> line.
>> We should also use "size_t n" there, not "int n", which is what it
>> returns.
>
> It returns an ssize_t not size_t, lower down we test `n < 0` so we
> certainly should not be using an unsigned type.

Good catch, I just skimmed it and missed the extra "s". In any case: let's use the type it's returning, so ssize_t, not int.

(DEVOPTS=extra-all etc. will also catch this, depending on your compiler etc.)

Calvin Wan· Nov 8, 2022, 18:41 UTC · lore

[PATCH v4 2/5] submodule: strbuf variable rename

A prepatory change for a future patch that moves the status parsing logic to a separate function.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 submodule.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
Show changes to submodule.c +13 −10
diff --git a/submodule.c b/submodule.c
index b958162d28..31ee53bd57 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1900,25 +1900,28 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 
 	fp = xfdopen(cp.out, "r");
 	while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
+		char *str = buf.buf;
+		const size_t len = buf.len;
+
 		/* regular untracked files */
-		if (buf.buf[0] == '?')
+		if (str[0] == '?')
 			dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
 
-		if (buf.buf[0] == 'u' ||
-		    buf.buf[0] == '1' ||
-		    buf.buf[0] == '2') {
+		if (str[0] == 'u' ||
+		    str[0] == '1' ||
+		    str[0] == '2') {
 			/* T = line type, XY = status, SSSS = submodule state */
-			if (buf.len < strlen("T XY SSSS"))
+			if (len < strlen("T XY SSSS"))
 				BUG("invalid status --porcelain=2 line %s",
-				    buf.buf);
+				    str);
 
-			if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
+			if (str[5] == 'S' && str[8] == 'U')
 				/* nested untracked file */
 				dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
 
-			if (buf.buf[0] == 'u' ||
-			    buf.buf[0] == '2' ||
-			    memcmp(buf.buf + 5, "S..U", 4))
+			if (str[0] == 'u' ||
+			    str[0] == '2' ||
+			    memcmp(str + 5, "S..U", 4))
 				/* other change */
 				dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
 		}
-- 
2.38.1.431.g37b22c650d-goog
Calvin Wan· Nov 8, 2022, 18:41 UTC · lore

[PATCH v4 3/5] submodule: move status parsing into function

A future patch requires the ability to parse the output of git status --porcelain=2. Move parsing code from is_submodule_modified to parse_status_porcelain.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 submodule.c | 74 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
Show changes to submodule.c +42 −32
diff --git a/submodule.c b/submodule.c
index 31ee53bd57..fd3385620c 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1864,6 +1864,45 @@ int fetch_submodules(struct repository *r,
 	return spf.result;
 }
 
+static int parse_status_porcelain(char *str, size_t len,
+				  unsigned *dirty_submodule,
+				  int ignore_untracked)
+{
+	/* regular untracked files */
+	if (str[0] == '?')
+		*dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
+
+	if (str[0] == 'u' ||
+	    str[0] == '1' ||
+	    str[0] == '2') {
+		/* T = line type, XY = status, SSSS = submodule state */
+		if (len < strlen("T XY SSSS"))
+			BUG("invalid status --porcelain=2 line %s",
+			    str);
+
+		if (str[5] == 'S' && str[8] == 'U')
+			/* nested untracked file */
+			*dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
+
+		if (str[0] == 'u' ||
+		    str[0] == '2' ||
+		    memcmp(str + 5, "S..U", 4))
+			/* other change */
+			*dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
+	}
+
+	if ((*dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
+	    ((*dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
+	     ignore_untracked)) {
+		/*
+		* We're not interested in any further information from
+		* the child any more, neither output nor its exit code.
+		*/
+		return 1;
+	}
+	return 0;
+}
+
 unsigned is_submodule_modified(const char *path, int ignore_untracked)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -1903,39 +1942,10 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 		char *str = buf.buf;
 		const size_t len = buf.len;
 
-		/* regular untracked files */
-		if (str[0] == '?')
-			dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
-
-		if (str[0] == 'u' ||
-		    str[0] == '1' ||
-		    str[0] == '2') {
-			/* T = line type, XY = status, SSSS = submodule state */
-			if (len < strlen("T XY SSSS"))
-				BUG("invalid status --porcelain=2 line %s",
-				    str);
-
-			if (str[5] == 'S' && str[8] == 'U')
-				/* nested untracked file */
-				dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
-
-			if (str[0] == 'u' ||
-			    str[0] == '2' ||
-			    memcmp(str + 5, "S..U", 4))
-				/* other change */
-				dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
-		}
-
-		if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
-		    ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
-		     ignore_untracked)) {
-			/*
-			 * We're not interested in any further information from
-			 * the child any more, neither output nor its exit code.
-			 */
-			ignore_cp_exit_code = 1;
+		ignore_cp_exit_code = parse_status_porcelain(str, len, &dirty_submodule,
+							     ignore_untracked);
+		if (ignore_cp_exit_code)
 			break;
-		}
 	}
 	fclose(fp);
 
-- 
2.38.1.431.g37b22c650d-goog
Calvin Wan· Nov 8, 2022, 18:41 UTC · lore

[PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule

Flatten out the if statements in match_stat_with_submodule so the logic is more readable and easier for future patches to add to. orig_flags didn't need to be set if the cache entry wasn't a GITLINK so defer setting it.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 diff-lib.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
Show changes to diff-lib.c +18 −11
diff --git a/diff-lib.c b/diff-lib.c
index 2edea41a23..f5257c0c71 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -73,18 +73,25 @@ static int match_stat_with_submodule(struct diff_options *diffopt,
 				     unsigned *dirty_submodule)
 {
 	int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
-	if (S_ISGITLINK(ce->ce_mode)) {
-		struct diff_flags orig_flags = diffopt->flags;
-		if (!diffopt->flags.override_submodule_config)
-			set_diffopt_flags_from_submodule_config(diffopt, ce->name);
-		if (diffopt->flags.ignore_submodules)
-			changed = 0;
-		else if (!diffopt->flags.ignore_dirty_submodules &&
-			 (!changed || diffopt->flags.dirty_submodules))
-			*dirty_submodule = is_submodule_modified(ce->name,
-								 diffopt->flags.ignore_untracked_in_submodules);
-		diffopt->flags = orig_flags;
+	struct diff_flags orig_flags;
+
+	if (!S_ISGITLINK(ce->ce_mode))
+		goto ret;
+
+	orig_flags = diffopt->flags;
+	if (!diffopt->flags.override_submodule_config)
+		set_diffopt_flags_from_submodule_config(diffopt, ce->name);
+	if (diffopt->flags.ignore_submodules) {
+		changed = 0;
+		goto cleanup;
 	}
+	if (!diffopt->flags.ignore_dirty_submodules &&
+	    (!changed || diffopt->flags.dirty_submodules))
+		*dirty_submodule = is_submodule_modified(ce->name,
+					 diffopt->flags.ignore_untracked_in_submodules);
+cleanup:
+	diffopt->flags = orig_flags;
+ret:
 	return changed;
 }
 
-- 
2.38.1.431.g37b22c650d-goog
Phillip Wood· Nov 30, 2022, 14:36 UTC · re: Calvin Wan · lore

Re: [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule

Hi Calven
On 08/11/2022 18:41, Calvin Wan wrote:
> Flatten out the if statements in match_stat_with_submodule so the
> logic is more readable and easier for future patches to add to.
> orig_flags didn't need to be set if the cache entry wasn't a
> GITLINK so defer setting it.

Thanks for splitting this change out. I have to say I find the original quite a bit easier to read. If you're worried about the code added in the next commit being too indented perhaps you could move the body of the if statement into a separate function.

Best Wishes
Phillip
Show 46 quoted lines
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>   diff-lib.c | 29 ++++++++++++++++++-----------
>   1 file changed, 18 insertions(+), 11 deletions(-)
> 
> diff --git a/diff-lib.c b/diff-lib.c
> index 2edea41a23..f5257c0c71 100644
> --- a/diff-lib.c
> +++ b/diff-lib.c
> @@ -73,18 +73,25 @@ static int match_stat_with_submodule(struct diff_options *diffopt,
>   				     unsigned *dirty_submodule)
>   {
>   	int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
> -	if (S_ISGITLINK(ce->ce_mode)) {
> -		struct diff_flags orig_flags = diffopt->flags;
> -		if (!diffopt->flags.override_submodule_config)
> -			set_diffopt_flags_from_submodule_config(diffopt, ce->name);
> -		if (diffopt->flags.ignore_submodules)
> -			changed = 0;
> -		else if (!diffopt->flags.ignore_dirty_submodules &&
> -			 (!changed || diffopt->flags.dirty_submodules))
> -			*dirty_submodule = is_submodule_modified(ce->name,
> -								 diffopt->flags.ignore_untracked_in_submodules);
> -		diffopt->flags = orig_flags;
> +	struct diff_flags orig_flags;
> +
> +	if (!S_ISGITLINK(ce->ce_mode))
> +		goto ret;
> +
> +	orig_flags = diffopt->flags;
> +	if (!diffopt->flags.override_submodule_config)
> +		set_diffopt_flags_from_submodule_config(diffopt, ce->name);
> +	if (diffopt->flags.ignore_submodules) {
> +		changed = 0;
> +		goto cleanup;
>   	}
> +	if (!diffopt->flags.ignore_dirty_submodules &&
> +	    (!changed || diffopt->flags.dirty_submodules))
> +		*dirty_submodule = is_submodule_modified(ce->name,
> +					 diffopt->flags.ignore_untracked_in_submodules);
> +cleanup:
> +	diffopt->flags = orig_flags;
> +ret:
>   	return changed;
>   }
>   
Calvin Wan· Nov 30, 2022, 19:08 UTC · re: Phillip Wood · lore

Re: [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule

> Thanks for splitting this change out. I have to say I find the original
> quite a bit easier to read. If you're worried about the code added in
> the next commit being too indented perhaps you could move the body of
> the if statement into a separate function.

Then we're just swapping if statement depth for function call depth, which seems even worse. I think the confusion comes from adding the "ret:" part which is currently unnecessary so I can get rid of that this patch.

Thanks
Calvin Wan· Nov 8, 2022, 18:42 UTC · lore

[PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules

During the iteration of the index entries in run_diff_files, whenever a submodule is found and needs its status checked, a subprocess is spawned for it. Instead of spawning the subprocess immediately and waiting for its completion to continue, hold onto all submodules and relevant information in a list. Then use that list to create tasks for run_processes_parallel. Subprocess output is duplicated and passed to status_pipe_output which parses it.

Add config option submodule.diffJobs to set the maximum number of parallel jobs. The option defaults to 1 if unset. If set to 0, the number of jobs is set to online_cpus().

Since run_diff_files is called from many different commands, I chose to grab the config option in the function rather than adding variables to every git command and then figuring out how to pass them all in.

Signed-off-by: Calvin Wan <calvinwan@google.com>
---
 Documentation/config/submodule.txt |  12 +++
 diff-lib.c                         |  80 +++++++++++++--
 submodule.c                        | 154 +++++++++++++++++++++++++++++
 submodule.h                        |   9 ++
 t/t4027-diff-submodule.sh          |  19 ++++
 t/t7506-status-submodule.sh        |  19 ++++
 6 files changed, 287 insertions(+), 6 deletions(-)
Show changes to 6 files +287 −6

Documentation/config/submodule.txt, diff-lib.c, submodule.c, submodule.h, t/t4027-diff-submodule.sh, t/t7506-status-submodule.sh

diff --git a/Documentation/config/submodule.txt b/Documentation/config/submodule.txt
index 6490527b45..1144a5ad74 100644
--- a/Documentation/config/submodule.txt
+++ b/Documentation/config/submodule.txt
@@ -93,6 +93,18 @@ submodule.fetchJobs::
 	in parallel. A value of 0 will give some reasonable default.
 	If unset, it defaults to 1.
 
+submodule.diffJobs::
+	Specifies how many submodules are diffed at the same time. A
+	positive integer allows up to that number of submodules diffed
+	in parallel. A value of 0 will give the number of logical cores.
+	If unset, it defaults to 1. The diff operation is used by many
+	other git commands such as add, merge, diff, status, stash and
+	more. Note that the expensive part of the diff operation is
+	reading the index from cache or memory. Therefore multiple jobs
+	may be detrimental to performance if your hardware does not
+	support parallel reads or if the number of jobs greatly exceeds
+	the amount of supported reads.
+
 submodule.alternateLocation::
 	Specifies how the submodules obtain alternates when submodules are
 	cloned. Possible values are `no`, `superproject`.
diff --git a/diff-lib.c b/diff-lib.c
index f5257c0c71..30a3d9a2b5 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -14,6 +14,7 @@
 #include "dir.h"
 #include "fsmonitor.h"
 #include "commit-reach.h"
+#include "config.h"
 
 /*
  * diff-files
@@ -65,15 +66,20 @@ static int check_removed(const struct index_state *istate, const struct cache_en
  * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
  * option is set, the caller does not only want to know if a submodule is
  * modified at all but wants to know all the conditions that are met (new
- * commits, untracked content and/or modified content).
+ * commits, untracked content and/or modified content). If
+ * defer_submodule_status bit is set, dirty_submodule will be left to the
+ * caller to set. defer_submodule_status can also be set to 0 in this
+ * function if there is no need to check if the submodule is modified.
  */
 static int match_stat_with_submodule(struct diff_options *diffopt,
 				     const struct cache_entry *ce,
 				     struct stat *st, unsigned ce_option,
-				     unsigned *dirty_submodule)
+				     unsigned *dirty_submodule, int *defer_submodule_status,
+				     unsigned *ignore_untracked)
 {
 	int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option);
 	struct diff_flags orig_flags;
+	int defer = 0;
 
 	if (!S_ISGITLINK(ce->ce_mode))
 		goto ret;
@@ -86,12 +92,20 @@ static int match_stat_with_submodule(struct diff_options *diffopt,
 		goto cleanup;
 	}
 	if (!diffopt->flags.ignore_dirty_submodules &&
-	    (!changed || diffopt->flags.dirty_submodules))
-		*dirty_submodule = is_submodule_modified(ce->name,
+	    (!changed || diffopt->flags.dirty_submodules)) {
+		if (defer_submodule_status && *defer_submodule_status) {
+			defer = 1;
+			*ignore_untracked = diffopt->flags.ignore_untracked_in_submodules;
+		} else {
+			*dirty_submodule = is_submodule_modified(ce->name,
 					 diffopt->flags.ignore_untracked_in_submodules);
+		}
+	}
 cleanup:
 	diffopt->flags = orig_flags;
 ret:
+	if (defer_submodule_status)
+		*defer_submodule_status = defer;
 	return changed;
 }
 
@@ -103,6 +117,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 			      ? CE_MATCH_RACY_IS_DIRTY : 0);
 	uint64_t start = getnanotime();
 	struct index_state *istate = revs->diffopt.repo->index;
+	struct string_list submodules = STRING_LIST_INIT_NODUP;
 
 	diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");
 
@@ -227,6 +242,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 			newmode = ce->ce_mode;
 		} else {
 			struct stat st;
+			unsigned ignore_untracked = 0;
+			int defer_submodule_status = !!revs->repo;
 
 			changed = check_removed(istate, ce, &st);
 			if (changed) {
@@ -248,8 +265,25 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 			}
 
 			changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
-							    ce_option, &dirty_submodule);
+							    ce_option, &dirty_submodule,
+							    &defer_submodule_status,
+							    &ignore_untracked);
 			newmode = ce_mode_from_stat(ce, st.st_mode);
+			if (defer_submodule_status) {
+				struct submodule_status_util tmp = {
+					.changed = changed,
+					.dirty_submodule = 0,
+					.ignore_untracked = ignore_untracked,
+					.newmode = newmode,
+					.ce = ce,
+				};
+				struct string_list_item *item;
+
+				item = string_list_append(&submodules, ce->name);
+				item->util = xmalloc(sizeof(tmp));
+				memcpy(item->util, &tmp, sizeof(tmp));
+				continue;
+			}
 		}
 
 		if (!changed && !dirty_submodule) {
@@ -268,6 +302,40 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 			    ce->name, 0, dirty_submodule);
 
 	}
+	if (submodules.nr > 0) {
+		int parallel_jobs;
+		if (git_config_get_int("submodule.diffjobs", &parallel_jobs))
+			parallel_jobs = 1;
+		else if (!parallel_jobs)
+			parallel_jobs = online_cpus();
+		else if (parallel_jobs < 0)
+			die(_("submodule.diffjobs cannot be negative"));
+
+		if (get_submodules_status(revs->repo, &submodules, parallel_jobs))
+			die(_("submodule status failed"));
+		for (size_t i = 0; i < submodules.nr; i++) {
+			struct submodule_status_util *util = submodules.items[i].util;
+			struct cache_entry *ce = util->ce;
+			unsigned int oldmode;
+			const struct object_id *old_oid, *new_oid;
+
+			if (!util->changed && !util->dirty_submodule) {
+				ce_mark_uptodate(ce);
+				mark_fsmonitor_valid(istate, ce);
+				if (!revs->diffopt.flags.find_copies_harder)
+					continue;
+			}
+			oldmode = ce->ce_mode;
+			old_oid = &ce->oid;
+			new_oid = util->changed ? null_oid() : &ce->oid;
+			diff_change(&revs->diffopt, oldmode, util->newmode,
+				    old_oid, new_oid,
+				    !is_null_oid(old_oid),
+				    !is_null_oid(new_oid),
+				    ce->name, 0, util->dirty_submodule);
+		}
+	}
+	string_list_clear(&submodules, 1);
 	diffcore_std(&revs->diffopt);
 	diff_flush(&revs->diffopt);
 	trace_performance_since(start, "diff-files");
@@ -315,7 +383,7 @@ static int get_stat_data(const struct index_state *istate,
 			return -1;
 		}
 		changed = match_stat_with_submodule(diffopt, ce, &st,
-						    0, dirty_submodule);
+						    0, dirty_submodule, NULL, NULL);
 		if (changed) {
 			mode = ce_mode_from_stat(ce, st.st_mode);
 			oid = null_oid();
diff --git a/submodule.c b/submodule.c
index fd3385620c..763a05d523 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1363,6 +1363,18 @@ int submodule_touches_in_range(struct repository *r,
 	return ret;
 }
 
+struct submodule_parallel_status {
+	size_t index_count;
+	int result;
+
+	struct string_list *submodule_names;
+	struct repository *r;
+
+	/* Pending statuses by OIDs */
+	struct status_task **oid_status_tasks;
+	int oid_status_tasks_nr, oid_status_tasks_alloc;
+};
+
 struct submodule_parallel_fetch {
 	/*
 	 * The index of the last index entry processed by
@@ -1445,6 +1457,12 @@ struct fetch_task {
 	struct oid_array *commits; /* Ensure these commits are fetched */
 };
 
+struct status_task {
+	const char *path;
+	unsigned dirty_submodule;
+	int ignore_untracked;
+};
+
 /**
  * When a submodule is not defined in .gitmodules, we cannot access it
  * via the regular submodule-config. Create a fake submodule, which we can
@@ -1956,6 +1974,142 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 	return dirty_submodule;
 }
 
+static struct status_task *
+get_status_task_from_index(struct submodule_parallel_status *sps,
+			   struct strbuf *err)
+{
+	for (; sps->index_count < sps->submodule_names->nr; sps->index_count++) {
+		struct submodule_status_util *util = sps->submodule_names->items[sps->index_count].util;
+		const struct cache_entry *ce = util->ce;
+		struct status_task *task;
+		struct status_task tmp = {
+			.path = ce->name,
+			.dirty_submodule = util->dirty_submodule,
+			.ignore_untracked = util->ignore_untracked,
+		};
+		struct strbuf buf = STRBUF_INIT;
+		const char *git_dir;
+
+		strbuf_addf(&buf, "%s/.git", ce->name);
+		git_dir = read_gitfile(buf.buf);
+		if (!git_dir)
+			git_dir = buf.buf;
+		if (!is_git_directory(git_dir)) {
+			if (is_directory(git_dir))
+				die(_("'%s' not recognized as a git repository"), git_dir);
+			strbuf_release(&buf);
+			/* The submodule is not checked out, so it is not modified */
+			util->dirty_submodule = 0;
+			continue;
+		}
+		strbuf_release(&buf);
+
+		task = xmalloc(sizeof(*task));
+		memcpy(task, &tmp, sizeof(*task));
+		sps->index_count++;
+		return task;
+	}
+	return NULL;
+}
+
+
+static int get_next_submodule_status(struct child_process *cp,
+				     struct strbuf *err, void *data,
+				     void **task_cb)
+{
+	struct submodule_parallel_status *sps = data;
+	struct status_task *task = get_status_task_from_index(sps, err);
+
+	if (!task)
+		return 0;
+
+	child_process_init(cp);
+	prepare_submodule_repo_env_in_gitdir(&cp->env);
+
+	strvec_init(&cp->args);
+	strvec_pushl(&cp->args, "status", "--porcelain=2", NULL);
+	if (task->ignore_untracked)
+		strvec_push(&cp->args, "-uno");
+
+	prepare_submodule_repo_env(&cp->env);
+	cp->git_cmd = 1;
+	cp->dir = task->path;
+	*task_cb = task;
+	return 1;
+}
+
+static int status_start_failure(struct strbuf *err,
+			        void *cb, void *task_cb)
+{
+	struct submodule_parallel_status *sps = cb;
+
+	sps->result = 1;
+	return 0;
+}
+
+static void status_duplicate_output(struct strbuf *process_out,
+				    struct strbuf *out,
+				    void *cb, void *task_cb)
+{
+	struct status_task *task = task_cb;
+	struct string_list list = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+
+	string_list_split(&list, process_out->buf, '\n', -1);
+
+	for_each_string_list_item(item, &list) {
+		if (parse_status_porcelain(item->string,
+				   strlen(item->string),
+				   &task->dirty_submodule,
+				   task->ignore_untracked))
+			break;
+	}
+	string_list_clear(&list, 0);
+	strbuf_reset(out);
+}
+
+static int status_finish(int retvalue, struct strbuf *err,
+			 void *cb, void *task_cb)
+{
+	struct submodule_parallel_status *sps = cb;
+	struct status_task *task = task_cb;
+	struct string_list_item *it =
+		string_list_lookup(sps->submodule_names, task->path);
+	struct submodule_status_util *util = it->util;
+
+	util->dirty_submodule = task->dirty_submodule;
+	free(task);
+
+	return 0;
+}
+
+int get_submodules_status(struct repository *r,
+			  struct string_list *submodules,
+			  int max_parallel_jobs)
+{
+	struct submodule_parallel_status sps = {
+                .r = r,
+                .submodule_names = submodules,
+        };
+	const struct run_process_parallel_opts opts = {
+		.tr2_category = "submodule",
+		.tr2_label = "parallel/status",
+
+		.processes = max_parallel_jobs,
+
+		.get_next_task = get_next_submodule_status,
+		.start_failure = status_start_failure,
+		.duplicate_output = status_duplicate_output,
+		.task_finished = status_finish,
+		.data = &sps,
+	};
+
+	string_list_sort(sps.submodule_names);
+	run_processes_parallel(&opts);
+
+	return sps.result;
+}
+
 int submodule_uses_gitfile(const char *path)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
diff --git a/submodule.h b/submodule.h
index 6a9fec6de1..cbb7231a5d 100644
--- a/submodule.h
+++ b/submodule.h
@@ -41,6 +41,12 @@ struct submodule_update_strategy {
 	.type = SM_UPDATE_UNSPECIFIED, \
 }
 
+struct submodule_status_util {
+	int changed, ignore_untracked;
+	unsigned dirty_submodule, newmode;
+	struct cache_entry *ce;
+};
+
 int is_gitmodules_unmerged(struct index_state *istate);
 int is_writing_gitmodules_ok(void);
 int is_staging_gitmodules_ok(struct index_state *istate);
@@ -94,6 +100,9 @@ int fetch_submodules(struct repository *r,
 		     int command_line_option,
 		     int default_option,
 		     int quiet, int max_parallel_jobs);
+int get_submodules_status(struct repository *r,
+			  struct string_list *submodules,
+			  int max_parallel_jobs);
 unsigned is_submodule_modified(const char *path, int ignore_untracked);
 int submodule_uses_gitfile(const char *path);
 
diff --git a/t/t4027-diff-submodule.sh b/t/t4027-diff-submodule.sh
index 40164ae07d..e08ee315a7 100755
--- a/t/t4027-diff-submodule.sh
+++ b/t/t4027-diff-submodule.sh
@@ -34,6 +34,25 @@ test_expect_success setup '
 	subtip=$3 subprev=$2
 '
 
+test_expect_success 'diff in superproject with submodules respects parallel settings' '
+	test_when_finished "rm -f trace.out" &&
+	(
+		GIT_TRACE=$(pwd)/trace.out git diff &&
+		grep "1 tasks" trace.out &&
+		>trace.out &&
+
+		git config submodule.diffJobs 8 &&
+		GIT_TRACE=$(pwd)/trace.out git diff &&
+		grep "8 tasks" trace.out &&
+		>trace.out &&
+
+		GIT_TRACE=$(pwd)/trace.out git -c submodule.diffJobs=0 diff &&
+		grep "preparing to run up to [0-9]* tasks" trace.out &&
+		! grep "up to 0 tasks" trace.out &&
+		>trace.out
+	)
+'
+
 test_expect_success 'git diff --raw HEAD' '
 	hexsz=$(test_oid hexsz) &&
 	git diff --raw --abbrev=$hexsz HEAD >actual &&
diff --git a/t/t7506-status-submodule.sh b/t/t7506-status-submodule.sh
index d050091345..52a82b703f 100755
--- a/t/t7506-status-submodule.sh
+++ b/t/t7506-status-submodule.sh
@@ -412,4 +412,23 @@ test_expect_success 'status with added file in nested submodule (short)' '
 	EOF
 '
 
+test_expect_success 'status in superproject with submodules respects parallel settings' '
+	test_when_finished "rm -f trace.out" &&
+	(
+		GIT_TRACE=$(pwd)/trace.out git status &&
+		grep "1 tasks" trace.out &&
+		>trace.out &&
+
+		git config submodule.diffJobs 8 &&
+		GIT_TRACE=$(pwd)/trace.out git status &&
+		grep "8 tasks" trace.out &&
+		>trace.out &&
+
+		GIT_TRACE=$(pwd)/trace.out git -c submodule.diffJobs=0 status &&
+		grep "preparing to run up to [0-9]* tasks" trace.out &&
+		! grep "up to 0 tasks" trace.out &&
+		>trace.out
+	)
+'
+
 test_done
-- 
2.38.1.431.g37b22c650d-goog
Jonathan Tan· Nov 28, 2022, 21:01 UTC · re: Calvin Wan · lore

Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules

Calvin Wan <calvinwan@google.com> writes:
>  submodule.c                        | 154 +++++++++++++++++++++++++++++

I think the way to implement this is to have a parallel implementation, and then have the serial implementation call the parallel implementation's functions, or have a common set of functions that both the parallel implementation and the serial implementation call. Here, it seems that the parallel implementation exists completely separate from the serial implementation, with no code shared. That makes it both more difficult to review, and also makes it difficult to make changes to how we diff submodules in the future (since we would have to make changes in two parts of the code).

I think that the layout of the code will be substantially different if we do that, so I'll hold off on a more thorough review for now.

Glen Choo· Nov 29, 2022, 22:29 UTC · re: Jonathan Tan · lore

Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules

Jonathan Tan <jonathantanmy@google.com> writes:
Show 11 quoted lines
> Calvin Wan <calvinwan@google.com> writes:
>>  submodule.c                        | 154 +++++++++++++++++++++++++++++
>
> I think the way to implement this is to have a parallel implementation,
> and then have the serial implementation call the parallel implementation's
> functions, or have a common set of functions that both the parallel
> implementation and the serial implementation call. Here, it seems that
> the parallel implementation exists completely separate from the serial
> implementation, with no code shared. That makes it both more difficult to
> review, and also makes it difficult to make changes to how we diff submodules
> in the future (since we would have to make changes in two parts of the code).

It seems that most of the code is copied from is_submodule_modified(), so a possible way to do this would be:

- Split is_submodule_modified() into 2 functions, one that sets up the
  "git status --porcelain=2" process (named something like
  setup_status_porcelain()) and one that parses its output (this is
  parse_status_porcelain() in Patch 2). The serial implementation
  (is_submodule_modified()) uses both of these and has some extra logic
  to run the child process.
- Refactor get_next_submodule_status() (from this patch) to use
  setup_status_porcelain().
Calvin Wan· Nov 30, 2022, 18:11 UTC · re: Glen Choo · lore

Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules

On Tue, Nov 29, 2022 at 2:29 PM Glen Choo <chooglen@google.com> wrote:
Show 27 quoted lines
>
> Jonathan Tan <jonathantanmy@google.com> writes:
>
> > Calvin Wan <calvinwan@google.com> writes:
> >>  submodule.c                        | 154 +++++++++++++++++++++++++++++
> >
> > I think the way to implement this is to have a parallel implementation,
> > and then have the serial implementation call the parallel implementation's
> > functions, or have a common set of functions that both the parallel
> > implementation and the serial implementation call. Here, it seems that
> > the parallel implementation exists completely separate from the serial
> > implementation, with no code shared. That makes it both more difficult to
> > review, and also makes it difficult to make changes to how we diff submodules
> > in the future (since we would have to make changes in two parts of the code).
>
> It seems that most of the code is copied from is_submodule_modified(),
> so a possible way to do this would be:
>
> - Split is_submodule_modified() into 2 functions, one that sets up the
>   "git status --porcelain=2" process (named something like
>   setup_status_porcelain()) and one that parses its output (this is
>   parse_status_porcelain() in Patch 2). The serial implementation
>   (is_submodule_modified()) uses both of these and has some extra logic
>   to run the child process.
>
> - Refactor get_next_submodule_status() (from this patch) to use
>   setup_status_porcelain().
That sounds like a reasonable plan to me. Thanks!
Elijah Newren· Nov 29, 2022, 05:13 UTC · re: Calvin Wan · lore

Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules

On Tue, Nov 8, 2022 at 11:32 AM Calvin Wan <calvinwan@google.com> wrote:
Show 39 quoted lines
>
> During the iteration of the index entries in run_diff_files, whenever
> a submodule is found and needs its status checked, a subprocess is
> spawned for it. Instead of spawning the subprocess immediately and
> waiting for its completion to continue, hold onto all submodules and
> relevant information in a list. Then use that list to create tasks for
> run_processes_parallel. Subprocess output is duplicated and passed to
> status_pipe_output which parses it.
>
> Add config option submodule.diffJobs to set the maximum number
> of parallel jobs. The option defaults to 1 if unset. If set to 0, the
> number of jobs is set to online_cpus().
>
> Since run_diff_files is called from many different commands, I chose
> to grab the config option in the function rather than adding variables
> to every git command and then figuring out how to pass them all in.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>  Documentation/config/submodule.txt |  12 +++
>  diff-lib.c                         |  80 +++++++++++++--
>  submodule.c                        | 154 +++++++++++++++++++++++++++++
>  submodule.h                        |   9 ++
>  t/t4027-diff-submodule.sh          |  19 ++++
>  t/t7506-status-submodule.sh        |  19 ++++
>  6 files changed, 287 insertions(+), 6 deletions(-)
>
> diff --git a/Documentation/config/submodule.txt b/Documentation/config/submodule.txt
> index 6490527b45..1144a5ad74 100644
> --- a/Documentation/config/submodule.txt
> +++ b/Documentation/config/submodule.txt
> @@ -93,6 +93,18 @@ submodule.fetchJobs::
>         in parallel. A value of 0 will give some reasonable default.
>         If unset, it defaults to 1.
>
> +submodule.diffJobs::
> +       Specifies how many submodules are diffed at the same time. A
> +       positive integer allows up to that number of submodules diffed
> +       in parallel. A value of 0 will give the number of logical cores.

Why hardcode that 0 gives the number of logical cores? Why not just state that a value of 0 "gives a guess at optimal parallelism", allowing us to adjust it in the future if we can do some smart heuristics? It'd be nice to not have us tied down and prevented from taking a smarter approach.

Show 7 quoted lines
> +       If unset, it defaults to 1. The diff operation is used by many
> +       other git commands such as add, merge, diff, status, stash and
> +       more. Note that the expensive part of the diff operation is
> +       reading the index from cache or memory. Therefore multiple jobs
> +       may be detrimental to performance if your hardware does not
> +       support parallel reads or if the number of jobs greatly exceeds
> +       the amount of supported reads.
So, in the future, someone who wants to speed things up is going to
need to configure submodule.diffJobs, submodule.fetchJobs,
submodule.checkoutJobs, submodule.grepJobs, submodule.mergeJobs, etc.?
 I worry that we're headed towards a bit of a suboptimal user
experience here.  It'd be nice to have a more central configuration of
"yes, I want parallelism; please don't make me benchmark things in
order to take advantage of it", if that's possible.  It may just be
that the "optimal" parallelism varies significantly between commands,
and also varies a lot based on hardware, repository sizes, background
load on the system, etc. such that we can't provide a reasonable
suggestion for those that want a value greater than 1.  Or maybe in
the future we allow folks somehow to request our best guess at a good
parallelization level and then let users override with these
individual flags.  I'm just a little worried we might be making users
do work that we should somehow figure out.
Calvin Wan· Nov 30, 2022, 18:04 UTC · re: Elijah Newren · lore

Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules

Show 18 quoted lines
> > diff --git a/Documentation/config/submodule.txt b/Documentation/config/submodule.txt
> > index 6490527b45..1144a5ad74 100644
> > --- a/Documentation/config/submodule.txt
> > +++ b/Documentation/config/submodule.txt
> > @@ -93,6 +93,18 @@ submodule.fetchJobs::
> >         in parallel. A value of 0 will give some reasonable default.
> >         If unset, it defaults to 1.
> >
> > +submodule.diffJobs::
> > +       Specifies how many submodules are diffed at the same time. A
> > +       positive integer allows up to that number of submodules diffed
> > +       in parallel. A value of 0 will give the number of logical cores.
>
> Why hardcode that 0 gives the number of logical cores?  Why not just
> state that a value of 0 "gives a guess at optimal parallelism",
> allowing us to adjust it in the future if we can do some smart
> heuristics?  It'd be nice to not have us tied down and prevented from
> taking a smarter approach.

I was unaware that the original intention of "reasonable default" was for flexibility (I have a WIP series standardizing these parallelism config options that also used "number of logical cores" but I think that should probably change now). There are other parallel config options that hardcode 0 as well, so my initial thought was that we should be using the more precise wording -- the argument for flexibility now seems more preferable, however.

Show 24 quoted lines
>
> > +       If unset, it defaults to 1. The diff operation is used by many
> > +       other git commands such as add, merge, diff, status, stash and
> > +       more. Note that the expensive part of the diff operation is
> > +       reading the index from cache or memory. Therefore multiple jobs
> > +       may be detrimental to performance if your hardware does not
> > +       support parallel reads or if the number of jobs greatly exceeds
> > +       the amount of supported reads.
>
> So, in the future, someone who wants to speed things up is going to
> need to configure submodule.diffJobs, submodule.fetchJobs,
> submodule.checkoutJobs, submodule.grepJobs, submodule.mergeJobs, etc.?
>  I worry that we're headed towards a bit of a suboptimal user
> experience here.  It'd be nice to have a more central configuration of
> "yes, I want parallelism; please don't make me benchmark things in
> order to take advantage of it", if that's possible.  It may just be
> that the "optimal" parallelism varies significantly between commands,
> and also varies a lot based on hardware, repository sizes, background
> load on the system, etc. such that we can't provide a reasonable
> suggestion for those that want a value greater than 1.  Or maybe in
> the future we allow folks somehow to request our best guess at a good
> parallelization level and then let users override with these
> individual flags.  I'm just a little worried we might be making users
> do work that we should somehow figure out.

I had the same worry as well -- see the discussion I had here: https://lore.kernel.org/git/CAFySSZAbsPuyPVX0+DQzArny2CEWs+GpQqJ3AOxUB_ffo8B3SQ@mail.gmail.com/ I would like to also eventually solve this problem, but this patch won't be the one to do so.

← back to recent threads