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

[PATCH] xdiff/histogram: remove tail recursion

From
Stefan Beller <sbeller@google.com>
Date
Jul 19, 2018, 22:19 UTC
Message-ID
<20180719221942.179565-1-sbeller@google.com>
In-Reply-To
<20180719185620.124768-4-sbeller@google.com>

When running the same reproduction script as the previous patch, it turns out the stack is too small, which can be easily avoided.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 xdiff/xhistogram.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
index fc2d3cd95d9..ec85f5992bd 100644
--- a/xdiff/xhistogram.c
+++ b/xdiff/xhistogram.c
@@ -318,7 +318,9 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
 {
 	struct region lcs;
 	int lcs_found;
-	int result = -1;
+	int result;
+redo:
+	result = -1;
 
 	if (count1 <= 0 && count2 <= 0)
 		return 0;
@@ -355,11 +357,17 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
 						line2, lcs.begin2 - line2);
 			if (result)
 				goto out;
-			result = histogram_diff(xpp, env,
-						lcs.end1 + 1, LINE_END(1) - lcs.end1,
-						lcs.end2 + 1, LINE_END(2) - lcs.end2);
-			if (result)
-				goto out;
+			/*
+			 * result = histogram_diff(xpp, env,
+			 *            lcs.end1 + 1, LINE_END(1) - lcs.end1,
+			 *            lcs.end2 + 1, LINE_END(2) - lcs.end2);
+			 * but let's optimize tail recursion ourself:
+			*/
+			count1 = LINE_END(1) - lcs.end1;
+			line1 = lcs.end1 + 1;
+			count2 = LINE_END(2) - lcs.end2;
+			line2 = lcs.end2 + 1;
+			goto redo;
 		}
 	}
 out:
-- 
2.18.0.233.g985f88cf7e-goog
Previous: Stefan Beller
Message 5 of 5 in “histogram diff: Fix memory ballooning in corner case”
  1. 0/3 histogram diff: Fix memory ballooning in corner caseStefan Beller, Jul 19, 2018
  2. 1/3 xdiff/xhistogram: pass arguments directly to fall_back_to_classic_diffStefan Beller, Jul 19, 2018
  3. 2/3 xdiff/xhistogram: factor out memory cleanup into free_index()Stefan Beller, Jul 19, 2018
  4. 3/3 xdiff/xhistogram: move index allocation into find_lcsStefan Beller, Jul 19, 2018
  5. xdiff/histogram: remove tail recursionStefan Beller, Jul 19, 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.