commit 2e6e8fe4306abe501e572dc29c459e19abeec8c4
Author: Elie Le Vaillant <[email protected]>
AuthorDate: Fri Dec 6 10:37:34 2024 +0100
Commit: Roberto E. Vargas Caballero <[email protected]>
CommitDate: Thu Dec 19 11:40:06 2024 +0100
sort: remove useless allocation
I'm not sure why we're doing malloc() then memcpy() here, when
we could just make col->line.data point to start.data. This is costy
for huge sorts (3 time slower than other implementations with no real
reason). Since we are now working with the original line.data we need
to revert the s/\n/\0/ that happens in columns().
diff --git a/sort.c b/sort.c
index fbb1abf..dd992e0 100644
--- a/sort.c
+++ b/sort.c
@@ -123,11 +123,7 @@ columns(struct line *line, const struct keydef *kd, struct
column *col)
end.len = 1;
}
col->line.len = MAX(0, end.data - start.data);
- if (!(col->line.data) || col->cap < col->line.len + 1) {
- free(col->line.data);
- col->line.data = emalloc(col->line.len + 1);
- }
- memcpy(col->line.data, start.data, col->line.len);
+ col->line.data = start.data;
col->line.data[col->line.len] = '\0';
}
@@ -423,6 +419,7 @@ main(int argc, char *argv[])
for (i = 0; i < linebuf.nlines; i++) {
if (!uflag || i == 0 ||
slinecmp(&linebuf.lines[i], &linebuf.lines[i - 1]))
{
+ linebuf.lines[i].data[linebuf.lines[i].len-1] =
'\n';
fwrite(linebuf.lines[i].data, 1,
linebuf.lines[i].len, ofp);
}