mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
packfile: use a strmap to store packs by name
To allow fast lookups of a packfile by name we use a hashmap that has the packfile name as key and the pack itself as value. But while this is the perfect use case for a `strmap`, we instead use `struct hashmap` and store the hashmap entry in the packfile itself. Simplify the code by using a `strmap` instead. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
ed3305fff7
commit
e78ab37054
24
packfile.c
24
packfile.c
@@ -788,8 +788,7 @@ void packfile_store_add_pack(struct packfile_store *store,
|
|||||||
pack->next = store->packs;
|
pack->next = store->packs;
|
||||||
store->packs = pack;
|
store->packs = pack;
|
||||||
|
|
||||||
hashmap_entry_init(&pack->packmap_ent, strhash(pack->pack_name));
|
strmap_put(&store->packs_by_path, pack->pack_name, pack);
|
||||||
hashmap_add(&store->map, &pack->packmap_ent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct packed_git *packfile_store_load_pack(struct packfile_store *store,
|
struct packed_git *packfile_store_load_pack(struct packfile_store *store,
|
||||||
@@ -806,8 +805,7 @@ struct packed_git *packfile_store_load_pack(struct packfile_store *store,
|
|||||||
strbuf_strip_suffix(&key, ".idx");
|
strbuf_strip_suffix(&key, ".idx");
|
||||||
strbuf_addstr(&key, ".pack");
|
strbuf_addstr(&key, ".pack");
|
||||||
|
|
||||||
p = hashmap_get_entry_from_hash(&store->map, strhash(key.buf), key.buf,
|
p = strmap_get(&store->packs_by_path, key.buf);
|
||||||
struct packed_git, packmap_ent);
|
|
||||||
if (!p) {
|
if (!p) {
|
||||||
p = add_packed_git(store->odb->repo, idx_path,
|
p = add_packed_git(store->odb->repo, idx_path,
|
||||||
strlen(idx_path), local);
|
strlen(idx_path), local);
|
||||||
@@ -2311,27 +2309,13 @@ int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *l
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pack_map_entry_cmp(const void *cmp_data UNUSED,
|
|
||||||
const struct hashmap_entry *entry,
|
|
||||||
const struct hashmap_entry *entry2,
|
|
||||||
const void *keydata)
|
|
||||||
{
|
|
||||||
const char *key = keydata;
|
|
||||||
const struct packed_git *pg1, *pg2;
|
|
||||||
|
|
||||||
pg1 = container_of(entry, const struct packed_git, packmap_ent);
|
|
||||||
pg2 = container_of(entry2, const struct packed_git, packmap_ent);
|
|
||||||
|
|
||||||
return strcmp(pg1->pack_name, key ? key : pg2->pack_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct packfile_store *packfile_store_new(struct object_database *odb)
|
struct packfile_store *packfile_store_new(struct object_database *odb)
|
||||||
{
|
{
|
||||||
struct packfile_store *store;
|
struct packfile_store *store;
|
||||||
CALLOC_ARRAY(store, 1);
|
CALLOC_ARRAY(store, 1);
|
||||||
store->odb = odb;
|
store->odb = odb;
|
||||||
INIT_LIST_HEAD(&store->mru);
|
INIT_LIST_HEAD(&store->mru);
|
||||||
hashmap_init(&store->map, pack_map_entry_cmp, NULL, 0);
|
strmap_init(&store->packs_by_path);
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2341,7 +2325,7 @@ void packfile_store_free(struct packfile_store *store)
|
|||||||
next = p->next;
|
next = p->next;
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
hashmap_clear(&store->map);
|
strmap_clear(&store->packs_by_path, 0);
|
||||||
free(store);
|
free(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@
|
|||||||
#include "object.h"
|
#include "object.h"
|
||||||
#include "odb.h"
|
#include "odb.h"
|
||||||
#include "oidset.h"
|
#include "oidset.h"
|
||||||
|
#include "strmap.h"
|
||||||
|
|
||||||
/* in odb.h */
|
/* in odb.h */
|
||||||
struct object_info;
|
struct object_info;
|
||||||
|
|
||||||
struct packed_git {
|
struct packed_git {
|
||||||
struct hashmap_entry packmap_ent;
|
|
||||||
struct packed_git *next;
|
struct packed_git *next;
|
||||||
struct list_head mru;
|
struct list_head mru;
|
||||||
struct pack_window *windows;
|
struct pack_window *windows;
|
||||||
@@ -85,7 +85,7 @@ struct packfile_store {
|
|||||||
* A map of packfile names to packed_git structs for tracking which
|
* A map of packfile names to packed_git structs for tracking which
|
||||||
* packs have been loaded already.
|
* packs have been loaded already.
|
||||||
*/
|
*/
|
||||||
struct hashmap map;
|
struct strmap packs_by_path;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Whether packfiles have already been populated with this store's
|
* Whether packfiles have already been populated with this store's
|
||||||
|
|||||||
Reference in New Issue
Block a user