Skip to content

Commit d51d8cc

Browse files
pks-tgitster
authored andcommitted
reftable/block: avoid decoding keys when searching restart points
When searching over restart points in a block we decode the key of each of the records, which results in a memory allocation. This is quite pointless though given that records it restart points will never use prefix compression and thus store their keys verbatim in the block. Refactor the code so that we can avoid decoding the keys, which saves us some allocations. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cd75790 commit d51d8cc

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

reftable/block.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,32 @@ static int restart_needle_less(size_t idx, void *_args)
287287
.buf = args->reader->block.data + off,
288288
.len = args->reader->block_len - off,
289289
};
290-
struct strbuf kth_restart_key = STRBUF_INIT;
291-
uint8_t unused_extra;
292-
int result, n;
290+
uint64_t prefix_len, suffix_len;
291+
uint8_t extra;
292+
int n;
293293

294294
/*
295-
* TODO: The restart key is verbatim in the block, so we can in theory
296-
* avoid decoding the key and thus save some allocations.
295+
* Records at restart points are stored without prefix compression, so
296+
* there is no need to fully decode the record key here. This removes
297+
* the need for allocating memory.
297298
*/
298-
n = reftable_decode_key(&kth_restart_key, &unused_extra, in);
299-
if (n < 0) {
299+
n = reftable_decode_keylen(in, &prefix_len, &suffix_len, &extra);
300+
if (n < 0 || prefix_len) {
300301
args->error = 1;
301302
return -1;
302303
}
303304

304-
result = strbuf_cmp(&args->needle, &kth_restart_key);
305-
strbuf_release(&kth_restart_key);
306-
return result < 0;
305+
string_view_consume(&in, n);
306+
if (suffix_len > in.len) {
307+
args->error = 1;
308+
return -1;
309+
}
310+
311+
n = memcmp(args->needle.buf, in.buf,
312+
args->needle.len < suffix_len ? args->needle.len : suffix_len);
313+
if (n)
314+
return n < 0;
315+
return args->needle.len < suffix_len;
307316
}
308317

309318
void block_iter_copy_from(struct block_iter *dest, struct block_iter *src)

0 commit comments

Comments
 (0)