mirror of
https://github.com/git/git
synced 2024-10-30 04:01:21 +00:00
92d8ed8ac1
This saves 8K per `struct object_directory', meaning it saves around 800MB in my case involving 100K alternates (half or more of those alternates are unlikely to hold loose objects). This is implemented in two parts: a generic, allocation-free `cbtree' and the `oidtree' wrapper on top of it. The latter provides allocation using alloc_state as a memory pool to improve locality and reduce free(3) overhead. Unlike oid-array, the crit-bit tree does not require sorting. Performance is bound by the key length, for oidtree that is fixed at sizeof(struct object_id). There's no need to have 256 oidtrees to mitigate the O(n log n) overhead like we did with oid-array. Being a prefix trie, it is natively suited for expanding short object IDs via prefix-limited iteration in `find_short_object_filename'. On my busy workstation, p4205 performance seems to be roughly unchanged (+/-8%). Startup with 100K total alternates with no loose objects seems around 10-20% faster on a hot cache. (800MB in memory savings means more memory for the kernel FS cache). The generic cbtree implementation does impose some extra overhead for oidtree in that it uses memcmp(3) on "struct object_id" so it wastes cycles comparing 12 extra bytes on SHA-1 repositories. I've not yet explored reducing this overhead, but I expect there are many places in our code base where we'd want to investigate this. More information on crit-bit trees: https://cr.yp.to/critbit.html Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
104 lines
2.4 KiB
C
104 lines
2.4 KiB
C
/*
|
|
* A wrapper around cbtree which stores oids
|
|
* May be used to replace oid-array for prefix (abbreviation) matches
|
|
*/
|
|
#include "oidtree.h"
|
|
#include "alloc.h"
|
|
#include "hash.h"
|
|
|
|
struct oidtree_node {
|
|
/* n.k[] is used to store "struct object_id" */
|
|
struct cb_node n;
|
|
};
|
|
|
|
struct oidtree_iter_data {
|
|
oidtree_iter fn;
|
|
void *arg;
|
|
size_t *last_nibble_at;
|
|
int algo;
|
|
uint8_t last_byte;
|
|
};
|
|
|
|
void oidtree_init(struct oidtree *ot)
|
|
{
|
|
cb_init(&ot->tree);
|
|
mem_pool_init(&ot->mem_pool, 0);
|
|
}
|
|
|
|
void oidtree_clear(struct oidtree *ot)
|
|
{
|
|
if (ot) {
|
|
mem_pool_discard(&ot->mem_pool, 0);
|
|
oidtree_init(ot);
|
|
}
|
|
}
|
|
|
|
void oidtree_insert(struct oidtree *ot, const struct object_id *oid)
|
|
{
|
|
struct oidtree_node *on;
|
|
|
|
if (!oid->algo)
|
|
BUG("oidtree_insert requires oid->algo");
|
|
|
|
on = mem_pool_alloc(&ot->mem_pool, sizeof(*on) + sizeof(*oid));
|
|
oidcpy_with_padding((struct object_id *)on->n.k, oid);
|
|
|
|
/*
|
|
* n.b. Current callers won't get us duplicates, here. If a
|
|
* future caller causes duplicates, there'll be a a small leak
|
|
* that won't be freed until oidtree_clear. Currently it's not
|
|
* worth maintaining a free list
|
|
*/
|
|
cb_insert(&ot->tree, &on->n, sizeof(*oid));
|
|
}
|
|
|
|
|
|
int oidtree_contains(struct oidtree *ot, const struct object_id *oid)
|
|
{
|
|
struct object_id k;
|
|
size_t klen = sizeof(k);
|
|
|
|
oidcpy_with_padding(&k, oid);
|
|
|
|
if (oid->algo == GIT_HASH_UNKNOWN)
|
|
klen -= sizeof(oid->algo);
|
|
|
|
/* cb_lookup relies on memcmp on the struct, so order matters: */
|
|
klen += BUILD_ASSERT_OR_ZERO(offsetof(struct object_id, hash) <
|
|
offsetof(struct object_id, algo));
|
|
|
|
return cb_lookup(&ot->tree, (const uint8_t *)&k, klen) ? 1 : 0;
|
|
}
|
|
|
|
static enum cb_next iter(struct cb_node *n, void *arg)
|
|
{
|
|
struct oidtree_iter_data *x = arg;
|
|
const struct object_id *oid = (const struct object_id *)n->k;
|
|
|
|
if (x->algo != GIT_HASH_UNKNOWN && x->algo != oid->algo)
|
|
return CB_CONTINUE;
|
|
|
|
if (x->last_nibble_at) {
|
|
if ((oid->hash[*x->last_nibble_at] ^ x->last_byte) & 0xf0)
|
|
return CB_CONTINUE;
|
|
}
|
|
|
|
return x->fn(oid, x->arg);
|
|
}
|
|
|
|
void oidtree_each(struct oidtree *ot, const struct object_id *oid,
|
|
size_t oidhexsz, oidtree_iter fn, void *arg)
|
|
{
|
|
size_t klen = oidhexsz / 2;
|
|
struct oidtree_iter_data x = { 0 };
|
|
assert(oidhexsz <= GIT_MAX_HEXSZ);
|
|
|
|
x.fn = fn;
|
|
x.arg = arg;
|
|
x.algo = oid->algo;
|
|
if (oidhexsz & 1) {
|
|
x.last_byte = oid->hash[klen];
|
|
x.last_nibble_at = &klen;
|
|
}
|
|
cb_each(&ot->tree, (const uint8_t *)oid, klen, iter, &x);
|
|
}
|