[hackers] [sbase][PATCH] sort: remove useless allocation

From: Elie Le Vaillant <eolien55_AT_disroot.org>
Date: Mon, 26 Feb 2024 23:07:47 +0100

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().
---
 sort.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/sort.c b/sort.c
index fbb1abf..dd992e0 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -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';
 }
 
_AT_@ -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);
 			}
-- 
2.43.0
Received on Mon Feb 26 2024 - 23:07:47 CET

This archive was generated by hypermail 2.3.0 : Tue Feb 27 2024 - 00:00:55 CET