Skip to content

Commit f37b9bc

Browse files
rscharfegitster
authored andcommitted
replace_object: use oidmap
Load the replace objects into an oidmap to allow for easy lookups in constant time. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent fe0a9ea commit f37b9bc

File tree

1 file changed

+16
-60
lines changed

1 file changed

+16
-60
lines changed

replace_object.c

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,14 @@
11
#include "cache.h"
2-
#include "sha1-lookup.h"
2+
#include "oidmap.h"
33
#include "refs.h"
44
#include "commit.h"
55

6-
/*
7-
* An array of replacements. The array is kept sorted by the original
8-
* sha1.
9-
*/
10-
static struct replace_object {
11-
struct object_id original;
6+
struct replace_object {
7+
struct oidmap_entry original;
128
struct object_id replacement;
13-
} **replace_object;
14-
15-
static int replace_object_alloc, replace_object_nr;
9+
};
1610

17-
static const unsigned char *replace_sha1_access(size_t index, void *table)
18-
{
19-
struct replace_object **replace = table;
20-
return replace[index]->original.hash;
21-
}
22-
23-
static int replace_object_pos(const unsigned char *sha1)
24-
{
25-
return sha1_pos(sha1, replace_object, replace_object_nr,
26-
replace_sha1_access);
27-
}
28-
29-
static int register_replace_object(struct replace_object *replace,
30-
int ignore_dups)
31-
{
32-
int pos = replace_object_pos(replace->original.hash);
33-
34-
if (0 <= pos) {
35-
if (ignore_dups)
36-
free(replace);
37-
else {
38-
free(replace_object[pos]);
39-
replace_object[pos] = replace;
40-
}
41-
return 1;
42-
}
43-
pos = -pos - 1;
44-
ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
45-
replace_object_nr++;
46-
if (pos < replace_object_nr)
47-
MOVE_ARRAY(replace_object + pos + 1, replace_object + pos,
48-
replace_object_nr - pos - 1);
49-
replace_object[pos] = replace;
50-
return 0;
51-
}
11+
static struct oidmap replace_map = OIDMAP_INIT;
5212

5313
static int register_replace_ref(const char *refname,
5414
const struct object_id *oid,
@@ -59,7 +19,7 @@ static int register_replace_ref(const char *refname,
5919
const char *hash = slash ? slash + 1 : refname;
6020
struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
6121

62-
if (get_oid_hex(hash, &repl_obj->original)) {
22+
if (get_oid_hex(hash, &repl_obj->original.oid)) {
6323
free(repl_obj);
6424
warning("bad replace ref name: %s", refname);
6525
return 0;
@@ -69,7 +29,7 @@ static int register_replace_ref(const char *refname,
6929
oidcpy(&repl_obj->replacement, oid);
7030

7131
/* Register new object */
72-
if (register_replace_object(repl_obj, 1))
32+
if (oidmap_put(&replace_map, repl_obj))
7333
die("duplicate replace ref: %s", refname);
7434

7535
return 0;
@@ -84,7 +44,7 @@ static void prepare_replace_object(void)
8444

8545
for_each_replace_ref(register_replace_ref, NULL);
8646
replace_object_prepared = 1;
87-
if (!replace_object_nr)
47+
if (!replace_map.map.tablesize)
8848
check_replace_refs = 0;
8949
}
9050

@@ -100,21 +60,17 @@ static void prepare_replace_object(void)
10060
*/
10161
const struct object_id *do_lookup_replace_object(const struct object_id *oid)
10262
{
103-
int pos, depth = MAXREPLACEDEPTH;
63+
int depth = MAXREPLACEDEPTH;
10464
const struct object_id *cur = oid;
10565

10666
prepare_replace_object();
10767

10868
/* Try to recursively replace the object */
109-
do {
110-
if (--depth < 0)
111-
die("replace depth too high for object %s",
112-
oid_to_hex(oid));
113-
114-
pos = replace_object_pos(cur->hash);
115-
if (0 <= pos)
116-
cur = &replace_object[pos]->replacement;
117-
} while (0 <= pos);
118-
119-
return cur;
69+
while (depth-- > 0) {
70+
struct replace_object *repl_obj = oidmap_get(&replace_map, cur);
71+
if (!repl_obj)
72+
return cur;
73+
cur = &repl_obj->replacement;
74+
}
75+
die("replace depth too high for object %s", oid_to_hex(oid));
12076
}

0 commit comments

Comments
 (0)