sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 2e6e8fe4306abe501e572dc29c459e19abeec8c4
parent 74a1fbed375bac92df1f3fea36127945f071aa84
Author: Elie Le Vaillant <eolien55@disroot.org>
Date:   Fri,  6 Dec 2024 10:37:34 +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().

Diffstat:
Msort.c | 7++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git 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); }