Module Name:    src
Committed By:   rillig
Date:           Sat Aug  1 13:35:13 UTC 2020

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: sysv.mk

Log Message:
make(1): reduce the number of string comparisons in ${VAR:%.c=%.o}

There is only a single position in the word where the tail ".c" can
match, since it is implicitly anchored at the end.  Therefore there's no
need to do several string comparisons.


To generate a diff of this commit:
cvs rdiff -u -r1.377 -r1.378 src/usr.bin/make/var.c
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/make/unit-tests/sysv.mk

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/make/var.c
diff -u src/usr.bin/make/var.c:1.377 src/usr.bin/make/var.c:1.378
--- src/usr.bin/make/var.c:1.377	Sat Aug  1 13:16:29 2020
+++ src/usr.bin/make/var.c	Sat Aug  1 13:35:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.377 2020/08/01 13:16:29 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.378 2020/08/01 13:35:13 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.377 2020/08/01 13:16:29 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.378 2020/08/01 13:35:13 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.377 2020/08/01 13:16:29 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.378 2020/08/01 13:35:13 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1205,19 +1205,18 @@ Str_SYSVMatch(const char *word, const ch
 	}
     }
 
-    const char *suffix = w;
+    /* Test whether the tail matches */
+    size_t w_len = strlen(w);
+    size_t p_len = strlen(p);
+    if (w_len < p_len)
+	return NULL;
 
-    /* Find a matching tail */
-    /* XXX: This loop should not be necessary since there is only one
-     * possible position where strcmp could ever return 0. */
-    do {
-	if (strcmp(p, w) == 0) {
-	    *match_len = w - suffix;
-	    return suffix;
-	}
-    } while (*w++ != '\0');
+    const char *w_tail = w + w_len - p_len;
+    if (memcmp(p, w_tail, p_len) != 0)
+        return NULL;
 
-    return NULL;
+    *match_len = w_tail - w;
+    return w;
 }
 
 

Index: src/usr.bin/make/unit-tests/sysv.mk
diff -u src/usr.bin/make/unit-tests/sysv.mk:1.11 src/usr.bin/make/unit-tests/sysv.mk:1.12
--- src/usr.bin/make/unit-tests/sysv.mk:1.11	Sat Aug  1 12:47:56 2020
+++ src/usr.bin/make/unit-tests/sysv.mk	Sat Aug  1 13:35:13 2020
@@ -1,4 +1,4 @@
-# $Id: sysv.mk,v 1.11 2020/08/01 12:47:56 rillig Exp $
+# $Id: sysv.mk,v 1.12 2020/08/01 13:35:13 rillig Exp $
 
 all: foo fun sam bla words ampersand anchor-dollar
 all: mismatch
@@ -87,8 +87,10 @@ EXPR.7=	${LIST:o%%e=X}		# Only the first
 EXP.7=	one two			# None of the words contains a literal '%'.
 EXPR.8=	${LIST:%=%%}
 EXP.8=	one% two%
+EXPR.9=	${LIST:%nes=%xxx}	# lhs is longer than the word "one"
+EXP.9=	one two
 
-.for i in ${:U:range=8}
+.for i in ${:U:range=9}
 .if ${EXPR.$i} != ${EXP.$i}
 .warning test case $i expected "${EXP.$i}", got "${EXPR.$i}
 .endif

Reply via email to