Skip to content

Commit 1db7412

Browse files
pks-tgitster
authored andcommitted
refs/reftable: reuse iterators when reading refs
When reading references the reftable backend has to: 1. Create a new ref iterator. 2. Seek the iterator to the record we're searching for. 3. Read the record. We cannot really avoid the last two steps, but re-creating the iterator every single time we want to read a reference is kind of expensive and a waste of resources. We couldn't help it in the past though because it was not possible to reuse iterators. But starting with 5bf96e0 (reftable/generic: move seeking of records into the iterator, 2024-05-13) we have split up the iterator lifecycle such that creating the iterator and seeking are two different concerns. Refactor the code such that we cache iterators in the reftable backend. This cache is invalidated whenever the respective stack is reloaded such that we know to recreate the iterator in that case. This leads to a sizeable speedup when creating many refs, which requires a lot of random reference reads: Benchmark 1: update-ref: create many refs (refcount = 100000, revision = master) Time (mean ± σ): 1.793 s ± 0.010 s [User: 0.954 s, System: 0.835 s] Range (min … max): 1.781 s … 1.811 s 10 runs Benchmark 2: update-ref: create many refs (refcount = 100000, revision = HEAD) Time (mean ± σ): 1.680 s ± 0.013 s [User: 0.846 s, System: 0.831 s] Range (min … max): 1.664 s … 1.702 s 10 runs Summary update-ref: create many refs (refcount = 100000, revision = HEAD) ran 1.07 ± 0.01 times faster than update-ref: create many refs (refcount = 100000, revision = master) While 7% is not a huge win, you have to consider that the benchmark is _writing_ data, so _reading_ references is only one part of what we do. Flame graphs show that we spend around 40% of our time reading refs, so the speedup when reading refs is approximately ~2.5x that. I could not find better benchmarks where we perform a lot of random ref reads. You can also see a sizeable impact on memory usage when creating 100k references. Before this change: HEAP SUMMARY: in use at exit: 19,112,538 bytes in 200,170 blocks total heap usage: 8,400,426 allocs, 8,200,256 frees, 454,367,048 bytes allocated After this change: HEAP SUMMARY: in use at exit: 674,416 bytes in 169 blocks total heap usage: 7,929,872 allocs, 7,929,703 frees, 281,509,985 bytes allocated As an additional factor, this refactoring opens up the possibility for more performance optimizations in how we re-seek iterators. Any change that allows us to optimize re-seeking by e.g. reusing data structures would thus also directly speed up random reads. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6b1e08 commit 1db7412

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

refs/reftable-backend.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,30 @@
3636

3737
struct reftable_backend {
3838
struct reftable_stack *stack;
39+
struct reftable_iterator it;
3940
};
4041

42+
static void reftable_backend_on_reload(void *payload)
43+
{
44+
struct reftable_backend *be = payload;
45+
reftable_iterator_destroy(&be->it);
46+
}
47+
4148
static int reftable_backend_init(struct reftable_backend *be,
4249
const char *path,
43-
const struct reftable_write_options *opts)
50+
const struct reftable_write_options *_opts)
4451
{
45-
return reftable_new_stack(&be->stack, path, opts);
52+
struct reftable_write_options opts = *_opts;
53+
opts.on_reload = reftable_backend_on_reload;
54+
opts.on_reload_payload = be;
55+
return reftable_new_stack(&be->stack, path, &opts);
4656
}
4757

4858
static void reftable_backend_release(struct reftable_backend *be)
4959
{
5060
reftable_stack_destroy(be->stack);
5161
be->stack = NULL;
62+
reftable_iterator_destroy(&be->it);
5263
}
5364

5465
static int reftable_backend_read_ref(struct reftable_backend *be,
@@ -60,10 +71,25 @@ static int reftable_backend_read_ref(struct reftable_backend *be,
6071
struct reftable_ref_record ref = {0};
6172
int ret;
6273

63-
ret = reftable_stack_read_ref(be->stack, refname, &ref);
74+
if (!be->it.ops) {
75+
ret = reftable_stack_init_ref_iterator(be->stack, &be->it);
76+
if (ret)
77+
goto done;
78+
}
79+
80+
ret = reftable_iterator_seek_ref(&be->it, refname);
6481
if (ret)
6582
goto done;
6683

84+
ret = reftable_iterator_next_ref(&be->it, &ref);
85+
if (ret)
86+
goto done;
87+
88+
if (strcmp(ref.refname, refname)) {
89+
ret = 1;
90+
goto done;
91+
}
92+
6793
if (ref.value_type == REFTABLE_REF_SYMREF) {
6894
strbuf_reset(referent);
6995
strbuf_addstr(referent, ref.value.symref);

0 commit comments

Comments
 (0)