refs/reftable: precompute prefix length

We're recomputing the prefix length on every iteration of the ref iterator. Precompute it for another speedup when iterating over 1 million refs: Benchmark 1: show-ref: single matching ref (revision = HEAD~) Time (mean ± σ): 100.3 ms ± 3.7 ms [User: 97.3 ms, System: 2.8 ms] Range (min … max): 97.5 ms … 139.7 ms 1000 runs Benchmark 2: show-ref: single matching ref (revision = HEAD) Time (mean ± σ): 95.8 ms ± 3.4 ms [User: 92.9 ms, System: 2.8 ms] Range (min … max): 93.0 ms … 121.9 ms 1000 runs Summary show-ref: single matching ref (revision = HEAD) ran 1.05 ± 0.05 times faster than show-ref: single matching ref (revision = HEAD~) Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 4, 2024 at 11:49 UTC 43f70eaea0e3fa9d98c895e9341674a67262b657
1 file changed +4 -2
refs/reftable-backend.c
+4 -2
@@ -346,6 +346,7 @@ struct reftable_ref_iterator {
346 struct object_id oid;
347
348 const char *prefix;
349 + size_t prefix_len;
350 unsigned int flags;
351 int err;
352 };
@@ -371,8 +372,8 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
372 if (!starts_with(iter->ref.refname, "refs/"))
373 continue;
374
374 - if (iter->prefix &&
375 - strncmp(iter->prefix, iter->ref.refname, strlen(iter->prefix))) {
375 + if (iter->prefix_len &&
376 + strncmp(iter->prefix, iter->ref.refname, iter->prefix_len)) {
377 iter->err = 1;
378 break;
379 }
@@ -481,6 +482,7 @@ static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_
482 iter = xcalloc(1, sizeof(*iter));
483 base_ref_iterator_init(&iter->base, &reftable_ref_iterator_vtable, 1);
484 iter->prefix = prefix;
485 + iter->prefix_len = prefix ? strlen(prefix) : 0;
486 iter->base.oid = &iter->oid;
487 iter->flags = flags;
488 iter->refs = refs;