@matheusbsbs / matheus-bagliano / commits / 9fb88419ba
Fork 0

Ref-count the filespecs used by diffcore

Rather than copy the filespecs when introducing new versions of them (for rename or copy detection), use a refcount and increment the count when reusing the diff_filespec. This avoids unnecessary allocations, but the real reason behind this is a future enhancement: we will want to track shared data across the copy/rename detection. In order to efficiently notice when a filespec is used by a rename, the rename machinery wants to keep track of a rename usage count which is shared across all different users of the filespec. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Oct 25, 2007 at 11:19 UTC 9fb88419ba85e641006c80db53620423f37f1c93
3 files changed +19 -14
diff.c
+11 -4
@@ -1440,9 +1440,18 @@ struct diff_filespec *alloc_filespec(const char *path)
1440 memset(spec, 0, sizeof(*spec));
1441 spec->path = (char *)(spec + 1);
1442 memcpy(spec->path, path, namelen+1);
1443 + spec->count = 1;
1444 return spec;
1445 }
1446
1447 +void free_filespec(struct diff_filespec *spec)
1448 +{
1449 + if (!--spec->count) {
1450 + diff_free_filespec_data(spec);
1451 + free(spec);
1452 + }
1453 +}
1454 +
1455 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1456 unsigned short mode)
1457 {
@@ -2435,10 +2444,8 @@ struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2444
2445 void diff_free_filepair(struct diff_filepair *p)
2446 {
2438 - diff_free_filespec_data(p->one);
2439 - diff_free_filespec_data(p->two);
2440 - free(p->one);
2441 - free(p->two);
2447 + free_filespec(p->one);
2448 + free_filespec(p->two);
2449 free(p);
2450 }
2451
diffcore-rename.c
+6 -10
@@ -209,21 +209,19 @@ static int estimate_similarity(struct diff_filespec *src,
209
210 static void record_rename_pair(int dst_index, int src_index, int score)
211 {
212 - struct diff_filespec *one, *two, *src, *dst;
212 + struct diff_filespec *src, *dst;
213 struct diff_filepair *dp;
214
215 if (rename_dst[dst_index].pair)
216 die("internal error: dst already matched.");
217
218 src = rename_src[src_index].one;
219 - one = alloc_filespec(src->path);
220 - fill_filespec(one, src->sha1, src->mode);
219 + src->count++;
220
221 dst = rename_dst[dst_index].two;
223 - two = alloc_filespec(dst->path);
224 - fill_filespec(two, dst->sha1, dst->mode);
222 + dst->count++;
223
226 - dp = diff_queue(NULL, one, two);
224 + dp = diff_queue(NULL, src, dst);
225 dp->renamed_pair = 1;
226 if (!strcmp(src->path, dst->path))
227 dp->score = rename_src[src_index].score;
@@ -526,10 +524,8 @@ void diffcore_rename(struct diff_options *options)
524 }
525 }
526
529 - for (i = 0; i < rename_dst_nr; i++) {
530 - diff_free_filespec_data(rename_dst[i].two);
531 - free(rename_dst[i].two);
532 - }
527 + for (i = 0; i < rename_dst_nr; i++)
528 + free_filespec(rename_dst[i].two);
529
530 free(rename_dst);
531 rename_dst = NULL;
diffcore.h
+2
@@ -29,6 +29,7 @@ struct diff_filespec {
29 void *cnt_data;
30 const char *funcname_pattern_ident;
31 unsigned long size;
32 + int count; /* Reference count */
33 int xfrm_flags; /* for use by the xfrm */
34 unsigned short mode; /* file mode */
35 unsigned sha1_valid : 1; /* if true, use sha1 and trust mode;
@@ -43,6 +44,7 @@ struct diff_filespec {
44 };
45
46 extern struct diff_filespec *alloc_filespec(const char *);
47 +extern void free_filespec(struct diff_filespec *);
48 extern void fill_filespec(struct diff_filespec *, const unsigned char *,
49 unsigned short);
50