Skip to content

Commit d779996

Browse files
pks-tgitster
authored andcommitted
reftable/stack: reuse buffers when reloading stack
In `reftable_stack_reload_once()` we iterate over all the tables added to the stack in order to figure out whether any of the tables needs to be reloaded. We use a set of buffers in this context to compute the paths of these tables, but discard those buffers on every iteration. This is quite wasteful given that we do not need to transfer ownership of the allocated buffer outside of the loop. Refactor the code to instead reuse the buffers to reduce the number of allocations we need to do. Note that we do not have to manually reset the buffer because `stack_filename()` does this for us already. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5c08645 commit d779996

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

reftable/stack.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
204204
reftable_calloc(sizeof(struct reftable_table) * names_len);
205205
int new_readers_len = 0;
206206
struct reftable_merged_table *new_merged = NULL;
207+
struct strbuf table_path = STRBUF_INIT;
207208
int i;
208209

209210
while (*names) {
@@ -223,13 +224,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
223224

224225
if (!rd) {
225226
struct reftable_block_source src = { NULL };
226-
struct strbuf table_path = STRBUF_INIT;
227227
stack_filename(&table_path, st, name);
228228

229229
err = reftable_block_source_from_file(&src,
230230
table_path.buf);
231-
strbuf_release(&table_path);
232-
233231
if (err < 0)
234232
goto done;
235233

@@ -267,16 +265,13 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
267265
for (i = 0; i < cur_len; i++) {
268266
if (cur[i]) {
269267
const char *name = reader_name(cur[i]);
270-
struct strbuf filename = STRBUF_INIT;
271-
stack_filename(&filename, st, name);
268+
stack_filename(&table_path, st, name);
272269

273270
reader_close(cur[i]);
274271
reftable_reader_free(cur[i]);
275272

276273
/* On Windows, can only unlink after closing. */
277-
unlink(filename.buf);
278-
279-
strbuf_release(&filename);
274+
unlink(table_path.buf);
280275
}
281276
}
282277

@@ -288,6 +283,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
288283
reftable_free(new_readers);
289284
reftable_free(new_tables);
290285
reftable_free(cur);
286+
strbuf_release(&table_path);
291287
return err;
292288
}
293289

0 commit comments

Comments
 (0)