Skip to content

Commit f3e8ba2

Browse files
rscharfegitster
authored andcommitted
test-mergesort: read sort input all at once
The sort subcommand of test-mergesort is used to test the performance of sorting linked lists. It reads lines from stdin, sorts them and prints the result to stdout. Two heap allocations are done per line: One for the linked list item and one for the actual line string. That imposes a significant amount of allocation overhead. Reduce it by doing the same as the sort subcommand of test-string-list, namely to read the whole input file into a single buffer and then split it in-place. 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.23(0.20+0.01) 0.22(0.20+0.01) 0071.14: DEFINE_LIST_SORT sorted 0.12(0.10+0.01) 0.10(0.08+0.01) 0071.16: DEFINE_LIST_SORT reversed 0.12(0.10+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.71(0.00+0.03) 0.54(0.00+0.06) 0071.14: DEFINE_LIST_SORT sorted 0.42(0.00+0.04) 0.21(0.03+0.03) 0071.16: DEFINE_LIST_SORT reversed 0.42(0.06+0.01) 0.21(0.01+0.04) Debian bullseye on WSL2 on the same system: 0071.12: DEFINE_LIST_SORT unsorted 0.41(0.39+0.02) 0.29(0.27+0.01) 0071.14: DEFINE_LIST_SORT sorted 0.11(0.08+0.02) 0.07(0.06+0.01) 0071.16: DEFINE_LIST_SORT reversed 0.11(0.08+0.02) 0.07(0.04+0.03) Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07ee72d commit f3e8ba2

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

t/helper/test-mergesort.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@ static int compare_strings(const struct line *x, const struct line *y)
2222

2323
static int sort_stdin(void)
2424
{
25-
struct line *line, *p = NULL, *lines = NULL;
25+
struct line *lines;
26+
struct line **tail = &lines;
2627
struct strbuf sb = STRBUF_INIT;
27-
28-
while (!strbuf_getline(&sb, stdin)) {
29-
line = xmalloc(sizeof(struct line));
30-
line->text = strbuf_detach(&sb, NULL);
31-
if (p) {
32-
line->next = p->next;
33-
p->next = line;
34-
} else {
35-
line->next = NULL;
36-
lines = line;
37-
}
38-
p = line;
28+
char *p;
29+
30+
strbuf_read(&sb, 0, 0);
31+
32+
/*
33+
* Split by newline, but don't create an item
34+
* for the empty string after the last separator.
35+
*/
36+
if (sb.len && sb.buf[sb.len - 1] == '\n')
37+
strbuf_setlen(&sb, sb.len - 1);
38+
39+
p = sb.buf;
40+
for (;;) {
41+
char *eol = strchr(p, '\n');
42+
struct line *line = xmalloc(sizeof(*line));
43+
line->text = p;
44+
*tail = line;
45+
tail = &line->next;
46+
if (!eol)
47+
break;
48+
*eol = '\0';
49+
p = eol + 1;
3950
}
51+
*tail = NULL;
4052

4153
sort_lines(&lines, compare_strings);
4254

0 commit comments

Comments
 (0)