Skip to content

Commit f1782d1

Browse files
pks-tgitster
authored andcommitted
refs: track ref stores via strmap
The refs code has two global maps that track the submodule and worktree ref stores. Even though both of these maps track values by strings, we still use a `struct hashmap` instead of a `struct strmap`. This has the benefit of saving us an allocation because we can combine key and value in a single struct. But it does introduce significant complexity that is completely unneeded. Refactor the code to use `struct strmap`s instead to reduce complexity. It's unlikely that this will have any real-world impact on performance given that most repositories likely won't have all that many ref stores. Furthermore, this refactoring allows us to de-globalize those maps and move them into `struct repository` in a subsequent commit more easily. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71c871b commit f1782d1

File tree

1 file changed

+14
-57
lines changed

1 file changed

+14
-57
lines changed

refs.c

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "advice.h"
77
#include "config.h"
88
#include "environment.h"
9-
#include "hashmap.h"
9+
#include "strmap.h"
1010
#include "gettext.h"
1111
#include "hex.h"
1212
#include "lockfile.h"
@@ -1960,66 +1960,27 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
19601960
return 0;
19611961
}
19621962

1963-
struct ref_store_hash_entry
1964-
{
1965-
struct hashmap_entry ent;
1966-
1967-
struct ref_store *refs;
1968-
1969-
/* NUL-terminated identifier of the ref store: */
1970-
char name[FLEX_ARRAY];
1971-
};
1963+
/* A strmap of ref_stores, stored by submodule name: */
1964+
static struct strmap submodule_ref_stores;
19721965

1973-
static int ref_store_hash_cmp(const void *cmp_data UNUSED,
1974-
const struct hashmap_entry *eptr,
1975-
const struct hashmap_entry *entry_or_key,
1976-
const void *keydata)
1977-
{
1978-
const struct ref_store_hash_entry *e1, *e2;
1979-
const char *name;
1980-
1981-
e1 = container_of(eptr, const struct ref_store_hash_entry, ent);
1982-
e2 = container_of(entry_or_key, const struct ref_store_hash_entry, ent);
1983-
name = keydata ? keydata : e2->name;
1984-
1985-
return strcmp(e1->name, name);
1986-
}
1987-
1988-
static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
1989-
const char *name, struct ref_store *refs)
1990-
{
1991-
struct ref_store_hash_entry *entry;
1992-
1993-
FLEX_ALLOC_STR(entry, name, name);
1994-
hashmap_entry_init(&entry->ent, strhash(name));
1995-
entry->refs = refs;
1996-
return entry;
1997-
}
1998-
1999-
/* A hashmap of ref_stores, stored by submodule name: */
2000-
static struct hashmap submodule_ref_stores;
2001-
2002-
/* A hashmap of ref_stores, stored by worktree id: */
2003-
static struct hashmap worktree_ref_stores;
1966+
/* A strmap of ref_stores, stored by worktree id: */
1967+
static struct strmap worktree_ref_stores;
20041968

20051969
/*
20061970
* Look up a ref store by name. If that ref_store hasn't been
20071971
* registered yet, return NULL.
20081972
*/
2009-
static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1973+
static struct ref_store *lookup_ref_store_map(struct strmap *map,
20101974
const char *name)
20111975
{
2012-
struct ref_store_hash_entry *entry;
2013-
unsigned int hash;
1976+
struct strmap_entry *entry;
20141977

2015-
if (!map->tablesize)
1978+
if (!map->map.tablesize)
20161979
/* It's initialized on demand in register_ref_store(). */
20171980
return NULL;
20181981

2019-
hash = strhash(name);
2020-
entry = hashmap_get_entry_from_hash(map, hash, name,
2021-
struct ref_store_hash_entry, ent);
2022-
return entry ? entry->refs : NULL;
1982+
entry = strmap_get_entry(map, name);
1983+
return entry ? entry->value : NULL;
20231984
}
20241985

20251986
/*
@@ -2064,18 +2025,14 @@ struct ref_store *get_main_ref_store(struct repository *r)
20642025
* Associate a ref store with a name. It is a fatal error to call this
20652026
* function twice for the same name.
20662027
*/
2067-
static void register_ref_store_map(struct hashmap *map,
2028+
static void register_ref_store_map(struct strmap *map,
20682029
const char *type,
20692030
struct ref_store *refs,
20702031
const char *name)
20712032
{
2072-
struct ref_store_hash_entry *entry;
2073-
2074-
if (!map->tablesize)
2075-
hashmap_init(map, ref_store_hash_cmp, NULL, 0);
2076-
2077-
entry = alloc_ref_store_hash_entry(name, refs);
2078-
if (hashmap_put(map, &entry->ent))
2033+
if (!map->map.tablesize)
2034+
strmap_init(map);
2035+
if (strmap_put(map, name, refs))
20792036
BUG("%s ref_store '%s' initialized twice", type, name);
20802037
}
20812038

0 commit comments

Comments
 (0)