Fri Oct 14 19:43:59 2016 UTC ()
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


(abhinav)
diff -r1.18 -r1.19 src/usr.bin/uniq/uniq.c

cvs diff -r1.18 -r1.19 src/usr.bin/uniq/uniq.c (expand / switch to context diff)
--- src/usr.bin/uniq/uniq.c 2012/08/26 14:14:16 1.18
+++ src/usr.bin/uniq/uniq.c 2016/10/14 19:43:59 1.19
@@ -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 @@
 #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 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 @@
 	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 @@
 		}
 		(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 @@
 }
 
 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 @@
 			}
 		} else if (!infield)
 			infield = 1;
-	for (nchars = numchars; nchars-- && *str; ++str)
+	for (nchars = numchars; nchars-- && *str; ++str, --ls)
 		continue;
+	*linesize = ls;
 	return str;
 }