Skip to content

Commit c333c2c

Browse files
rscharfegitster
authored andcommitted
test-mergesort: use mem_pool for sort input
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f3e8ba2 commit c333c2c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

t/helper/test-mergesort.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static int sort_stdin(void)
2525
struct line *lines;
2626
struct line **tail = &lines;
2727
struct strbuf sb = STRBUF_INIT;
28+
struct mem_pool lines_pool;
2829
char *p;
2930

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

40+
mem_pool_init(&lines_pool, 0);
3941
p = sb.buf;
4042
for (;;) {
4143
char *eol = strchr(p, '\n');
42-
struct line *line = xmalloc(sizeof(*line));
44+
struct line *line = mem_pool_alloc(&lines_pool, sizeof(*line));
4345
line->text = p;
4446
*tail = line;
4547
tail = &line->next;

0 commit comments

Comments
 (0)