git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 06/11] reftable/stack: reuse buffers when reloading stack

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 8, 2023, 14:53 UTC
Message-ID
<f797feff8dec383f1db9ae403cd89b80d1743432.1702047081.git.ps@pks.im>
In-Reply-To
<cover.1702047081.git.ps@pks.im>

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.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/stack.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/reftable/stack.c b/reftable/stack.c
index f5d18a842a..2dd2373360 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -204,6 +204,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
 		reftable_calloc(sizeof(struct reftable_table) * names_len);
 	int new_readers_len = 0;
 	struct reftable_merged_table *new_merged = NULL;
+	struct strbuf table_path = STRBUF_INIT;
 	int i;
 
 	while (*names) {
@@ -223,13 +224,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
 
 		if (!rd) {
 			struct reftable_block_source src = { NULL };
-			struct strbuf table_path = STRBUF_INIT;
 			stack_filename(&table_path, st, name);
 
 			err = reftable_block_source_from_file(&src,
 							      table_path.buf);
-			strbuf_release(&table_path);
-
 			if (err < 0)
 				goto done;
 
@@ -267,16 +265,13 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
 	for (i = 0; i < cur_len; i++) {
 		if (cur[i]) {
 			const char *name = reader_name(cur[i]);
-			struct strbuf filename = STRBUF_INIT;
-			stack_filename(&filename, st, name);
+			stack_filename(&table_path, st, name);
 
 			reader_close(cur[i]);
 			reftable_reader_free(cur[i]);
 
 			/* On Windows, can only unlink after closing. */
-			unlink(filename.buf);
-
-			strbuf_release(&filename);
+			unlink(table_path.buf);
 		}
 	}
 
@@ -288,6 +283,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
 	reftable_free(new_readers);
 	reftable_free(new_tables);
 	reftable_free(cur);
+	strbuf_release(&table_path);
 	return err;
 }
 
-- 
2.43.0
Previous: Patrick SteinhardtNext: Taylor Blau
Message 27 of 58 in “reftable: small set of fixes”
  1. 0/8 reftable: small set of fixesPatrick Steinhardt, Nov 21, 2023
  2. 1/8 reftable: wrap EXPECT macros in do/whilePatrick Steinhardt, Nov 21, 2023
  3. 2/8 reftable: handle interrupted readsPatrick Steinhardt, Nov 21, 2023
  4. 3/8 reftable: handle interrupted writesPatrick Steinhardt, Nov 21, 2023
  5. 4/8 reftable/stack: verify that `reftable_stack_add()` uses auto-compactionPatrick Steinhardt, Nov 21, 2023
  6. 5/8 reftable/stack: perform auto-compaction with transactional interfacePatrick Steinhardt, Nov 21, 2023
  7. Han-Wen NienhuysDec 21, 2023
  8. Patrick SteinhardtDec 21, 2023
  9. 6/8 reftable/stack: reuse buffers when reloading stackPatrick Steinhardt, Nov 21, 2023
  10. 7/8 reftable/merged: reuse buffer to compute record keysPatrick Steinhardt, Nov 21, 2023
  11. Han-Wen NienhuysDec 21, 2023
  12. 8/8 reftable/stack: fix stale lock when dyingPatrick Steinhardt, Nov 21, 2023
  13. 00/11 reftable: small set of fixesPatrick Steinhardt, Dec 8, 2023
  14. 01/11 reftable: wrap EXPECT macros in do/whilePatrick Steinhardt, Dec 8, 2023
  15. 02/11 reftable: handle interrupted readsPatrick Steinhardt, Dec 8, 2023
  16. Taylor BlauDec 8, 2023
  17. Patrick SteinhardtDec 11, 2023
  18. 03/11 reftable: handle interrupted writesPatrick Steinhardt, Dec 8, 2023
  19. 04/11 reftable/stack: verify that `reftable_stack_add()` uses auto-compactionPatrick Steinhardt, Dec 8, 2023
  20. Taylor BlauDec 8, 2023
  21. Eric SunshineDec 8, 2023
  22. Patrick SteinhardtDec 11, 2023
  23. Eric SunshineDec 11, 2023
  24. 05/11 reftable/stack: perform auto-compaction with transactional interfacePatrick Steinhardt, Dec 8, 2023
  25. Taylor BlauDec 8, 2023
  26. Patrick SteinhardtDec 11, 2023
  27. 06/11 reftable/stack: reuse buffers when reloading stackPatrick Steinhardt, Dec 8, 2023
  28. Taylor BlauDec 8, 2023
  29. Patrick SteinhardtDec 11, 2023
  30. Han-Wen NienhuysDec 21, 2023
  31. 07/11 reftable/stack: fix stale lock when dyingPatrick Steinhardt, Dec 8, 2023
  32. Taylor BlauDec 8, 2023
  33. Patrick SteinhardtDec 11, 2023
  34. 08/11 reftable/stack: fix use of unseeded randomnessPatrick Steinhardt, Dec 8, 2023
  35. Han-Wen NienhuysDec 21, 2023
  36. 09/11 reftable/merged: reuse buffer to compute record keysPatrick Steinhardt, Dec 8, 2023
  37. 10/11 reftable/block: introduce macro to initialize `struct block_iter`Patrick Steinhardt, Dec 8, 2023
  38. 11/11 reftable/block: reuse buffer to compute record keysPatrick Steinhardt, Dec 8, 2023
  39. Han-Wen NienhuysDec 21, 2023
  40. Patrick SteinhardtDec 28, 2023
  41. Taylor BlauDec 8, 2023
  42. 00/11 reftable: small set of fixesPatrick Steinhardt, Dec 11, 2023
  43. 01/11 reftable: wrap EXPECT macros in do/whilePatrick Steinhardt, Dec 11, 2023
  44. 02/11 reftable: handle interrupted readsPatrick Steinhardt, Dec 11, 2023
  45. 03/11 reftable: handle interrupted writesPatrick Steinhardt, Dec 11, 2023
  46. 04/11 reftable/stack: verify that `reftable_stack_add()` uses auto-compactionPatrick Steinhardt, Dec 11, 2023
  47. Taylor BlauDec 11, 2023
  48. Patrick SteinhardtDec 12, 2023
  49. 05/11 reftable/stack: perform auto-compaction with transactional interfacePatrick Steinhardt, Dec 11, 2023
  50. 06/11 reftable/stack: reuse buffers when reloading stackPatrick Steinhardt, Dec 11, 2023
  51. 07/11 reftable/stack: fix stale lock when dyingPatrick Steinhardt, Dec 11, 2023
  52. 08/11 reftable/stack: fix use of unseeded randomnessPatrick Steinhardt, Dec 11, 2023
  53. 09/11 reftable/merged: reuse buffer to compute record keysPatrick Steinhardt, Dec 11, 2023
  54. 10/11 reftable/block: introduce macro to initialize `struct block_iter`Patrick Steinhardt, Dec 11, 2023
  55. 11/11 reftable/block: reuse buffer to compute record keysPatrick Steinhardt, Dec 11, 2023
  56. Taylor BlauDec 11, 2023
  57. Patrick SteinhardtDec 12, 2023
  58. Han-Wen NienhuysDec 21, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.