Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make Add cached_realpath()



details:   https://anonhg.NetBSD.org/src/rev/2ab2a90ea9a5
branches:  trunk
changeset: 815777:2ab2a90ea9a5
user:      sjg <sjg%NetBSD.org@localhost>
date:      Fri Jun 03 01:21:59 2016 +0000

description:
Add cached_realpath()

realpath(3) is expensive, and meta mode at least uses it extensively.
We use cached_realpath() to save the result of successful calls to
realpath(3) in a private variable context.

This improves the worst case performance (eg examining libc with
nothing to do) by a factor of 4.

Reviewed by: christos

diffstat:

 usr.bin/make/main.c    |  42 ++++++++++++++++++++++++++++++++++++++----
 usr.bin/make/make.h    |   3 ++-
 usr.bin/make/meta.c    |  10 +++++-----
 usr.bin/make/nonints.h |   3 ++-
 usr.bin/make/var.c     |  10 +++++-----
 5 files changed, 52 insertions(+), 16 deletions(-)

diffs (200 lines):

diff -r b6cf19739daf -r 2ab2a90ea9a5 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Fri Jun 03 01:16:27 2016 +0000
+++ b/usr.bin/make/main.c       Fri Jun 03 01:21:59 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.244 2016/04/05 04:25:43 sjg Exp $   */
+/*     $NetBSD: main.c,v 1.245 2016/06/03 01:21:59 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.244 2016/04/05 04:25:43 sjg Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.245 2016/06/03 01:21:59 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.244 2016/04/05 04:25:43 sjg Exp $");
+__RCSID("$NetBSD: main.c,v 1.245 2016/06/03 01:21:59 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -979,7 +979,7 @@
            /*
             * A relative path, canonicalize it.
             */
-           p1 = realpath(argv[0], mdpath);
+           p1 = cached_realpath(argv[0], mdpath);
            if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
                p1 = argv[0];           /* realpath failed */
            }
@@ -1850,6 +1850,40 @@
 }
 
 
+/*
+ * realpath(3) can get expensive, cache results...
+ */
+char *
+cached_realpath(const char *pathname, char *resolved)
+{
+    static GNode *cache;
+    char *rp, *cp;
+
+    if (!pathname || !pathname[0])
+       return NULL;
+
+    if (!cache) {
+       cache = Targ_NewGN("Realpath");
+#ifndef DEBUG_REALPATH_CACHE
+       cache->flags = INTERNAL;
+#endif
+    }
+
+    rp = Var_Value(pathname, cache, &cp);
+    if (rp) {
+       /* a hit */
+       if (resolved)
+           strlcpy(resolved, rp, MAXPATHLEN);
+       else
+           resolved = bmake_strdup(rp);
+    } else {
+       if ((rp = realpath(pathname, resolved))) {
+           Var_Set(pathname, rp, cache, 0);
+       }
+    }
+    return rp ? resolved : NULL;
+}
+
 int
 PrintAddr(void *a, void *b)
 {
diff -r b6cf19739daf -r 2ab2a90ea9a5 usr.bin/make/make.h
--- a/usr.bin/make/make.h       Fri Jun 03 01:16:27 2016 +0000
+++ b/usr.bin/make/make.h       Fri Jun 03 01:21:59 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.98 2016/02/18 18:29:14 christos Exp $       */
+/*     $NetBSD: make.h,v 1.99 2016/06/03 01:21:59 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -183,6 +183,7 @@
 #define DONE_ALLSRC    0x40    /* We do it once only */
 #define CYCLE          0x1000  /* Used by MakePrintStatus */
 #define DONECYCLE      0x2000  /* Used by MakePrintStatus */
+#define INTERNAL       0x4000  /* Internal use only */
     enum enum_made {
        UNMADE, DEFERRED, REQUESTED, BEINGMADE,
        MADE, UPTODATE, ERROR, ABORTED
diff -r b6cf19739daf -r 2ab2a90ea9a5 usr.bin/make/meta.c
--- a/usr.bin/make/meta.c       Fri Jun 03 01:16:27 2016 +0000
+++ b/usr.bin/make/meta.c       Fri Jun 03 01:21:59 2016 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.58 2016/06/03 01:16:27 sjg Exp $ */
+/*      $NetBSD: meta.c,v 1.59 2016/06/03 01:21:59 sjg Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -251,7 +251,7 @@
      * basename as given to us.
      */
     if ((cp = strrchr(tname, '/'))) {
-       if (realpath(tname, buf)) {
+       if (cached_realpath(tname, buf)) {
            if ((rp = strrchr(buf, '/'))) {
                rp++;
                cp++;
@@ -433,7 +433,7 @@
     }
 
     /* make sure these are canonical */
-    if (realpath(dname, objdir))
+    if (cached_realpath(dname, objdir))
        dname = objdir;
 
     /* If we aren't in the object directory, don't create a meta file. */
@@ -1256,7 +1256,7 @@
                     * they are _expected_ to change.
                     */
                    if (*p == '/') {
-                       realpath(p, fname1); /* clean it up */
+                       cached_realpath(p, fname1); /* clean it up */
                        if (Lst_ForEach(metaIgnorePaths, prefix_match, fname1)) {
 #ifdef DEBUG_META_MODE
                            if (DEBUG(META))
@@ -1341,7 +1341,7 @@
                                oodate = TRUE;
                            } else if (S_ISDIR(fs.st_mode)) {
                                /* Update the latest directory. */
-                               realpath(p, latestdir);
+                               cached_realpath(p, latestdir);
                            }
                        } else if (errno == ENOENT && *p == '/' &&
                                   strncmp(p, cwd, cwdlen) != 0) {
diff -r b6cf19739daf -r 2ab2a90ea9a5 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Fri Jun 03 01:16:27 2016 +0000
+++ b/usr.bin/make/nonints.h    Fri Jun 03 01:21:59 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.72 2016/02/18 20:25:08 sjg Exp $ */
+/*     $NetBSD: nonints.h,v 1.73 2016/06/03 01:21:59 sjg Exp $ */
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -120,6 +120,7 @@
 char *getTmpdir(void);
 Boolean s2Boolean(const char *, Boolean);
 Boolean getBoolean(const char *, Boolean);
+char *cached_realpath(const char *, char *);
 
 /* parse.c */
 void Parse_Error(int, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
diff -r b6cf19739daf -r 2ab2a90ea9a5 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Fri Jun 03 01:16:27 2016 +0000
+++ b/usr.bin/make/var.c        Fri Jun 03 01:21:59 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.207 2016/03/11 15:12:39 matthias Exp $       */
+/*     $NetBSD: var.c,v 1.208 2016/06/03 01:21:59 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.207 2016/03/11 15:12:39 matthias Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.208 2016/06/03 01:21:59 sjg 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.207 2016/03/11 15:12:39 matthias Exp $");
+__RCSID("$NetBSD: var.c,v 1.208 2016/06/03 01:21:59 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -531,7 +531,7 @@
     h = Hash_CreateEntry(&ctxt->context, name, NULL);
     Hash_SetValue(h, v);
     v->name = h->name;
-    if (DEBUG(VAR)) {
+    if (DEBUG(VAR) && (ctxt->flags & INTERNAL) == 0) {
        fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
     }
 }
@@ -1951,7 +1951,7 @@
            Buf_AddByte(buf, vpstate->varSpace);
        }
        addSpace = TRUE;
-       rp = realpath(word, rbuf);
+       rp = cached_realpath(word, rbuf);
        if (rp && *rp == '/' && stat(rp, &st) == 0)
                word = rp;
        



Home | Main Index | Thread Index | Old Index