Skip to contentSkip to conversation

submodule: parallelize diff

PATCH Superseded Calvin Wan 7 participants 26 messages Nov 8, 2022 – Jan 17, 2023 1 message not in the archive Documentation

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): https://lore.kernel.org/git/[email protected]/

Calvin Wan

author
#1 lore raw

v4 cover submodule: parallelize diff

Original cover letter for context:
https://lore.kernel.org/git/[email protected]/

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(-)
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#2 lore raw
Quoted 11 lines · Calvin Wan

Original cover letter for context:
https://lore.kernel.org/git/[email protected]/

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.

Quoted 4 lines

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/[email protected]/
v2: https://lore.kernel.org/git/[email protected]/
v1: https://lore.kernel.org/git/[email protected]/

#3 lore raw
In reply to Calvin Wan
Quoted 10 lines · Calvin Wan

Original cover letter for context:
https://lore.kernel.org/git/[email protected]/
...
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

Quoted 11 lines
 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.

author
#4 lore raw

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)

Quoted 58 lines · Junio C Hamano
Quoted 10 lines · Calvin Wan

Original cover letter for context:
https://lore.kernel.org/git/[email protected]/
...
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

Quoted 11 lines
 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.

author
#5 lore raw

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

In reply to a message not in the archive

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.

---
 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(-)
run-command.c +11 -2
index c772acd743..b8f430eb03 100644
@@ -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;
run-command.h +24 -0
index e3e1ea01ad..dd6d6a86c2 100644
@@ -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.
t/helper/test-run-command.c +21 -0
index 3ecb830f4a..40dd329e02 100644
@@ -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);
t/t0061-run-command.sh +39 -0
index 7b5423eebd..130aec7c68 100755
@@ -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 &&
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#6 lore raw

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

run-command.c static void pp_init(struct parallel_processes *pp, · line 1565

Quoted 2 lines · Calvin Wan
run-command.c +2 -0
+	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.

Quoted 14 lines
run-command.c +8 -2
@@ -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]

Quoted 4 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.

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.)

Quoted 27 lines
t/helper/test-run-command.c +15 -0
index 3ecb830f4a..40dd329e02 100644
@@ -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,

t/helper/test-run-command.c int cmd__run_command(int argc, const char **argv) · line 461

Quoted 8 lines
t/helper/test-run-command.c +5 -0
@@ -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.

author
#7 lore raw

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

Quoted 4 lines · Jonathan Tan

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)".

Quoted 6 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.

Quoted 31 lines
Quoted 31 lines
t/helper/test-run-command.c +15 -0
index 3ecb830f4a..40dd329e02 100644
@@ -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;
      }
 
Quoted 8 lines

t/helper/test-run-command.c int cmd__run_command(int argc, const char **argv) · line 461

Quoted 5 lines
t/helper/test-run-command.c +5 -0
+     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.

#8 lore raw

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

In reply to Calvin Wan
Quoted 7 lines · Calvin Wan

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.

[...]

t/helper/test-run-command.c static int no_job(struct child_process *cp, · line 58

Quoted 12 lines
t/helper/test-run-command.c +4 -0
index 3ecb830f4a..40dd329e02 100644
@@ -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?

author
#9 lore raw

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

Quoted 18 lines · Elijah Newren
Quoted 7 lines · Calvin Wan

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.

[...]

Quoted 8 lines
t/helper/test-run-command.c +0 -0
index 3ecb830f4a..40dd329e02 100644
@@ -52,6 +52,21 @@ static int no_job(struct child_process *cp,
        return 0;
 }
 
Quoted 6 lines

t/helper/test-run-command.c static int no_job(struct child_process *cp, · line 58

Quoted 4 lines
t/helper/test-run-command.c +4 -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!

#10 lore raw

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

In reply to Calvin Wan
Quoted 7 lines · Calvin Wan
run-command.c +1 -2
@@ -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;

run-command.c static void pp_buffer_stderr(struct parallel_processes *pp, · line 1696

Quoted 10 lines
run-command.c +7 -0
+			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.

#11 lore raw

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

Quoted 34 lines · Glen Choo
Quoted 7 lines · Calvin Wan
run-command.c +1 -2
@@ -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;

run-command.c static void pp_buffer_stderr(struct parallel_processes *pp, · line 1696

Quoted 10 lines
run-command.c +7 -0
+			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);
Quoted 5 lines

+ 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.

#12 lore raw

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

Quoted 5 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Glen Choo

Calvin Wan <[email protected]> writes:

Quoted 10 lines
Quoted 10 lines

run-command.c static void pp_buffer_stderr(struct parallel_processes *pp, · line 1689

Quoted 10 lines
run-command.c +4 -2
@@ -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?

Quoted 25 lines
Quoted 22 lines

run-command.c static void pp_buffer_stderr(struct parallel_processes *pp, · line 1696

Quoted 7 lines
run-command.c +4 -0
+				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);
Quoted 3 lines

+ strbuf_release(&buf);
+ }

Quoted 2 lines

[...]
And why does "&buf" exist at all?

I was wondering that as too

Quoted 5 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.

Quoted 1 line

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

author
#13 lore raw

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

Quoted 7 lines · Phillip Wood
Quoted 5 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Glen Choo

Calvin Wan <[email protected]> writes:

Quoted 12 lines
Quoted 10 lines
Quoted 10 lines

run-command.c static void pp_buffer_stderr(struct parallel_processes *pp, · line 1689

Quoted 10 lines
run-command.c +4 -2
@@ -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.

Quoted 31 lines
Quoted 26 lines
Quoted 21 lines

run-command.c static void pp_buffer_stderr(struct parallel_processes *pp, · line 1696

Quoted 8 lines
run-command.c +4 -0
+                           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,
Quoted 5 lines

+ opts->data,
+ pp->children[i].data);
+ strbuf_release(&buf);
+ }

Quoted 2 lines

[...]
And why does "&buf" exist at all?

I was wondering that as too

Quoted 7 lines
Quoted 5 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.

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

Quoted 7 lines
Quoted 2 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 ;)

#14 lore raw

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

In reply to Ævar Arnfjörð Bjarmason
Quoted 17 lines · Ævar Arnfjörð Bjarmason
Quoted 14 lines · Glen Choo

Calvin Wan <[email protected]> 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);
+ }

Quoted 5 lines
Quoted 2 lines

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

#15 lore raw

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

Quoted 17 lines · Phillip Wood
Quoted 16 lines · Ævar Arnfjörð Bjarmason
Quoted 14 lines · Glen Choo

Calvin Wan <[email protected]> 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);
+ }

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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.)

author
#16 lore raw

PATCH v4 2/5 submodule: strbuf variable rename

In reply to a message not in the archive

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

---
 submodule.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
submodule.c +13 -10
index b958162d28..31ee53bd57 100644
@@ -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;
 		}
Signature · 1 line
2.38.1.431.g37b22c650d-goog
author
#17 lore raw

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

In reply to a message not in the archive

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.

---
 submodule.c | 74 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
submodule.c +42 -32
index 31ee53bd57..fd3385620c 100644
@@ -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);
 
Signature · 1 line
2.38.1.431.g37b22c650d-goog
author
#18 lore raw

PATCH v4 4/5 diff-lib: refactor match_stat_with_submodule

In reply to a message not in the archive

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.

---
 diff-lib.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
diff-lib.c +18 -11
index 2edea41a23..f5257c0c71 100644
@@ -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;
 }
 
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#19 lore raw

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

Hi Calven

Quoted 4 lines · Calvin Wan

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

Quoted 46 lines
---
  diff-lib.c | 29 ++++++++++++++++++-----------
  1 file changed, 18 insertions(+), 11 deletions(-)
diff-lib.c +18 -11
index 2edea41a23..f5257c0c71 100644
@@ -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;
  }
 
author
#20 lore raw

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

Quoted 4 lines

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

author
#21 lore raw

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

In reply to a message not in the archive

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.

---
 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(-)
Documentation/config/submodule.txt +12 -0
index 6490527b45..1144a5ad74 100644
@@ -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-lib.c +74 -6

140 lines not on this page · raw message

submodule.c +154 -0

172 lines not on this page · raw message

submodule.h +9 -0
index 6a9fec6de1..cbb7231a5d 100644
@@ -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);
 
t/t4027-diff-submodule.sh +19 -0
index 40164ae07d..e08ee315a7 100755
@@ -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 &&
t/t7506-status-submodule.sh +19 -0
index d050091345..52a82b703f 100755
@@ -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
Signature · 1 line
2.38.1.431.g37b22c650d-goog
#22 lore raw

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

Quoted 1 line · Calvin Wan
 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.

#23 lore raw

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

Quoted 11 lines · Jonathan Tan
Quoted 1 line · Calvin Wan
 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().

author
#24 lore raw

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

Quoted 25 lines · Glen Choo
Quoted 11 lines · Jonathan Tan
Quoted 1 line · Calvin Wan
 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.

Quoted 2 lines

- Refactor get_next_submodule_status() (from this patch) to use setup_status_porcelain().

That sounds like a reasonable plan to me. Thanks!

#25 lore raw

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

In reply to Calvin Wan
Quoted 32 lines · Calvin Wan

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.

---
 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(-)
Documentation/config/submodule.txt +0 -0
index 6490527b45..1144a5ad74 100644

Documentation/config/submodule.txt submodule.fetchJobs:: · line 99

Quoted 7 lines
Documentation/config/submodule.txt +4 -0
@@ -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.

Documentation/config/submodule.txt submodule.fetchJobs:: · line 106

Quoted 7 lines
Documentation/config/submodule.txt +7 -0
+       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.

author
#26 lore raw

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

Quoted 8 lines
Quoted 8 lines
Documentation/config/submodule.txt +0 -0
index 6490527b45..1144a5ad74 100644
@@ -93,6 +93,18 @@ submodule.fetchJobs::
        in parallel. A value of 0 will give some reasonable default.
        If unset, it defaults to 1.
 
Quoted 10 lines

Documentation/config/submodule.txt submodule.fetchJobs:: · line 99

Quoted 4 lines
Documentation/config/submodule.txt +4 -0
+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.

Quoted 9 lines

Documentation/config/submodule.txt submodule.fetchJobs:: · line 106

Quoted 8 lines
Documentation/config/submodule.txt +7 -0
+       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.
Quoted 15 lines

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.