Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): reduce the number of string comparison...



details:   https://anonhg.NetBSD.org/src/rev/f62c0f8403a2
branches:  trunk
changeset: 936668:f62c0f8403a2
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Aug 01 13:35:13 2020 +0000

description:
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.

diffstat:

 usr.bin/make/unit-tests/sysv.mk |   6 ++++--
 usr.bin/make/var.c              |  31 +++++++++++++++----------------
 2 files changed, 19 insertions(+), 18 deletions(-)

diffs (79 lines):

diff -r f83e29dbb868 -r f62c0f8403a2 usr.bin/make/unit-tests/sysv.mk
--- a/usr.bin/make/unit-tests/sysv.mk   Sat Aug 01 13:16:29 2020 +0000
+++ b/usr.bin/make/unit-tests/sysv.mk   Sat Aug 01 13:35:13 2020 +0000
@@ -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 @@
 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
diff -r f83e29dbb868 -r f62c0f8403a2 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Aug 01 13:16:29 2020 +0000
+++ b/usr.bin/make/var.c        Sat Aug 01 13:35:13 2020 +0000
@@ -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 @@
        }
     }
 
-    const char *suffix = w;
-
-    /* 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');
-
-    return NULL;
+    /* Test whether the tail matches */
+    size_t w_len = strlen(w);
+    size_t p_len = strlen(p);
+    if (w_len < p_len)
+       return NULL;
+
+    const char *w_tail = w + w_len - p_len;
+    if (memcmp(p, w_tail, p_len) != 0)
+        return NULL;
+
+    *match_len = w_tail - w;
+    return w;
 }
 
 



Home | Main Index | Thread Index | Old Index