[hackers] [sbase] uniq: optimization: reuse the prevline buffer || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Tue, 24 Mar 2015 23:54:12 +0100 (CET)

commit 78187474cf6a45eeee8a5500eff665e473bc75c6
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Mar 8 14:41:05 2015 +0100

    uniq: optimization: reuse the prevline buffer
    
    don't strdup for each prevline, try to reuse the allocated buffer and if needed
    increase it's size.

diff --git a/uniq.c b/uniq.c
index db30afe..b6ac93f 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -15,11 +15,12 @@ static int sskip = 0;
 static char *prevline = NULL;
 static char *prevoffset = NULL;
 static long prevlinecount = 0;
+static size_t prevlinesiz = 0;
 
-static char *
-uniqskip(char *l)
+static const char *
+uniqskip(const char *l)
 {
- char *lo = l;
+ const char *lo = l;
         int f = fskip, s = sskip;
 
         for (; f; --f) {
_AT_@ -33,34 +34,37 @@ uniqskip(char *l)
 }
 
 static void
-uniqline(FILE *ofp, char *l)
+uniqline(FILE *ofp, const char *l, size_t len)
 {
- char *loffset = l ? uniqskip(l) : l;
+ const char *loffset = l ? uniqskip(l) : l;
 
- int linesequel = (!l || !prevline)
- ? l == prevline
- : !strcmp(loffset, prevoffset);
+ int linesequel = l && prevoffset &&
+ !strcmp(loffset, prevoffset);
 
         if (linesequel) {
                 ++prevlinecount;
                 return;
         }
 
- if (prevline) {
+ if (prevoffset) {
                 if ((prevlinecount == 1 && !dflag) ||
                     (prevlinecount != 1 && !uflag)) {
                         if (*countfmt)
                                 fprintf(ofp, countfmt, prevlinecount);
                         fputs(prevline, ofp);
                 }
- free(prevline);
- prevline = prevoffset = NULL;
+ prevoffset = NULL;
         }
 
- if (l) {
- prevline = estrdup(l);
- prevoffset = prevline + (loffset - l);
- }
+ if (l) {
+ if (!prevline || len >= prevlinesiz) {
+ prevlinesiz = len + 1;
+ prevline = erealloc(prevline, prevlinesiz);
+ }
+ memcpy(prevline, l, len);
+ prevline[len] = '\0';
+ prevoffset = prevline + (loffset - l);
+ }
         prevlinecount = 1;
 }
 
_AT_@ -69,15 +73,16 @@ uniq(FILE *fp, FILE *ofp)
 {
         char *buf = NULL;
         size_t size = 0;
+ ssize_t len;
 
- while (getline(&buf, &size, fp) != -1)
- uniqline(ofp, buf);
+ while ((len = getline(&buf, &size, fp)) != -1)
+ uniqline(ofp, buf, (size_t)len);
 }
 
 static void
 uniqfinish(FILE *ofp)
 {
- uniqline(ofp, NULL);
+ uniqline(ofp, NULL, 0);
 }
 
 static void
Received on Tue Mar 24 2015 - 23:54:12 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 25 2015 - 00:11:34 CET