Skip to content

Commit a5ac31b

Browse files
committed
Merge branch 'en/diffcore-rename'
File-level rename detection updates. * en/diffcore-rename: diffcore-rename: remove unnecessary duplicate entry checks diffcore-rename: accelerate rename_dst setup diffcore-rename: simplify and accelerate register_rename_src() t4058: explore duplicate tree entry handling in a bit more detail t4058: add more tests and documentation for duplicate tree entry handling diffcore-rename: reduce jumpiness in progress counters diffcore-rename: simplify limit check diffcore-rename: avoid usage of global in too_many_rename_candidates() diffcore-rename: rename num_create to num_destinations
2 parents 58e2ce9 + 350410f commit a5ac31b

File tree

2 files changed

+192
-131
lines changed

2 files changed

+192
-131
lines changed

diffcore-rename.c

Lines changed: 80 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,36 @@
99
#include "hashmap.h"
1010
#include "progress.h"
1111
#include "promisor-remote.h"
12+
#include "strmap.h"
1213

1314
/* Table of rename/copy destinations */
1415

1516
static struct diff_rename_dst {
16-
struct diff_filespec *two;
17-
struct diff_filepair *pair;
17+
struct diff_filepair *p;
18+
struct diff_filespec *filespec_to_free;
19+
int is_rename; /* false -> just a create; true -> rename or copy */
1820
} *rename_dst;
1921
static int rename_dst_nr, rename_dst_alloc;
22+
/* Mapping from break source pathname to break destination index */
23+
static struct strintmap *break_idx = NULL;
2024

21-
static int find_rename_dst(struct diff_filespec *two)
22-
{
23-
int first, last;
24-
25-
first = 0;
26-
last = rename_dst_nr;
27-
while (last > first) {
28-
int next = first + ((last - first) >> 1);
29-
struct diff_rename_dst *dst = &(rename_dst[next]);
30-
int cmp = strcmp(two->path, dst->two->path);
31-
if (!cmp)
32-
return next;
33-
if (cmp < 0) {
34-
last = next;
35-
continue;
36-
}
37-
first = next+1;
38-
}
39-
return -first - 1;
40-
}
41-
42-
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two)
25+
static struct diff_rename_dst *locate_rename_dst(struct diff_filepair *p)
4326
{
44-
int ofs = find_rename_dst(two);
45-
return ofs < 0 ? NULL : &rename_dst[ofs];
27+
/* Lookup by p->ONE->path */
28+
int idx = break_idx ? strintmap_get(break_idx, p->one->path) : -1;
29+
return (idx == -1) ? NULL : &rename_dst[idx];
4630
}
4731

4832
/*
4933
* Returns 0 on success, -1 if we found a duplicate.
5034
*/
51-
static int add_rename_dst(struct diff_filespec *two)
35+
static int add_rename_dst(struct diff_filepair *p)
5236
{
53-
int first = find_rename_dst(two);
54-
55-
if (first >= 0)
56-
return -1;
57-
first = -first - 1;
58-
59-
/* insert to make it at "first" */
6037
ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
38+
rename_dst[rename_dst_nr].p = p;
39+
rename_dst[rename_dst_nr].filespec_to_free = NULL;
40+
rename_dst[rename_dst_nr].is_rename = 0;
6141
rename_dst_nr++;
62-
if (first < rename_dst_nr)
63-
MOVE_ARRAY(rename_dst + first + 1, rename_dst + first,
64-
rename_dst_nr - first - 1);
65-
rename_dst[first].two = alloc_filespec(two->path);
66-
fill_filespec(rename_dst[first].two, &two->oid, two->oid_valid,
67-
two->mode);
68-
rename_dst[first].pair = NULL;
6942
return 0;
7043
}
7144

@@ -76,36 +49,20 @@ static struct diff_rename_src {
7649
} *rename_src;
7750
static int rename_src_nr, rename_src_alloc;
7851

79-
static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
52+
static void register_rename_src(struct diff_filepair *p)
8053
{
81-
int first, last;
82-
struct diff_filespec *one = p->one;
83-
unsigned short score = p->score;
84-
85-
first = 0;
86-
last = rename_src_nr;
87-
while (last > first) {
88-
int next = first + ((last - first) >> 1);
89-
struct diff_rename_src *src = &(rename_src[next]);
90-
int cmp = strcmp(one->path, src->p->one->path);
91-
if (!cmp)
92-
return src;
93-
if (cmp < 0) {
94-
last = next;
95-
continue;
54+
if (p->broken_pair) {
55+
if (!break_idx) {
56+
break_idx = xmalloc(sizeof(*break_idx));
57+
strintmap_init(break_idx, -1);
9658
}
97-
first = next+1;
59+
strintmap_set(break_idx, p->one->path, rename_dst_nr);
9860
}
9961

100-
/* insert to make it at "first" */
10162
ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
63+
rename_src[rename_src_nr].p = p;
64+
rename_src[rename_src_nr].score = p->score;
10265
rename_src_nr++;
103-
if (first < rename_src_nr)
104-
MOVE_ARRAY(rename_src + first + 1, rename_src + first,
105-
rename_src_nr - first - 1);
106-
rename_src[first].p = p;
107-
rename_src[first].score = score;
108-
return &(rename_src[first]);
10966
}
11067

11168
static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
@@ -141,14 +98,14 @@ static void prefetch(void *prefetch_options)
14198
struct oid_array to_fetch = OID_ARRAY_INIT;
14299

143100
for (i = 0; i < rename_dst_nr; i++) {
144-
if (rename_dst[i].pair)
101+
if (rename_dst[i].p->renamed_pair)
145102
/*
146103
* The loop in diffcore_rename() will not need these
147104
* blobs, so skip prefetching.
148105
*/
149106
continue; /* already found exact match */
150107
diff_add_if_missing(options->repo, &to_fetch,
151-
rename_dst[i].two);
108+
rename_dst[i].p->two);
152109
}
153110
for (i = 0; i < rename_src_nr; i++) {
154111
if (options->skip_unmodified &&
@@ -258,26 +215,24 @@ static int estimate_similarity(struct repository *r,
258215

259216
static void record_rename_pair(int dst_index, int src_index, int score)
260217
{
261-
struct diff_filespec *src, *dst;
262-
struct diff_filepair *dp;
218+
struct diff_filepair *src = rename_src[src_index].p;
219+
struct diff_filepair *dst = rename_dst[dst_index].p;
263220

264-
if (rename_dst[dst_index].pair)
221+
if (dst->renamed_pair)
265222
die("internal error: dst already matched.");
266223

267-
src = rename_src[src_index].p->one;
268-
src->rename_used++;
269-
src->count++;
224+
src->one->rename_used++;
225+
src->one->count++;
270226

271-
dst = rename_dst[dst_index].two;
272-
dst->count++;
227+
rename_dst[dst_index].filespec_to_free = dst->one;
228+
rename_dst[dst_index].is_rename = 1;
273229

274-
dp = diff_queue(NULL, src, dst);
275-
dp->renamed_pair = 1;
276-
if (!strcmp(src->path, dst->path))
277-
dp->score = rename_src[src_index].score;
230+
dst->one = src->one;
231+
dst->renamed_pair = 1;
232+
if (!strcmp(dst->one->path, dst->two->path))
233+
dst->score = rename_src[src_index].score;
278234
else
279-
dp->score = score;
280-
rename_dst[dst_index].pair = dp;
235+
dst->score = score;
281236
}
282237

283238
/*
@@ -323,7 +278,7 @@ static int find_identical_files(struct hashmap *srcs,
323278
struct diff_options *options)
324279
{
325280
int renames = 0;
326-
struct diff_filespec *target = rename_dst[dst_index].two;
281+
struct diff_filespec *target = rename_dst[dst_index].p->two;
327282
struct file_similarity *p, *best = NULL;
328283
int i = 100, best_score = -1;
329284
unsigned int hash = hash_filespec(options->repo, target);
@@ -434,44 +389,46 @@ static void record_if_better(struct diff_score m[], struct diff_score *o)
434389
* 1 if we need to disable inexact rename detection;
435390
* 2 if we would be under the limit if we were given -C instead of -C -C.
436391
*/
437-
static int too_many_rename_candidates(int num_create,
392+
static int too_many_rename_candidates(int num_destinations, int num_sources,
438393
struct diff_options *options)
439394
{
440395
int rename_limit = options->rename_limit;
441-
int num_src = rename_src_nr;
442-
int i;
396+
int i, limited_sources;
443397

444398
options->needed_rename_limit = 0;
445399

446400
/*
447401
* This basically does a test for the rename matrix not
448402
* growing larger than a "rename_limit" square matrix, ie:
449403
*
450-
* num_create * num_src > rename_limit * rename_limit
404+
* num_destinations * num_sources > rename_limit * rename_limit
405+
*
406+
* We use st_mult() to check overflow conditions; in the
407+
* exceptional circumstance that size_t isn't large enough to hold
408+
* the multiplication, the system won't be able to allocate enough
409+
* memory for the matrix anyway.
451410
*/
452411
if (rename_limit <= 0)
453412
rename_limit = 32767;
454-
if ((num_create <= rename_limit || num_src <= rename_limit) &&
455-
((uint64_t)num_create * (uint64_t)num_src
456-
<= (uint64_t)rename_limit * (uint64_t)rename_limit))
413+
if (st_mult(num_destinations, num_sources)
414+
<= st_mult(rename_limit, rename_limit))
457415
return 0;
458416

459417
options->needed_rename_limit =
460-
num_src > num_create ? num_src : num_create;
418+
num_sources > num_destinations ? num_sources : num_destinations;
461419

462420
/* Are we running under -C -C? */
463421
if (!options->flags.find_copies_harder)
464422
return 1;
465423

466424
/* Would we bust the limit if we were running under -C? */
467-
for (num_src = i = 0; i < rename_src_nr; i++) {
425+
for (limited_sources = i = 0; i < num_sources; i++) {
468426
if (diff_unmodified_pair(rename_src[i].p))
469427
continue;
470-
num_src++;
428+
limited_sources++;
471429
}
472-
if ((num_create <= rename_limit || num_src <= rename_limit) &&
473-
((uint64_t)num_create * (uint64_t)num_src
474-
<= (uint64_t)rename_limit * (uint64_t)rename_limit))
430+
if (st_mult(num_destinations, limited_sources)
431+
<= st_mult(rename_limit, rename_limit))
475432
return 2;
476433
return 1;
477434
}
@@ -487,7 +444,7 @@ static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, i
487444
(mx[i].score < minimum_score))
488445
break; /* there is no more usable pair. */
489446
dst = &rename_dst[mx[i].dst];
490-
if (dst->pair)
447+
if (dst->is_rename)
491448
continue; /* already done, either exact or fuzzy. */
492449
if (!copies && rename_src[mx[i].src].p->one->rename_used)
493450
continue;
@@ -505,7 +462,7 @@ void diffcore_rename(struct diff_options *options)
505462
struct diff_queue_struct outq;
506463
struct diff_score *mx;
507464
int i, j, rename_count, skip_unmodified = 0;
508-
int num_create, dst_cnt;
465+
int num_destinations, dst_cnt;
509466
struct progress *progress = NULL;
510467

511468
if (!minimum_score)
@@ -522,7 +479,7 @@ void diffcore_rename(struct diff_options *options)
522479
else if (!options->flags.rename_empty &&
523480
is_empty_blob_oid(&p->two->oid))
524481
continue;
525-
else if (add_rename_dst(p->two) < 0) {
482+
else if (add_rename_dst(p) < 0) {
526483
warning("skipping rename detection, detected"
527484
" duplicate destination '%s'",
528485
p->two->path);
@@ -570,13 +527,14 @@ void diffcore_rename(struct diff_options *options)
570527
* Calculate how many renames are left (but all the source
571528
* files still remain as options for rename/copies!)
572529
*/
573-
num_create = (rename_dst_nr - rename_count);
530+
num_destinations = (rename_dst_nr - rename_count);
574531

575532
/* All done? */
576-
if (!num_create)
533+
if (!num_destinations)
577534
goto cleanup;
578535

579-
switch (too_many_rename_candidates(num_create, options)) {
536+
switch (too_many_rename_candidates(num_destinations, rename_src_nr,
537+
options)) {
580538
case 1:
581539
goto cleanup;
582540
case 2:
@@ -590,15 +548,16 @@ void diffcore_rename(struct diff_options *options)
590548
if (options->show_rename_progress) {
591549
progress = start_delayed_progress(
592550
_("Performing inexact rename detection"),
593-
(uint64_t)rename_dst_nr * (uint64_t)rename_src_nr);
551+
(uint64_t)num_destinations * (uint64_t)rename_src_nr);
594552
}
595553

596-
mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_create), sizeof(*mx));
554+
mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_destinations),
555+
sizeof(*mx));
597556
for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
598-
struct diff_filespec *two = rename_dst[i].two;
557+
struct diff_filespec *two = rename_dst[i].p->two;
599558
struct diff_score *m;
600559

601-
if (rename_dst[i].pair)
560+
if (rename_dst[i].is_rename)
602561
continue; /* dealt with exact match already. */
603562

604563
m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
@@ -629,7 +588,8 @@ void diffcore_rename(struct diff_options *options)
629588
diff_free_filespec_blob(two);
630589
}
631590
dst_cnt++;
632-
display_progress(progress, (uint64_t)(i+1)*(uint64_t)rename_src_nr);
591+
display_progress(progress,
592+
(uint64_t)dst_cnt * (uint64_t)rename_src_nr);
633593
}
634594
stop_progress(&progress);
635595

@@ -654,22 +614,8 @@ void diffcore_rename(struct diff_options *options)
654614
diff_q(&outq, p);
655615
}
656616
else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
657-
/*
658-
* Creation
659-
*
660-
* We would output this create record if it has
661-
* not been turned into a rename/copy already.
662-
*/
663-
struct diff_rename_dst *dst = locate_rename_dst(p->two);
664-
if (dst && dst->pair) {
665-
diff_q(&outq, dst->pair);
666-
pair_to_free = p;
667-
}
668-
else
669-
/* no matching rename/copy source, so
670-
* record this as a creation.
671-
*/
672-
diff_q(&outq, p);
617+
/* Creation */
618+
diff_q(&outq, p);
673619
}
674620
else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
675621
/*
@@ -690,8 +636,10 @@ void diffcore_rename(struct diff_options *options)
690636
*/
691637
if (DIFF_PAIR_BROKEN(p)) {
692638
/* broken delete */
693-
struct diff_rename_dst *dst = locate_rename_dst(p->one);
694-
if (dst && dst->pair)
639+
struct diff_rename_dst *dst = locate_rename_dst(p);
640+
if (!dst)
641+
BUG("tracking failed somehow; failed to find associated dst for broken pair");
642+
if (dst->is_rename)
695643
/* counterpart is now rename/copy */
696644
pair_to_free = p;
697645
}
@@ -701,16 +649,14 @@ void diffcore_rename(struct diff_options *options)
701649
pair_to_free = p;
702650
}
703651

704-
if (pair_to_free)
705-
;
706-
else
652+
if (!pair_to_free)
707653
diff_q(&outq, p);
708654
}
709655
else if (!diff_unmodified_pair(p))
710656
/* all the usual ones need to be kept */
711657
diff_q(&outq, p);
712658
else
713-
/* no need to keep unmodified pairs */
659+
/* no need to keep unmodified pairs; FIXME: remove earlier? */
714660
pair_to_free = p;
715661

716662
if (pair_to_free)
@@ -723,11 +669,16 @@ void diffcore_rename(struct diff_options *options)
723669
diff_debug_queue("done collapsing", q);
724670

725671
for (i = 0; i < rename_dst_nr; i++)
726-
free_filespec(rename_dst[i].two);
672+
if (rename_dst[i].filespec_to_free)
673+
free_filespec(rename_dst[i].filespec_to_free);
727674

728675
FREE_AND_NULL(rename_dst);
729676
rename_dst_nr = rename_dst_alloc = 0;
730677
FREE_AND_NULL(rename_src);
731678
rename_src_nr = rename_src_alloc = 0;
679+
if (break_idx) {
680+
strintmap_clear(break_idx);
681+
FREE_AND_NULL(break_idx);
682+
}
732683
return;
733684
}

0 commit comments

Comments
 (0)