Skip to content

Commit 680ff91

Browse files
peffgitster
authored andcommitted
rerere: use strmap to store rerere directories
We store a struct for each directory we access under .git/rr-cache. The structs are kept in an array sorted by the binary hash associated with their name (and we do lookups with a binary search). This works OK, but there are a few small downsides: - the amount of code isn't huge, but it's more than we'd need using one of our other stock data structures - the insertion into a sorted array is quadratic (though in practice it's unlikely anybody has enough conflicts for this to matter) - it's intimately tied to the representation of an object hash. This isn't a big deal, as the conflict ids we generate use the same hash, but it produces a few awkward bits (e.g., we are the only user of hash_pos() that is not using object_id). Let's instead just treat the directory names as strings, and store them in a strmap. This is less code, and removes the use of hash_pos(). Insertion is now non-quadratic, though we probably use a bit more memory. Besides the hash table overhead, and storing hex bytes instead of a binary hash, we actually store each name twice. Other code expects to access the name of a rerere_dir struct from the struct itself, so we need a copy there. But strmap keeps its own copy of the name, as well. Using a bare hashmap instead of strmap means we could use the name for both, but at the cost of extra code (e.g., our own comparison function). Likewise, strmap has a feature to use a pointer to the in-struct name at the cost of a little extra code. I didn't do either here, as simple code seemed more important than squeezing out a few bytes of efficiency. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 098c173 commit 680ff91

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

rerere.c

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pathspec.h"
1212
#include "object-store.h"
1313
#include "hash-lookup.h"
14+
#include "strmap.h"
1415

1516
#define RESOLVED 0
1617
#define PUNTED 1
@@ -23,26 +24,27 @@ static int rerere_enabled = -1;
2324
/* automatically update cleanly resolved paths to the index */
2425
static int rerere_autoupdate;
2526

26-
static int rerere_dir_nr;
27-
static int rerere_dir_alloc;
28-
2927
#define RR_HAS_POSTIMAGE 1
3028
#define RR_HAS_PREIMAGE 2
31-
static struct rerere_dir {
32-
unsigned char hash[GIT_MAX_HEXSZ];
29+
struct rerere_dir {
3330
int status_alloc, status_nr;
3431
unsigned char *status;
35-
} **rerere_dir;
32+
char name[FLEX_ARRAY];
33+
};
34+
35+
static struct strmap rerere_dirs = STRMAP_INIT;
3636

3737
static void free_rerere_dirs(void)
3838
{
39-
int i;
40-
for (i = 0; i < rerere_dir_nr; i++) {
41-
free(rerere_dir[i]->status);
42-
free(rerere_dir[i]);
39+
struct hashmap_iter iter;
40+
struct strmap_entry *ent;
41+
42+
strmap_for_each_entry(&rerere_dirs, &iter, ent) {
43+
struct rerere_dir *rr_dir = ent->value;
44+
free(rr_dir->status);
45+
free(rr_dir);
4346
}
44-
FREE_AND_NULL(rerere_dir);
45-
rerere_dir_nr = rerere_dir_alloc = 0;
47+
strmap_clear(&rerere_dirs, 0);
4648
}
4749

4850
static void free_rerere_id(struct string_list_item *item)
@@ -52,7 +54,7 @@ static void free_rerere_id(struct string_list_item *item)
5254

5355
static const char *rerere_id_hex(const struct rerere_id *id)
5456
{
55-
return hash_to_hex(id->collection->hash);
57+
return id->collection->name;
5658
}
5759

5860
static void fit_variant(struct rerere_dir *rr_dir, int variant)
@@ -115,7 +117,7 @@ static int is_rr_file(const char *name, const char *filename, int *variant)
115117
static void scan_rerere_dir(struct rerere_dir *rr_dir)
116118
{
117119
struct dirent *de;
118-
DIR *dir = opendir(git_path("rr-cache/%s", hash_to_hex(rr_dir->hash)));
120+
DIR *dir = opendir(git_path("rr-cache/%s", rr_dir->name));
119121

120122
if (!dir)
121123
return;
@@ -133,39 +135,21 @@ static void scan_rerere_dir(struct rerere_dir *rr_dir)
133135
closedir(dir);
134136
}
135137

136-
static const unsigned char *rerere_dir_hash(size_t i, void *table)
137-
{
138-
struct rerere_dir **rr_dir = table;
139-
return rr_dir[i]->hash;
140-
}
141-
142138
static struct rerere_dir *find_rerere_dir(const char *hex)
143139
{
144-
unsigned char hash[GIT_MAX_RAWSZ];
145140
struct rerere_dir *rr_dir;
146-
int pos;
147-
148-
if (get_sha1_hex(hex, hash))
149-
BUG("cannot parse rerere dir hex?");
150-
pos = hash_pos(hash, rerere_dir, rerere_dir_nr, rerere_dir_hash);
151-
if (pos < 0) {
152-
rr_dir = xmalloc(sizeof(*rr_dir));
153-
hashcpy(rr_dir->hash, hash);
141+
142+
rr_dir = strmap_get(&rerere_dirs, hex);
143+
if (!rr_dir) {
144+
FLEX_ALLOC_STR(rr_dir, name, hex);
154145
rr_dir->status = NULL;
155146
rr_dir->status_nr = 0;
156147
rr_dir->status_alloc = 0;
157-
pos = -1 - pos;
158-
159-
/* Make sure the array is big enough ... */
160-
ALLOC_GROW(rerere_dir, rerere_dir_nr + 1, rerere_dir_alloc);
161-
/* ... and add it in. */
162-
rerere_dir_nr++;
163-
MOVE_ARRAY(rerere_dir + pos + 1, rerere_dir + pos,
164-
rerere_dir_nr - pos - 1);
165-
rerere_dir[pos] = rr_dir;
148+
strmap_put(&rerere_dirs, hex, rr_dir);
149+
166150
scan_rerere_dir(rr_dir);
167151
}
168-
return rerere_dir[pos];
152+
return rr_dir;
169153
}
170154

171155
static int has_rerere_resolution(const struct rerere_id *id)

0 commit comments

Comments
 (0)