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

[PATCH 8/9] range-diff: use a hunk callback

From
Jeff King <peff@peff.net>
Date
Nov 2, 2018, 06:39 UTC
Message-ID
<20181102063939.GH31216@sigill.intra.peff.net>
In-Reply-To
<20181102063156.GA30252@sigill.intra.peff.net>

When we count the lines in a diff, we don't actually care about the contents of each line. By using a hunk callback, we tell xdiff that it does not need to even bother generating a hunk header line, saving a small amount of work.

Arguably we could even ignore the hunk headers completely, since we're just computing a cost function between patches. But doing it this way maintains the exact same behavior before and after.

Signed-off-by: Jeff King <peff@peff.net>
---
This one might be going overboard. It can be dropped without affecting
any of the other patches.
 range-diff.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/range-diff.c b/range-diff.c
index 245fc17fee..3958720f00 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -197,6 +197,12 @@ static void diffsize_consume(void *data, char *line, unsigned long len)
 	(*(int *)data)++;
 }
 
+static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
+			  const char *funcline, long funclen)
+{
+	diffsize_consume(data, NULL, 0);
+}
+
 static int diffsize(const char *a, const char *b)
 {
 	xpparam_t pp = { 0 };
@@ -210,7 +216,9 @@ static int diffsize(const char *a, const char *b)
 	mf2.size = strlen(b);
 
 	cfg.ctxlen = 3;
-	if (!xdi_diff_outf(&mf1, &mf2, NULL, diffsize_consume, &count, &pp, &cfg))
+	if (!xdi_diff_outf(&mf1, &mf2,
+			   diffsize_hunk, diffsize_consume, &count,
+			   &pp, &cfg))
 		return count;
 
 	error(_("failed to generate diff"));
-- 
2.19.1.1336.g081079ac04
Previous: Jeff KingNext: Jeff King
Message 12 of 14 in “saner xdiff hunk callbacks”
  1. 0/9 saner xdiff hunk callbacksJeff King, Nov 2, 2018
  2. 1/9 xdiff: provide a separate emit callback for hunksJeff King, Nov 2, 2018
  3. 2/9 xdiff-interface: provide a separate consume callback for hunksJeff King, Nov 2, 2018
  4. 3/9 diff: avoid generating unused hunk header linesJeff King, Nov 2, 2018
  5. Stefan BellerNov 2, 2018
  6. Jeff KingNov 2, 2018
  7. Junio C HamanoNov 3, 2018
  8. 4/9 diff: discard hunk headers for patch-ids earlierJeff King, Nov 2, 2018
  9. 5/9 diff: use hunk callback for word-diffJeff King, Nov 2, 2018
  10. 6/9 combine-diff: use an xdiff hunk callbackJeff King, Nov 2, 2018
  11. 7/9 diff: convert --check to use a hunk callbackJeff King, Nov 2, 2018
  12. 8/9 range-diff: use a hunk callbackJeff King, Nov 2, 2018
  13. 9/9 xdiff-interface: drop parse_hunk_header()Jeff King, Nov 2, 2018
  14. Junio C HamanoNov 2, 2018

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.