A small optimization: since we already know the length of the lines, check trunk
authorabhinav <abhinav@NetBSD.org>
Fri, 14 Oct 2016 19:43:59 +0000
branchtrunk
changeset 245556 d3c941ab4453
parent 245555 720d7dd93a66
child 245557 157171649b60
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
usr.bin/uniq/uniq.c
--- a/usr.bin/uniq/uniq.c	Fri Oct 14 19:28:06 2016 +0000
+++ b/usr.bin/uniq/uniq.c	Fri Oct 14 19:43:59 2016 +0000
@@ -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;
 }