Skip to content

Commit 5784d20

Browse files
committed
Merge branch 'rs/test-mergesort'
Optimization of a test-helper command. * rs/test-mergesort: test-mergesort: use mem_pool for sort input test-mergesort: read sort input all at once
2 parents b5d2e99 + c333c2c commit 5784d20

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

t/helper/test-mergesort.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,35 @@ 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+
struct mem_pool lines_pool;
29+
char *p;
30+
31+
strbuf_read(&sb, 0, 0);
32+
33+
/*
34+
* Split by newline, but don't create an item
35+
* for the empty string after the last separator.
36+
*/
37+
if (sb.len && sb.buf[sb.len - 1] == '\n')
38+
strbuf_setlen(&sb, sb.len - 1);
39+
40+
mem_pool_init(&lines_pool, 0);
41+
p = sb.buf;
42+
for (;;) {
43+
char *eol = strchr(p, '\n');
44+
struct line *line = mem_pool_alloc(&lines_pool, sizeof(*line));
45+
line->text = p;
46+
*tail = line;
47+
tail = &line->next;
48+
if (!eol)
49+
break;
50+
*eol = '\0';
51+
p = eol + 1;
3952
}
53+
*tail = NULL;
4054

4155
sort_lines(&lines, compare_strings);
4256

0 commit comments

Comments
 (0)