Skip to content

Commit 6620f91

Browse files
pks-tgitster
authored andcommitted
reftable/record: reuse refname when copying
Do the same optimization as in the preceding commit, but this time for `reftable_record_copy()`. While not as noticeable, it still results in a small speedup when iterating over 1 million refs: Benchmark 1: show-ref: single matching ref (revision = HEAD~) Time (mean ± σ): 114.0 ms ± 3.8 ms [User: 111.1 ms, System: 2.7 ms] Range (min … max): 110.9 ms … 144.3 ms 1000 runs Benchmark 2: show-ref: single matching ref (revision = HEAD) Time (mean ± σ): 112.5 ms ± 3.7 ms [User: 109.5 ms, System: 2.8 ms] Range (min … max): 109.2 ms … 140.7 ms 1000 runs Summary show-ref: single matching ref (revision = HEAD) ran 1.01 ± 0.05 times faster than show-ref: single matching ref (revision = HEAD~) Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71d9a2e commit 6620f91

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

reftable/record.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,26 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
205205
{
206206
struct reftable_ref_record *ref = rec;
207207
const struct reftable_ref_record *src = src_rec;
208+
char *refname = NULL;
209+
size_t refname_cap = 0;
210+
208211
assert(hash_size > 0);
209212

210-
/* This is simple and correct, but we could probably reuse the hash
211-
* fields. */
213+
SWAP(refname, ref->refname);
214+
SWAP(refname_cap, ref->refname_cap);
212215
reftable_ref_record_release(ref);
216+
SWAP(ref->refname, refname);
217+
SWAP(ref->refname_cap, refname_cap);
218+
213219
if (src->refname) {
214-
ref->refname = xstrdup(src->refname);
220+
size_t refname_len = strlen(src->refname);
221+
222+
REFTABLE_ALLOC_GROW(ref->refname, refname_len + 1,
223+
ref->refname_cap);
224+
memcpy(ref->refname, src->refname, refname_len);
225+
ref->refname[refname_len] = 0;
215226
}
227+
216228
ref->update_index = src->update_index;
217229
ref->value_type = src->value_type;
218230
switch (src->value_type) {

0 commit comments

Comments
 (0)