git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 10/12] diff --color-moved-ws=allow-indentation-change: improve hash lookups

From
PGPhillip Wood via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 20, 2021, 10:36 UTC
Message-ID
<dd365ad115ffcf3ce5b4b14864ac3ff0ac292214.1626777394.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.981.v2.git.1626777393.gitgitgadget@gmail.com>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

As libxdiff does not have a whitespace flag to ignore the indentation the code for --color-moved-ws=allow-indentation-change uses XDF_IGNORE_WHITESPACE and then filters out any hash lookups where there are non-indentation changes. This filtering is inefficient as we have to perform another string comparison.

By using the offset data that we have already computed to skip the indentation we can avoid using XDF_IGNORE_WHITESPACE and safely remove the extra checks which improves the performance by 11% and paves the way for the elimination of string comparisons in the next commit.

This change slightly increases the run time of other --color-moved modes. This could be avoided by using different comparison functions for the different modes but after the next two commits there is no measurable benefit in doing so.

Test HEAD^ HEAD -------------------------------------------------------------------------------------------------------------- 4002.1: diff --no-color-moved --no-color-moved-ws large change 0.41(0.38+0.03) 0.41(0.36+0.04) +0.0% 4002.2: diff --color-moved --no-color-moved-ws large change 0.82(0.76+0.05) 0.84(0.79+0.04) +2.4% 4002.3: diff --color-moved-ws=allow-indentation-change large change 0.91(0.88+0.03) 0.81(0.74+0.06) -11.0% 4002.4: log --no-color-moved --no-color-moved-ws 1.32(1.21+0.10) 1.31(1.19+0.11) -0.8% 4002.5: log --color-moved --no-color-moved-ws 1.47(1.37+0.10) 1.47(1.36+0.11) +0.0% 4002.6: log --color-moved-ws=allow-indentation-change 1.51(1.42+0.09) 1.48(1.37+0.10) -2.0%

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 diff.c | 66 +++++++++++++++++-----------------------------------------
 1 file changed, 19 insertions(+), 47 deletions(-)
diff --git a/diff.c b/diff.c
index 4b5776a5a0a..f899083d028 100644
--- a/diff.c
+++ b/diff.c
@@ -850,28 +850,15 @@ static void fill_es_indent_data(struct emitted_diff_symbol *es)
 }
 
 static int compute_ws_delta(const struct emitted_diff_symbol *a,
-			    const struct emitted_diff_symbol *b,
-			    int *out)
-{
-	int a_len = a->len,
-	    b_len = b->len,
-	    a_off = a->indent_off,
-	    a_width = a->indent_width,
-	    b_off = b->indent_off,
+			    const struct emitted_diff_symbol *b)
+{
+	int a_width = a->indent_width,
 	    b_width = b->indent_width;
 
-	if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) {
-		*out = INDENT_BLANKLINE;
-		return 1;
-	}
-
-	if (a_len - a_off != b_len - b_off ||
-	    memcmp(a->line + a_off, b->line + b_off, a_len - a_off))
-		return 0;
-
-	*out = a_width - b_width;
+	if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE)
+		return INDENT_BLANKLINE;
 
-	return 1;
+	return a_width - b_width;
 }
 
 static int cmp_in_block_with_wsd(const struct diff_options *o,
@@ -917,26 +904,17 @@ static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
 			   const void *keydata)
 {
 	const struct diff_options *diffopt = hashmap_cmp_fn_data;
-	const struct moved_entry *a, *b;
+	const struct emitted_diff_symbol *a, *b;
 	unsigned flags = diffopt->color_moved_ws_handling
 			 & XDF_WHITESPACE_FLAGS;
 
-	a = container_of(eptr, const struct moved_entry, ent);
-	b = container_of(entry_or_key, const struct moved_entry, ent);
-
-	if (diffopt->color_moved_ws_handling &
-	    COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
-		/*
-		 * As there is not specific white space config given,
-		 * we'd need to check for a new block, so ignore all
-		 * white space. The setup of the white space
-		 * configuration for the next block is done else where
-		 */
-		flags |= XDF_IGNORE_WHITESPACE;
+	a = container_of(eptr, const struct moved_entry, ent)->es;
+	b = container_of(entry_or_key, const struct moved_entry, ent)->es;
 
-	return !xdiff_compare_lines(a->es->line, a->es->len,
-				    b->es->line, b->es->len,
-				    flags);
+	return !xdiff_compare_lines(a->line + a->indent_off,
+				    a->len - a->indent_off,
+				    b->line + b->indent_off,
+				    b->len - b->indent_off, flags);
 }
 
 static struct moved_entry *prepare_entry(struct diff_options *o,
@@ -945,7 +923,8 @@ static struct moved_entry *prepare_entry(struct diff_options *o,
 	struct moved_entry *ret = xmalloc(sizeof(*ret));
 	struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
 	unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
-	unsigned int hash = xdiff_hash_string(l->line, l->len, flags);
+	unsigned int hash = xdiff_hash_string(l->line + l->indent_off,
+					      l->len - l->indent_off, flags);
 
 	hashmap_entry_init(&ret->ent, hash);
 	ret->es = l;
@@ -1113,14 +1092,11 @@ static void mark_color_as_moved(struct diff_options *o,
 			hashmap_for_each_entry_from(hm, match, ent) {
 				ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
 				if (o->color_moved_ws_handling &
-				    COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
-					if (compute_ws_delta(l, match->es,
-							     &pmb[pmb_nr].wsd))
-						pmb[pmb_nr++].match = match;
-				} else {
+				    COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
+					pmb[pmb_nr].wsd = compute_ws_delta(l, match->es);
+				else
 					pmb[pmb_nr].wsd = 0;
-					pmb[pmb_nr++].match = match;
-				}
+				pmb[pmb_nr++].match = match;
 			}
 
 			if (adjust_last_block(o, n, block_length) &&
@@ -6240,10 +6216,6 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
 		if (o->color_moved) {
 			struct hashmap add_lines, del_lines;
 
-			if (o->color_moved_ws_handling &
-			    COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
-				o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
-
 			hashmap_init(&del_lines, moved_entry_cmp, o, 0);
 			hashmap_init(&add_lines, moved_entry_cmp, o, 0);
 
-- 
gitgitgadget
Previous: Phillip Wood via GitGitGadgetNext: Phillip Wood via GitGitGadget
Message 28 of 92 in “diff --color-moved[-ws] speedups”
  1. 00/10 diff --color-moved[-ws] speedupsPhillip Wood via GitGitGadget, Jun 14, 2021
  2. 01/10 diff --color-moved=zerba: fix alternate coloringPhillip Wood via GitGitGadget, Jun 14, 2021
  3. Junio C HamanoJun 15, 2021
  4. Phillip WoodJun 15, 2021
  5. 07/10 diff --color-moved: shrink potential moved blocks as we goPhillip Wood via GitGitGadget, Jun 14, 2021
  6. 09/10 diff --color-moved-ws=allow-indentation-change: improve hash lookupsPhillip Wood via GitGitGadget, Jun 14, 2021
  7. Elijah NewrenJul 9, 2021
  8. 02/10 diff --color-moved: avoid false short line matches and bad zerba coloringPhillip Wood via GitGitGadget, Jun 14, 2021
  9. 03/10 diff: simplify allow-indentation-change delta calculationPhillip Wood via GitGitGadget, Jun 14, 2021
  10. 04/10 diff --color-moved-ws=allow-indentation-change: simplify and optimizePhillip Wood via GitGitGadget, Jun 14, 2021
  11. 05/10 diff --color-moved: call comparison function directlyPhillip Wood via GitGitGadget, Jun 14, 2021
  12. 06/10 diff --color-moved: unify moved block growth functionsPhillip Wood via GitGitGadget, Jun 14, 2021
  13. 10/10 diff --color-moved: intern stringsPhillip Wood via GitGitGadget, Jun 14, 2021
  14. 08/10 diff --color-moved: stop clearing potential moved blocksPhillip Wood via GitGitGadget, Jun 14, 2021
  15. Ævar Arnfjörð BjarmasonJun 16, 2021
  16. Phillip WoodJun 21, 2021
  17. 00/12 diff --color-moved[-ws] speedupsPhillip Wood via GitGitGadget, Jul 20, 2021
  18. 05/12 diff --color-moved-ws=allow-indentation-change: simplify and optimizePhillip Wood via GitGitGadget, Jul 20, 2021
  19. 04/12 diff: simplify allow-indentation-change delta calculationPhillip Wood via GitGitGadget, Jul 20, 2021
  20. 03/12 diff --color-moved: avoid false short line matches and bad zerba coloringPhillip Wood via GitGitGadget, Jul 20, 2021
  21. 02/12 diff --color-moved=zebra: fix alternate coloringPhillip Wood via GitGitGadget, Jul 20, 2021
  22. 01/12 diff --color-moved: add perf testsPhillip Wood via GitGitGadget, Jul 20, 2021
  23. 07/12 diff --color-moved: unify moved block growth functionsPhillip Wood via GitGitGadget, Jul 20, 2021
  24. 06/12 diff --color-moved: call comparison function directlyPhillip Wood via GitGitGadget, Jul 20, 2021
  25. 09/12 diff --color-moved: stop clearing potential moved blocksPhillip Wood via GitGitGadget, Jul 20, 2021
  26. 08/12 diff --color-moved: shrink potential moved blocks as we goPhillip Wood via GitGitGadget, Jul 20, 2021
  27. 11/12 diff: use designated initializers for emitted_diff_symbolPhillip Wood via GitGitGadget, Jul 20, 2021
  28. 10/12 diff --color-moved-ws=allow-indentation-change: improve hash lookupsPhillip Wood via GitGitGadget, Jul 20, 2021
  29. 12/12 diff --color-moved: intern stringsPhillip Wood via GitGitGadget, Jul 20, 2021
  30. Phillip WoodJul 20, 2021
  31. 00/15 diff --color-moved[-ws] speedupsPhillip Wood via GitGitGadget, Oct 27, 2021
  32. 01/15 diff --color-moved: add perf testsPhillip Wood via GitGitGadget, Oct 27, 2021
  33. Junio C HamanoOct 28, 2021
  34. Phillip WoodOct 29, 2021
  35. Ævar Arnfjörð BjarmasonOct 29, 2021
  36. Phillip WoodNov 10, 2021
  37. 02/15 diff --color-moved: clear all flags on blocks that are too shortPhillip Wood via GitGitGadget, Oct 27, 2021
  38. 03/15 diff --color-moved: factor out functionPhillip Wood via GitGitGadget, Oct 27, 2021
  39. Junio C HamanoOct 28, 2021
  40. Phillip WoodOct 29, 2021
  41. 04/15 diff --color-moved: rewind when discarding pmbPhillip Wood via GitGitGadget, Oct 27, 2021
  42. 05/15 diff --color-moved=zebra: fix alternate coloringPhillip Wood via GitGitGadget, Oct 27, 2021
  43. 06/15 diff --color-moved: avoid false short line matches and bad zerba coloringPhillip Wood via GitGitGadget, Oct 27, 2021
  44. 07/15 diff: simplify allow-indentation-change delta calculationPhillip Wood via GitGitGadget, Oct 27, 2021
  45. 08/15 diff --color-moved-ws=allow-indentation-change: simplify and optimizePhillip Wood via GitGitGadget, Oct 27, 2021
  46. 09/15 diff --color-moved: call comparison function directlyPhillip Wood via GitGitGadget, Oct 27, 2021
  47. 10/15 diff --color-moved: unify moved block growth functionsPhillip Wood via GitGitGadget, Oct 27, 2021
  48. 11/15 diff --color-moved: shrink potential moved blocks as we goPhillip Wood via GitGitGadget, Oct 27, 2021
  49. 12/15 diff --color-moved: stop clearing potential moved blocksPhillip Wood via GitGitGadget, Oct 27, 2021
  50. 13/15 diff --color-moved-ws=allow-indentation-change: improve hash lookupsPhillip Wood via GitGitGadget, Oct 27, 2021
  51. 14/15 diff: use designated initializers for emitted_diff_symbolPhillip Wood via GitGitGadget, Oct 27, 2021
  52. 15/15 diff --color-moved: intern stringsPhillip Wood via GitGitGadget, Oct 27, 2021
  53. Phillip WoodOct 27, 2021
  54. 00/15 diff --color-moved[-ws] speedupsPhillip Wood via GitGitGadget, Nov 16, 2021
  55. 01/15 diff --color-moved: add perf testsPhillip Wood via GitGitGadget, Nov 16, 2021
  56. 02/15 diff --color-moved: clear all flags on blocks that are too shortPhillip Wood via GitGitGadget, Nov 16, 2021
  57. 03/15 diff --color-moved: factor out functionPhillip Wood via GitGitGadget, Nov 16, 2021
  58. 04/15 diff --color-moved: rewind when discarding pmbPhillip Wood via GitGitGadget, Nov 16, 2021
  59. 05/15 diff --color-moved=zebra: fix alternate coloringPhillip Wood via GitGitGadget, Nov 16, 2021
  60. Johannes SchindelinNov 22, 2021
  61. 06/15 diff --color-moved: avoid false short line matches and bad zerba coloringPhillip Wood via GitGitGadget, Nov 16, 2021
  62. Johannes SchindelinNov 22, 2021
  63. Phillip WoodNov 22, 2021
  64. Johannes SchindelinNov 22, 2021
  65. 07/15 diff: simplify allow-indentation-change delta calculationPhillip Wood via GitGitGadget, Nov 16, 2021
  66. 08/15 diff --color-moved-ws=allow-indentation-change: simplify and optimizePhillip Wood via GitGitGadget, Nov 16, 2021
  67. Johannes SchindelinNov 23, 2021
  68. 11/15 diff --color-moved: shrink potential moved blocks as we goPhillip Wood via GitGitGadget, Nov 16, 2021
  69. 10/15 diff --color-moved: unify moved block growth functionsPhillip Wood via GitGitGadget, Nov 16, 2021
  70. 09/15 diff --color-moved: call comparison function directlyPhillip Wood via GitGitGadget, Nov 16, 2021
  71. Johannes SchindelinNov 23, 2021
  72. 12/15 diff --color-moved: stop clearing potential moved blocksPhillip Wood via GitGitGadget, Nov 16, 2021
  73. 13/15 diff --color-moved-ws=allow-indentation-change: improve hash lookupsPhillip Wood via GitGitGadget, Nov 16, 2021
  74. 14/15 diff: use designated initializers for emitted_diff_symbolPhillip Wood via GitGitGadget, Nov 16, 2021
  75. 15/15 diff --color-moved: intern stringsPhillip Wood via GitGitGadget, Nov 16, 2021
  76. Johannes SchindelinDec 8, 2021
  77. 00/15 diff --color-moved[-ws] speedupsPhillip Wood via GitGitGadget, Dec 9, 2021
  78. 01/15 diff --color-moved: add perf testsPhillip Wood via GitGitGadget, Dec 9, 2021
  79. 02/15 diff --color-moved: clear all flags on blocks that are too shortPhillip Wood via GitGitGadget, Dec 9, 2021
  80. 05/15 diff --color-moved=zebra: fix alternate coloringPhillip Wood via GitGitGadget, Dec 9, 2021
  81. 03/15 diff --color-moved: factor out functionPhillip Wood via GitGitGadget, Dec 9, 2021
  82. 06/15 diff --color-moved: avoid false short line matches and bad zebra coloringPhillip Wood via GitGitGadget, Dec 9, 2021
  83. 07/15 diff: simplify allow-indentation-change delta calculationPhillip Wood via GitGitGadget, Dec 9, 2021
  84. 04/15 diff --color-moved: rewind when discarding pmbPhillip Wood via GitGitGadget, Dec 9, 2021
  85. 08/15 diff --color-moved-ws=allow-indentation-change: simplify and optimizePhillip Wood via GitGitGadget, Dec 9, 2021
  86. 09/15 diff --color-moved: call comparison function directlyPhillip Wood via GitGitGadget, Dec 9, 2021
  87. 10/15 diff --color-moved: unify moved block growth functionsPhillip Wood via GitGitGadget, Dec 9, 2021
  88. 11/15 diff --color-moved: shrink potential moved blocks as we goPhillip Wood via GitGitGadget, Dec 9, 2021
  89. 12/15 diff --color-moved: stop clearing potential moved blocksPhillip Wood via GitGitGadget, Dec 9, 2021
  90. 13/15 diff --color-moved-ws=allow-indentation-change: improve hash lookupsPhillip Wood via GitGitGadget, Dec 9, 2021
  91. 15/15 diff --color-moved: intern stringsPhillip Wood via GitGitGadget, Dec 9, 2021
  92. 14/15 diff: use designated initializers for emitted_diff_symbolPhillip Wood via GitGitGadget, Dec 9, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.