Skip to content

Commit 79cb2eb

Browse files
stefanbellergitster
authored andcommitted
xdiff/histogram: remove tail recursion
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 64c4e8b commit 79cb2eb

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

xdiff/xhistogram.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
318318
{
319319
struct region lcs;
320320
int lcs_found;
321-
int result = -1;
321+
int result;
322+
redo:
323+
result = -1;
322324

323325
if (count1 <= 0 && count2 <= 0)
324326
return 0;
@@ -355,11 +357,17 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
355357
line2, lcs.begin2 - line2);
356358
if (result)
357359
goto out;
358-
result = histogram_diff(xpp, env,
359-
lcs.end1 + 1, LINE_END(1) - lcs.end1,
360-
lcs.end2 + 1, LINE_END(2) - lcs.end2);
361-
if (result)
362-
goto out;
360+
/*
361+
* result = histogram_diff(xpp, env,
362+
* lcs.end1 + 1, LINE_END(1) - lcs.end1,
363+
* lcs.end2 + 1, LINE_END(2) - lcs.end2);
364+
* but let's optimize tail recursion ourself:
365+
*/
366+
count1 = LINE_END(1) - lcs.end1;
367+
line1 = lcs.end1 + 1;
368+
count2 = LINE_END(2) - lcs.end2;
369+
line2 = lcs.end2 + 1;
370+
goto redo;
363371
}
364372
}
365373
out:

0 commit comments

Comments
 (0)