Skip to content

Commit

Permalink
reftable/block: reuse buffer to compute record keys
Browse files Browse the repository at this point in the history
When iterating over entries in the block iterator we compute the key of
each of the entries and write it into a buffer. We do not reuse the
buffer though and thus re-allocate it on every iteration, which is
wasteful.

Refactor the code to reuse the buffer.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pks-t authored and gitster committed Dec 11, 2023
1 parent a8305bc commit c0cadb0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
19 changes: 8 additions & 11 deletions reftable/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,30 +323,28 @@ int block_iter_next(struct block_iter *it, struct reftable_record *rec)
.len = it->br->block_len - it->next_off,
};
struct string_view start = in;
struct strbuf key = STRBUF_INIT;
uint8_t extra = 0;
int n = 0;

if (it->next_off >= it->br->block_len)
return 1;

n = reftable_decode_key(&key, &extra, it->last_key, in);
n = reftable_decode_key(&it->key, &extra, it->last_key, in);
if (n < 0)
return -1;

if (!key.len)
if (!it->key.len)
return REFTABLE_FORMAT_ERROR;

string_view_consume(&in, n);
n = reftable_record_decode(rec, key, extra, in, it->br->hash_size);
n = reftable_record_decode(rec, it->key, extra, in, it->br->hash_size);
if (n < 0)
return -1;
string_view_consume(&in, n);

strbuf_reset(&it->last_key);
strbuf_addbuf(&it->last_key, &key);
strbuf_addbuf(&it->last_key, &it->key);
it->next_off += start.len - in.len;
strbuf_release(&key);
return 0;
}

Expand Down Expand Up @@ -377,6 +375,7 @@ int block_iter_seek(struct block_iter *it, struct strbuf *want)
void block_iter_close(struct block_iter *it)
{
strbuf_release(&it->last_key);
strbuf_release(&it->key);
}

int block_reader_seek(struct block_reader *br, struct block_iter *it,
Expand All @@ -387,7 +386,6 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
.r = br,
};
struct reftable_record rec = reftable_new_record(block_reader_type(br));
struct strbuf key = STRBUF_INIT;
int err = 0;
struct block_iter next = BLOCK_ITER_INIT;

Expand All @@ -414,8 +412,8 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
if (err < 0)
goto done;

reftable_record_key(&rec, &key);
if (err > 0 || strbuf_cmp(&key, want) >= 0) {
reftable_record_key(&rec, &it->key);
if (err > 0 || strbuf_cmp(&it->key, want) >= 0) {
err = 0;
goto done;
}
Expand All @@ -424,8 +422,7 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it,
}

done:
strbuf_release(&key);
strbuf_release(&next.last_key);
block_iter_close(&next);
reftable_record_release(&rec);

return err;
Expand Down
2 changes: 2 additions & 0 deletions reftable/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ struct block_iter {

/* key for last entry we read. */
struct strbuf last_key;
struct strbuf key;
};

#define BLOCK_ITER_INIT { \
.last_key = STRBUF_INIT, \
.key = STRBUF_INIT, \
}

/* initializes a block reader. */
Expand Down

0 comments on commit c0cadb0

Please sign in to comment.