git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v4 0/5] submodule: parallelize diff
       [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
@ 2022-11-08 18:41 ` Calvin Wan
  2022-11-23 17:49   ` Glen Choo
  2023-01-15  9:31   ` Junio C Hamano
  2022-11-08 18:41 ` [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts Calvin Wan
                   ` (4 subsequent siblings)
  5 siblings, 2 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-08 18:41 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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


^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
       [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
  2022-11-08 18:41 ` [PATCH v4 0/5] submodule: parallelize diff Calvin Wan
@ 2022-11-08 18:41 ` Calvin Wan
  2022-11-28 20:45   ` Jonathan Tan
                     ` (2 more replies)
  2022-11-08 18:41 ` [PATCH v4 2/5] submodule: strbuf variable rename Calvin Wan
                   ` (3 subsequent siblings)
  5 siblings, 3 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-08 18:41 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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

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


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 2/5] submodule: strbuf variable rename
       [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
  2022-11-08 18:41 ` [PATCH v4 0/5] submodule: parallelize diff Calvin Wan
  2022-11-08 18:41 ` [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts Calvin Wan
@ 2022-11-08 18:41 ` Calvin Wan
  2022-11-08 18:41 ` [PATCH v4 3/5] submodule: move status parsing into function Calvin Wan
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-08 18:41 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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

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


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 3/5] submodule: move status parsing into function
       [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
                   ` (2 preceding siblings ...)
  2022-11-08 18:41 ` [PATCH v4 2/5] submodule: strbuf variable rename Calvin Wan
@ 2022-11-08 18:41 ` Calvin Wan
  2022-11-08 18:41 ` [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule Calvin Wan
  2022-11-08 18:42 ` [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules Calvin Wan
  5 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-08 18:41 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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

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


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule
       [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
                   ` (3 preceding siblings ...)
  2022-11-08 18:41 ` [PATCH v4 3/5] submodule: move status parsing into function Calvin Wan
@ 2022-11-08 18:41 ` Calvin Wan
  2022-11-30 14:36   ` Phillip Wood
  2022-11-08 18:42 ` [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules Calvin Wan
  5 siblings, 1 reply; 26+ messages in thread
From: Calvin Wan @ 2022-11-08 18:41 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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

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


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules
       [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
                   ` (4 preceding siblings ...)
  2022-11-08 18:41 ` [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule Calvin Wan
@ 2022-11-08 18:42 ` Calvin Wan
  2022-11-28 21:01   ` Jonathan Tan
  2022-11-29  5:13   ` Elijah Newren
  5 siblings, 2 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-08 18:42 UTC (permalink / raw)
  To: git; +Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 0/5] submodule: parallelize diff
  2022-11-08 18:41 ` [PATCH v4 0/5] submodule: parallelize diff Calvin Wan
@ 2022-11-23 17:49   ` Glen Choo
  2023-01-15  9:31   ` Junio C Hamano
  1 sibling, 0 replies; 26+ messages in thread
From: Glen Choo @ 2022-11-23 17:49 UTC (permalink / raw)
  To: Calvin Wan, git
  Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

Calvin Wan <calvinwan@google.com> writes:

> 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/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-08 18:41 ` [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts Calvin Wan
@ 2022-11-28 20:45   ` Jonathan Tan
  2022-11-30 18:46     ` Calvin Wan
  2022-11-29  5:11   ` Elijah Newren
  2022-11-29 23:29   ` Glen Choo
  2 siblings, 1 reply; 26+ messages in thread
From: Jonathan Tan @ 2022-11-28 20:45 UTC (permalink / raw)
  To: Calvin Wan
  Cc: Jonathan Tan, git, emilyshaffer, avarab, phillip.wood123,
	myriamanis

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.

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

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules
  2022-11-08 18:42 ` [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules Calvin Wan
@ 2022-11-28 21:01   ` Jonathan Tan
  2022-11-29 22:29     ` Glen Choo
  2022-11-29  5:13   ` Elijah Newren
  1 sibling, 1 reply; 26+ messages in thread
From: Jonathan Tan @ 2022-11-28 21:01 UTC (permalink / raw)
  To: Calvin Wan
  Cc: Jonathan Tan, git, emilyshaffer, avarab, phillip.wood123,
	myriamanis

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.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-08 18:41 ` [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts Calvin Wan
  2022-11-28 20:45   ` Jonathan Tan
@ 2022-11-29  5:11   ` Elijah Newren
  2022-11-30 18:47     ` Calvin Wan
  2022-11-29 23:29   ` Glen Choo
  2 siblings, 1 reply; 26+ messages in thread
From: Elijah Newren @ 2022-11-29  5:11 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

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?

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules
  2022-11-08 18:42 ` [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules Calvin Wan
  2022-11-28 21:01   ` Jonathan Tan
@ 2022-11-29  5:13   ` Elijah Newren
  2022-11-30 18:04     ` Calvin Wan
  1 sibling, 1 reply; 26+ messages in thread
From: Elijah Newren @ 2022-11-29  5:13 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

On Tue, Nov 8, 2022 at 11:32 AM Calvin Wan <calvinwan@google.com> wrote:
>
> 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.

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules
  2022-11-28 21:01   ` Jonathan Tan
@ 2022-11-29 22:29     ` Glen Choo
  2022-11-30 18:11       ` Calvin Wan
  0 siblings, 1 reply; 26+ messages in thread
From: Glen Choo @ 2022-11-29 22:29 UTC (permalink / raw)
  To: Jonathan Tan, Calvin Wan
  Cc: Jonathan Tan, git, emilyshaffer, avarab, phillip.wood123,
	myriamanis

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-08 18:41 ` [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts Calvin Wan
  2022-11-28 20:45   ` Jonathan Tan
  2022-11-29  5:11   ` Elijah Newren
@ 2022-11-29 23:29   ` Glen Choo
  2022-11-30  9:53     ` Ævar Arnfjörð Bjarmason
  2 siblings, 1 reply; 26+ messages in thread
From: Glen Choo @ 2022-11-29 23:29 UTC (permalink / raw)
  To: Calvin Wan, git
  Cc: Calvin Wan, emilyshaffer, avarab, phillip.wood123, myriamanis

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.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-29 23:29   ` Glen Choo
@ 2022-11-30  9:53     ` Ævar Arnfjörð Bjarmason
  2022-11-30 10:26       ` Phillip Wood
  2022-11-30 10:28       ` Phillip Wood
  0 siblings, 2 replies; 26+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-30  9:53 UTC (permalink / raw)
  To: Glen Choo; +Cc: git, Calvin Wan, emilyshaffer, phillip.wood123, myriamanis


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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-30  9:53     ` Ævar Arnfjörð Bjarmason
@ 2022-11-30 10:26       ` Phillip Wood
  2022-11-30 19:02         ` Calvin Wan
  2022-11-30 10:28       ` Phillip Wood
  1 sibling, 1 reply; 26+ messages in thread
From: Phillip Wood @ 2022-11-30 10:26 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, Glen Choo
  Cc: git, Calvin Wan, emilyshaffer, phillip.wood123, myriamanis

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?

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

> 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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-30  9:53     ` Ævar Arnfjörð Bjarmason
  2022-11-30 10:26       ` Phillip Wood
@ 2022-11-30 10:28       ` Phillip Wood
  2022-11-30 10:57         ` Ævar Arnfjörð Bjarmason
  1 sibling, 1 reply; 26+ messages in thread
From: Phillip Wood @ 2022-11-30 10:28 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, Glen Choo
  Cc: git, Calvin Wan, emilyshaffer, phillip.wood123, myriamanis

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.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-30 10:28       ` Phillip Wood
@ 2022-11-30 10:57         ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 26+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-11-30 10:57 UTC (permalink / raw)
  To: phillip.wood
  Cc: Glen Choo, git, Calvin Wan, emilyshaffer, phillip.wood123,
	myriamanis


On Wed, Nov 30 2022, Phillip Wood wrote:

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule
  2022-11-08 18:41 ` [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule Calvin Wan
@ 2022-11-30 14:36   ` Phillip Wood
  2022-11-30 19:08     ` Calvin Wan
  0 siblings, 1 reply; 26+ messages in thread
From: Phillip Wood @ 2022-11-30 14:36 UTC (permalink / raw)
  To: Calvin Wan, git; +Cc: emilyshaffer, avarab, phillip.wood123, myriamanis

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

> 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;
>   }
>   

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules
  2022-11-29  5:13   ` Elijah Newren
@ 2022-11-30 18:04     ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-30 18:04 UTC (permalink / raw)
  To: Elijah Newren; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

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

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules
  2022-11-29 22:29     ` Glen Choo
@ 2022-11-30 18:11       ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-30 18:11 UTC (permalink / raw)
  To: Glen Choo
  Cc: Jonathan Tan, git, emilyshaffer, avarab, phillip.wood123,
	myriamanis

On Tue, Nov 29, 2022 at 2:29 PM Glen Choo <chooglen@google.com> wrote:
>
> 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!

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-28 20:45   ` Jonathan Tan
@ 2022-11-30 18:46     ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-30 18:46 UTC (permalink / raw)
  To: Jonathan Tan; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

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

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

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-29  5:11   ` Elijah Newren
@ 2022-11-30 18:47     ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-30 18:47 UTC (permalink / raw)
  To: Elijah Newren; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

On Mon, Nov 28, 2022 at 9:11 PM Elijah Newren <newren@gmail.com> wrote:
>
> 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!

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts
  2022-11-30 10:26       ` Phillip Wood
@ 2022-11-30 19:02         ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-30 19:02 UTC (permalink / raw)
  To: phillip.wood
  Cc: Ævar Arnfjörð Bjarmason, Glen Choo, git,
	emilyshaffer, phillip.wood123, myriamanis

On Wed, Nov 30, 2022 at 2:26 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>
> 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.
>
> >>> +                           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.

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule
  2022-11-30 14:36   ` Phillip Wood
@ 2022-11-30 19:08     ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2022-11-30 19:08 UTC (permalink / raw)
  To: phillip.wood; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

> 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

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 0/5] submodule: parallelize diff
  2022-11-08 18:41 ` [PATCH v4 0/5] submodule: parallelize diff Calvin Wan
  2022-11-23 17:49   ` Glen Choo
@ 2023-01-15  9:31   ` Junio C Hamano
  2023-01-17 19:31     ` Calvin Wan
  1 sibling, 1 reply; 26+ messages in thread
From: Junio C Hamano @ 2023-01-15  9:31 UTC (permalink / raw)
  To: Calvin Wan; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

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.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 0/5] submodule: parallelize diff
  2023-01-15  9:31   ` Junio C Hamano
@ 2023-01-17 19:31     ` Calvin Wan
  0 siblings, 0 replies; 26+ messages in thread
From: Calvin Wan @ 2023-01-17 19:31 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, emilyshaffer, avarab, phillip.wood123, myriamanis

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

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2023-01-17 21:07 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <https://lore.kernel.org/git/20221020232532.1128326-1-calvinwan@google.com/>
2022-11-08 18:41 ` [PATCH v4 0/5] submodule: parallelize diff Calvin Wan
2022-11-23 17:49   ` Glen Choo
2023-01-15  9:31   ` Junio C Hamano
2023-01-17 19:31     ` Calvin Wan
2022-11-08 18:41 ` [PATCH v4 1/5] run-command: add duplicate_output_fn to run_processes_parallel_opts Calvin Wan
2022-11-28 20:45   ` Jonathan Tan
2022-11-30 18:46     ` Calvin Wan
2022-11-29  5:11   ` Elijah Newren
2022-11-30 18:47     ` Calvin Wan
2022-11-29 23:29   ` Glen Choo
2022-11-30  9:53     ` Ævar Arnfjörð Bjarmason
2022-11-30 10:26       ` Phillip Wood
2022-11-30 19:02         ` Calvin Wan
2022-11-30 10:28       ` Phillip Wood
2022-11-30 10:57         ` Ævar Arnfjörð Bjarmason
2022-11-08 18:41 ` [PATCH v4 2/5] submodule: strbuf variable rename Calvin Wan
2022-11-08 18:41 ` [PATCH v4 3/5] submodule: move status parsing into function Calvin Wan
2022-11-08 18:41 ` [PATCH v4 4/5] diff-lib: refactor match_stat_with_submodule Calvin Wan
2022-11-30 14:36   ` Phillip Wood
2022-11-30 19:08     ` Calvin Wan
2022-11-08 18:42 ` [PATCH v4 5/5] diff-lib: parallelize run_diff_files for submodules Calvin Wan
2022-11-28 21:01   ` Jonathan Tan
2022-11-29 22:29     ` Glen Choo
2022-11-30 18:11       ` Calvin Wan
2022-11-29  5:13   ` Elijah Newren
2022-11-30 18:04     ` Calvin Wan

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).