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
This commit is contained in:
Junio C Hamano 2022-09-05 18:33:40 -07:00
commit 5784d201da

View File

@ -22,21 +22,35 @@ static int compare_strings(const struct line *x, const struct line *y)
static int sort_stdin(void) static int sort_stdin(void)
{ {
struct line *line, *p = NULL, *lines = NULL; struct line *lines;
struct line **tail = &lines;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct mem_pool lines_pool;
char *p;
while (!strbuf_getline(&sb, stdin)) { strbuf_read(&sb, 0, 0);
line = xmalloc(sizeof(struct line));
line->text = strbuf_detach(&sb, NULL); /*
if (p) { * Split by newline, but don't create an item
line->next = p->next; * for the empty string after the last separator.
p->next = line; */
} else { if (sb.len && sb.buf[sb.len - 1] == '\n')
line->next = NULL; strbuf_setlen(&sb, sb.len - 1);
lines = line;
} mem_pool_init(&lines_pool, 0);
p = line; p = sb.buf;
for (;;) {
char *eol = strchr(p, '\n');
struct line *line = mem_pool_alloc(&lines_pool, sizeof(*line));
line->text = p;
*tail = line;
tail = &line->next;
if (!eol)
break;
*eol = '\0';
p = eol + 1;
} }
*tail = NULL;
sort_lines(&lines, compare_strings); sort_lines(&lines, compare_strings);