[hackers] [sbase] sort: remove useless allocation || Elie Le Vaillant

From: <git_AT_suckless.org>
Date: Thu, 19 Dec 2024 11:46:25 +0100 (CET)

commit 2e6e8fe4306abe501e572dc29c459e19abeec8c4
Author: Elie Le Vaillant <eolien55_AT_disroot.org>
AuthorDate: Fri Dec 6 10:37:34 2024 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
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
_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);
                         }
Received on Thu Dec 19 2024 - 11:46:25 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 19 2024 - 11:48:44 CET