@matheusbsbs / matheus-bagliano / commits / eede7b7d11
Fork 0

diffcore-rename: cache file deltas

We find rename candidates by computing a fingerprint hash of each file, and then comparing those fingerprints. There are inherently O(n^2) comparisons, so it pays in CPU time to hoist the (rather expensive) computation of the fingerprint out of that loop (or to cache it once we have computed it once). Previously, we didn't keep the filespec information around because then we had the potential to consume a great deal of memory. However, instead of keeping all of the filespec data, we can instead just keep the fingerprint. This patch implements and uses diff_free_filespec_data_large to accomplish that goal. We also have to change estimate_similarity not to needlessly repopulate the filespec data when we already have the hash. Practical tests showed 4.5x speedup for a 10% memory usage increase. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 25, 2007 at 15:29 UTC eede7b7d110e2c354235d7a3f6c8f1644b5120e5
3 files changed +11 -4
diff.c
+6 -1
@@ -1675,7 +1675,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1675 return 0;
1676 }
1677
1678 -void diff_free_filespec_data(struct diff_filespec *s)
1678 +void diff_free_filespec_data_large(struct diff_filespec *s)
1679 {
1680 if (s->should_free)
1681 free(s->data);
@@ -1686,6 +1686,11 @@ void diff_free_filespec_data(struct diff_filespec *s)
1686 s->should_free = s->should_munmap = 0;
1687 s->data = NULL;
1688 }
1689 +}
1690 +
1691 +void diff_free_filespec_data(struct diff_filespec *s)
1692 +{
1693 + diff_free_filespec_data_large(s);
1694 free(s->cnt_data);
1695 s->cnt_data = NULL;
1696 }
diffcore-rename.c
+4 -3
@@ -184,7 +184,8 @@ static int estimate_similarity(struct diff_filespec *src,
184 if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
185 return 0;
186
187 - if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
187 + if ((!src->cnt_data && diff_populate_filespec(src, 0))
188 + || (!dst->cnt_data && diff_populate_filespec(dst, 0)))
189 return 0; /* error but caught downstream */
190
191
@@ -377,10 +378,10 @@ void diffcore_rename(struct diff_options *options)
378 m->score = estimate_similarity(one, two,
379 minimum_score);
380 m->name_score = basename_same(one, two);
380 - diff_free_filespec_data(one);
381 + diff_free_filespec_data_large(one);
382 }
383 /* We do not need the text anymore */
383 - diff_free_filespec_data(two);
384 + diff_free_filespec_data_large(two);
385 dst_cnt++;
386 }
387 /* cost matrix sorted by most to least similar pair */
diffcore.h
+1
@@ -48,6 +48,7 @@ extern void fill_filespec(struct diff_filespec *, const unsigned char *,
48
49 extern int diff_populate_filespec(struct diff_filespec *, int);
50 extern void diff_free_filespec_data(struct diff_filespec *);
51 +extern void diff_free_filespec_data_large(struct diff_filespec *);
52 extern int diff_filespec_is_binary(struct diff_filespec *);
53
54 struct diff_filepair {