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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 11, 2023 at 10:07 UTC d779996a1087747886853f4e73cff920c09ab1a2
1 file changed +4 -8
reftable/stack.c
+4 -8
@@ -204,6 +204,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
204 reftable_calloc(sizeof(struct reftable_table) * names_len);
205 int new_readers_len = 0;
206 struct reftable_merged_table *new_merged = NULL;
207 + struct strbuf table_path = STRBUF_INIT;
208 int i;
209
210 while (*names) {
@@ -223,13 +224,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
224
225 if (!rd) {
226 struct reftable_block_source src = { NULL };
226 - struct strbuf table_path = STRBUF_INIT;
227 stack_filename(&table_path, st, name);
228
229 err = reftable_block_source_from_file(&src,
230 table_path.buf);
231 - strbuf_release(&table_path);
232 -
231 if (err < 0)
232 goto done;
233
@@ -267,16 +265,13 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
265 for (i = 0; i < cur_len; i++) {
266 if (cur[i]) {
267 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);
269
270 reader_close(cur[i]);
271 reftable_reader_free(cur[i]);
272
273 /* On Windows, can only unlink after closing. */
277 - unlink(filename.buf);
278 -
279 - strbuf_release(&filename);
274 + unlink(table_path.buf);
275 }
276 }
277
@@ -288,6 +283,7 @@ done:
283 reftable_free(new_readers);
284 reftable_free(new_tables);
285 reftable_free(cur);
286 + strbuf_release(&table_path);
287 return err;
288 }
289