Module Name:    src
Committed By:   abhinav
Date:           Fri Oct 14 19:43:59 UTC 2016

Modified Files:
        src/usr.bin/uniq: uniq.c

Log Message:
A small optimization: since we already know the length of the lines, check
if the lenghts are equal before calling strcmp(3). Most of the times, the call
to strcmp(3) can be saved if the lines are not of the same length.

Thanks to Christos for the reviews


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/uniq/uniq.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/uniq/uniq.c
diff -u src/usr.bin/uniq/uniq.c:1.18 src/usr.bin/uniq/uniq.c:1.19
--- src/usr.bin/uniq/uniq.c:1.18	Sun Aug 26 14:14:16 2012
+++ src/usr.bin/uniq/uniq.c	Fri Oct 14 19:43:59 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: uniq.c,v 1.18 2012/08/26 14:14:16 wiz Exp $	*/
+/*	$NetBSD: uniq.c,v 1.19 2016/10/14 19:43:59 abhinav Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)uniq.c	8.3 (Berkeley) 5/4/95";
 #endif
-__RCSID("$NetBSD: uniq.c,v 1.18 2012/08/26 14:14:16 wiz Exp $");
+__RCSID("$NetBSD: uniq.c,v 1.19 2016/10/14 19:43:59 abhinav Exp $");
 #endif /* not lint */
 
 #include <err.h>
@@ -58,7 +58,7 @@ static int numchars, numfields, repeats;
 
 static FILE *file(const char *, const char *);
 static void show(FILE *, const char *);
-static const char *skip(const char *);
+static const char *skip(const char *, size_t *);
 static void obsolete(char *[]);
 static void usage(void) __dead;
 
@@ -70,6 +70,7 @@ main (int argc, char *argv[])
 	int ch;
 	char *prevline, *thisline, *p;
 	size_t prevlinesize, thislinesize, psize;
+	size_t prevlinecompsize, thislinecompsize;
 
 	setprogname(argv[0]);
 	ifp = ofp = NULL;
@@ -144,18 +145,20 @@ done:	argc -= optind;
 		}
 		(void)memcpy(thisline, p, psize);
 		thisline[psize] = '\0';
+		thislinecompsize = thislinesize;
+		prevlinecompsize = prevlinesize;
 
 		/* If requested get the chosen fields + character offsets. */
 		if (numfields || numchars) {
-			t1 = skip(thisline);
-			t2 = skip(prevline);
+			t1 = skip(thisline, &thislinecompsize);
+			t2 = skip(prevline, &prevlinecompsize);
 		} else {
 			t1 = thisline;
 			t2 = prevline;
 		}
 
 		/* If different, print; set previous to new value. */
-		if (strcmp(t1, t2)) {
+		if (thislinecompsize != prevlinecompsize || strcmp(t1, t2)) {
 			char *t;
 			size_t ts;
 
@@ -195,11 +198,12 @@ show(FILE *ofp, const char *str)
 }
 
 static const char *
-skip(const char *str)
+skip(const char *str, size_t *linesize)
 {
 	int infield, nchars, nfields;
+	size_t ls = *linesize;
 
-	for (nfields = numfields, infield = 0; nfields && *str; ++str)
+	for (nfields = numfields, infield = 0; nfields && *str; ++str, --ls)
 		if (isspace((unsigned char)*str)) {
 			if (infield) {
 				infield = 0;
@@ -207,8 +211,9 @@ skip(const char *str)
 			}
 		} else if (!infield)
 			infield = 1;
-	for (nchars = numchars; nchars-- && *str; ++str)
+	for (nchars = numchars; nchars-- && *str; ++str, --ls)
 		continue;
+	*linesize = ls;
 	return str;
 }
 

Reply via email to