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: reduce memory allocations in the modifier...



details:   https://anonhg.NetBSD.org/src/rev/1e1794b2786d
branches:  trunk
changeset: 378482:1e1794b2786d
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Apr 14 16:59:34 2021 +0000

description:
make: reduce memory allocations in the modifiers ':D' and ':U'

diffstat:

 usr.bin/make/str.h |   5 ++++-
 usr.bin/make/var.c |  18 +++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diffs (96 lines):

diff -r 7c178cfa15bc -r 1e1794b2786d usr.bin/make/str.h
--- a/usr.bin/make/str.h        Wed Apr 14 16:26:23 2021 +0000
+++ b/usr.bin/make/str.h        Wed Apr 14 16:59:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: str.h,v 1.6 2021/04/12 18:48:00 rillig Exp $   */
+/*     $NetBSD: str.h,v 1.7 2021/04/14 16:59:34 rillig Exp $   */
 
 /*
  Copyright (c) 2021 Roland Illig <rillig%NetBSD.org@localhost>
@@ -205,9 +205,12 @@ Substring_HasSuffix(Substring sub, Subst
               memcmp(sub.end - suffixLen, suffix.start, suffixLen) == 0;
 }
 
+/* Returns an independent, null-terminated copy of the substring. */
 MAKE_INLINE FStr
 Substring_Str(Substring sub)
 {
+       if (Substring_IsEmpty(sub))
+               return FStr_InitRefer("");
        return FStr_InitOwn(bmake_strsedup(sub.start, sub.end));
 }
 
diff -r 7c178cfa15bc -r 1e1794b2786d usr.bin/make/var.c
--- a/usr.bin/make/var.c        Wed Apr 14 16:26:23 2021 +0000
+++ b/usr.bin/make/var.c        Wed Apr 14 16:59:34 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.928 2021/04/14 16:12:26 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.929 2021/04/14 16:59:34 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.928 2021/04/14 16:12:26 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.929 2021/04/14 16:59:34 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -2484,7 +2484,7 @@ static ApplyModifierResult
 ApplyModifier_Defined(const char **pp, ModChain *ch)
 {
        Expr *expr = ch->expr;
-       Buffer buf;
+       LazyBuf buf;
        const char *p;
 
        VarEvalMode emode = VARE_PARSE_ONLY;
@@ -2492,8 +2492,8 @@ ApplyModifier_Defined(const char **pp, M
                if ((**pp == 'D') == (expr->defined == DEF_REGULAR))
                        emode = expr->emode;
 
-       Buf_Init(&buf);
        p = *pp + 1;
+       LazyBuf_Init(&buf, p);
        while (!IsDelimiter(*p, ch) && *p != '\0') {
 
                /* XXX: This code is similar to the one in Var_Parse.
@@ -2505,7 +2505,7 @@ ApplyModifier_Defined(const char **pp, M
                if (*p == '\\') {
                        char c = p[1];
                        if (IsDelimiter(c, ch) || c == '$' || c == '\\') {
-                               Buf_AddByte(&buf, c);
+                               LazyBuf_Add(&buf, c);
                                p += 2;
                                continue;
                        }
@@ -2518,13 +2518,13 @@ ApplyModifier_Defined(const char **pp, M
                        (void)Var_Parse(&p, expr->scope, emode, &nested_val);
                        /* TODO: handle errors */
                        if (Expr_ShouldEval(expr))
-                               Buf_AddStr(&buf, nested_val.str);
+                               LazyBuf_AddStr(&buf, nested_val.str);
                        FStr_Done(&nested_val);
                        continue;
                }
 
                /* Ordinary text */
-               Buf_AddByte(&buf, *p);
+               LazyBuf_Add(&buf, *p);
                p++;
        }
        *pp = p;
@@ -2532,9 +2532,9 @@ ApplyModifier_Defined(const char **pp, M
        Expr_Define(expr);
 
        if (VarEvalMode_ShouldEval(emode))
-               Expr_SetValueOwn(expr, Buf_DoneData(&buf));
+               Expr_SetValue(expr, Substring_Str(LazyBuf_Get(&buf)));
        else
-               Buf_Done(&buf);
+               LazyBuf_Done(&buf);
 
        return AMR_OK;
 }



Home | Main Index | Thread Index | Old Index