mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
reftable/block: move ownership of block reader into struct table_iter
The table iterator allows the caller to iterate through all records in a
reftable table. To do so it iterates through all blocks of the desired
type one by one, where for each block it creates a new block iterator
and yields all its entries.
One of the things that is somewhat confusing in this context is who owns
the block reader that is being used to read the blocks and pass them to
the block iterator. Intuitively, as the table iterator is responsible
for iterating through the blocks, one would assume that this iterator is
also responsible for managing the lifecycle of the reader. And while it
somewhat is, the block reader is ultimately stored inside of the block
iterator.
Refactor the code such that the block reader is instead fully managed by
the table iterator. Instead of passing the reader to the block iterator,
we now only end up passing the block data to it. Despite clearing up the
lifecycle of the reader, it will also allow for better reuse of the
reader in subsequent patches.
The following benchmark prints a single matching ref out of 1 million
refs. Before:
HEAP SUMMARY:
in use at exit: 13,603 bytes in 125 blocks
total heap usage: 6,607 allocs, 6,482 frees, 509,635 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,603 bytes in 125 blocks
total heap usage: 7,235 allocs, 7,110 frees, 301,481 bytes allocated
Note that while there are more allocation and free calls now, the
overall number of bytes allocated is significantly lower. The number of
allocations will be reduced significantly by the next patch though.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
b371221a60
commit
bcdc586db0
@@ -84,16 +84,18 @@ int block_reader_init(struct block_reader *br, struct reftable_block *bl,
|
||||
void block_reader_release(struct block_reader *br);
|
||||
|
||||
/* Returns the block type (eg. 'r' for refs) */
|
||||
uint8_t block_reader_type(struct block_reader *r);
|
||||
uint8_t block_reader_type(const struct block_reader *r);
|
||||
|
||||
/* Decodes the first key in the block */
|
||||
int block_reader_first_key(struct block_reader *br, struct strbuf *key);
|
||||
int block_reader_first_key(const struct block_reader *br, struct strbuf *key);
|
||||
|
||||
/* Iterate over entries in a block */
|
||||
struct block_iter {
|
||||
/* offset within the block of the next entry to read. */
|
||||
uint32_t next_off;
|
||||
struct block_reader *br;
|
||||
const unsigned char *block;
|
||||
size_t block_len;
|
||||
int hash_size;
|
||||
|
||||
/* key for last entry we read. */
|
||||
struct strbuf last_key;
|
||||
@@ -106,17 +108,20 @@ struct block_iter {
|
||||
}
|
||||
|
||||
/* Position `it` at start of the block */
|
||||
void block_iter_seek_start(struct block_iter *it, struct block_reader *br);
|
||||
void block_iter_seek_start(struct block_iter *it, const struct block_reader *br);
|
||||
|
||||
/* Position `it` to the `want` key in the block */
|
||||
int block_iter_seek_key(struct block_iter *it, struct block_reader *br,
|
||||
int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
|
||||
struct strbuf *want);
|
||||
|
||||
void block_iter_copy_from(struct block_iter *dest, struct block_iter *src);
|
||||
void block_iter_copy_from(struct block_iter *dest, const struct block_iter *src);
|
||||
|
||||
/* return < 0 for error, 0 for OK, > 0 for EOF. */
|
||||
int block_iter_next(struct block_iter *it, struct reftable_record *rec);
|
||||
|
||||
/* Reset the block iterator to pristine state without releasing its memory. */
|
||||
void block_iter_reset(struct block_iter *it);
|
||||
|
||||
/* deallocate memory for `it`. The block reader and its block is left intact. */
|
||||
void block_iter_close(struct block_iter *it);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user