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

[PATCH 2/2] test-mergesort: use mem_pool for sort input

From
René Scharfe <l.s.r@web.de>
Date
Aug 28, 2022, 10:34 UTC
Message-ID
<a597ca34-04d7-4505-2e6b-f8f33d7e817e@web.de>
In-Reply-To
<128f0fb8-d29b-8622-0cfe-2ecadc999db5@web.de>

The previous patch almost halved the number of heap allocations for the sort subcommand. Reduce it further by using a mem_pool for the line objects.

Note that t/perf/run can't be used directly to compare two versions of test-mergesort because it always runs the helpers from the checked-out version. So I hand-merged the results of separate runs before and with this patch:

macOS 12.5.1 on M1: 0071.12: DEFINE_LIST_SORT unsorted 0.22(0.20+0.01) 0.21(0.19+0.01) 0071.14: DEFINE_LIST_SORT sorted 0.10(0.08+0.01) 0.10(0.08+0.01) 0071.16: DEFINE_LIST_SORT reversed 0.10(0.08+0.01) 0.10(0.08+0.01)

Git SDK 64-bit on Windows 11 21H2 on Ryzen 7 5800H: 0071.12: DEFINE_LIST_SORT unsorted 0.54(0.00+0.06) 0.44(0.01+0.06) 0071.14: DEFINE_LIST_SORT sorted 0.21(0.03+0.03) 0.19(0.04+0.01) 0071.16: DEFINE_LIST_SORT reversed 0.21(0.01+0.04) 0.19(0.04+0.04)

Debian bullseye on WSL2 on the same system: 0071.12: DEFINE_LIST_SORT unsorted 0.29(0.27+0.01) 0.22(0.19+0.02) 0071.14: DEFINE_LIST_SORT sorted 0.07(0.06+0.01) 0.06(0.04+0.02) 0071.16: DEFINE_LIST_SORT reversed 0.07(0.04+0.03) 0.06(0.04+0.02)

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 t/helper/test-mergesort.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/t/helper/test-mergesort.c b/t/helper/test-mergesort.c
index 540537224f..335e5bb3a9 100644
--- a/t/helper/test-mergesort.c
+++ b/t/helper/test-mergesort.c
@@ -25,6 +25,7 @@ static int sort_stdin(void)
 	struct line *lines;
 	struct line **tail = &lines;
 	struct strbuf sb = STRBUF_INIT;
+	struct mem_pool lines_pool;
 	char *p;

 	strbuf_read(&sb, 0, 0);
@@ -36,10 +37,11 @@ static int sort_stdin(void)
 	if (sb.len && sb.buf[sb.len - 1] == '\n')
 		strbuf_setlen(&sb, sb.len - 1);

+	mem_pool_init(&lines_pool, 0);
 	p = sb.buf;
 	for (;;) {
 		char *eol = strchr(p, '\n');
-		struct line *line = xmalloc(sizeof(*line));
+		struct line *line = mem_pool_alloc(&lines_pool, sizeof(*line));
 		line->text = p;
 		*tail = line;
 		tail = &line->next;
--
2.30.2
Previous: René Scharfe
Message 3 of 3 in “test-mergesort: reduce memory allocation overhead of sort subcommand”
  1. 0/2 test-mergesort: reduce memory allocation overhead of sort subcommandRené Scharfe, Aug 28, 2022
  2. 1/2 test-mergesort: read sort input all at onceRené Scharfe, Aug 28, 2022
  3. 2/2 test-mergesort: use mem_pool for sort inputRené Scharfe, Aug 28, 2022

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.