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>

Stefan Beller committed Jul 19, 2018 at 15:19 UTC 79cb2ebb92c18af11edf5eea238425d86eef173d
1 file changed +14 -6
xdiff/xhistogram.c
+14 -6
@@ -318,7 +318,9 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
318 {
319 struct region lcs;
320 int lcs_found;
321 - int result = -1;
321 + int result;
322 +redo:
323 + result = -1;
324
325 if (count1 <= 0 && count2 <= 0)
326 return 0;
@@ -355,11 +357,17 @@ static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
357 line2, lcs.begin2 - line2);
358 if (result)
359 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;
371 }
372 }
373 out: