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 allocation in modifier ':?'



details:   https://anonhg.NetBSD.org/src/rev/99b3042faaa2
branches:  trunk
changeset: 1027617:99b3042faaa2
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Dec 13 03:55:16 2021 +0000

description:
make: reduce memory allocation in modifier ':?'

The memory allocation was in LazyBuf_DoneGet.  The substring for the
untaken branch had been allocated even though it was not necessary.

No functional change.

diffstat:

 usr.bin/make/var.c |  42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)

diffs (85 lines):

diff -r 0068a036c566 -r 99b3042faaa2 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Mon Dec 13 03:41:57 2021 +0000
+++ b/usr.bin/make/var.c        Mon Dec 13 03:55:16 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.984 2021/12/13 03:41:57 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.985 2021/12/13 03:55:16 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.984 2021/12/13 03:41:57 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.985 2021/12/13 03:55:16 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -3436,8 +3436,8 @@
 {
        Expr *expr = ch->expr;
        VarParseResult res;
-       LazyBuf buf;
-       FStr then_expr, else_expr;
+       LazyBuf thenBuf;
+       LazyBuf elseBuf;
 
        bool value = false;
        VarEvalMode then_emode = VARE_PARSE_ONLY;
@@ -3453,37 +3453,39 @@
        }
 
        (*pp)++;                        /* skip past the '?' */
-       res = ParseModifierPart(pp, ':', then_emode, ch, &buf);
+       res = ParseModifierPart(pp, ':', then_emode, ch, &thenBuf);
        if (res != VPR_OK)
                return AMR_CLEANUP;
-       then_expr = LazyBuf_DoneGet(&buf);
-
-       res = ParseModifierPart(pp, ch->endc, else_emode, ch, &buf);
+
+       res = ParseModifierPart(pp, ch->endc, else_emode, ch, &elseBuf);
        if (res != VPR_OK) {
-               FStr_Done(&then_expr);
+               LazyBuf_Done(&thenBuf);
                return AMR_CLEANUP;
        }
-       else_expr = LazyBuf_DoneGet(&buf);
 
        (*pp)--;                /* Go back to the ch->endc. */
 
        if (cond_rc == COND_INVALID) {
-               Error("Bad conditional expression '%s' in '%s?%s:%s'",
-                   expr->name, expr->name, then_expr.str, else_expr.str);
-               FStr_Done(&then_expr);
-               FStr_Done(&else_expr);
+               Substring thenExpr = LazyBuf_Get(&thenBuf);
+               Substring elseExpr = LazyBuf_Get(&elseBuf);
+               Error("Bad conditional expression '%s' in '%s?%.*s:%.*s'",
+                   expr->name, expr->name,
+                   (int)Substring_Length(thenExpr), thenExpr.start,
+                   (int)Substring_Length(elseExpr), elseExpr.start);
+               LazyBuf_Done(&thenBuf);
+               LazyBuf_Done(&elseBuf);
                return AMR_CLEANUP;
        }
 
        if (!Expr_ShouldEval(expr)) {
-               FStr_Done(&then_expr);
-               FStr_Done(&else_expr);
+               LazyBuf_Done(&thenBuf);
+               LazyBuf_Done(&elseBuf);
        } else if (value) {
-               Expr_SetValue(expr, then_expr);
-               FStr_Done(&else_expr);
+               Expr_SetValue(expr, LazyBuf_DoneGet(&thenBuf));
+               LazyBuf_Done(&elseBuf);
        } else {
-               FStr_Done(&then_expr);
-               Expr_SetValue(expr, else_expr);
+               LazyBuf_Done(&thenBuf);
+               Expr_SetValue(expr, LazyBuf_DoneGet(&elseBuf));
        }
        Expr_Define(expr);
        return AMR_OK;



Home | Main Index | Thread Index | Old Index