Module Name:    src
Committed By:   rillig
Date:           Wed Apr 14 16:59:34 UTC 2021

Modified Files:
        src/usr.bin/make: str.h var.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/make/str.h
cvs rdiff -u -r1.928 -r1.929 src/usr.bin/make/var.c

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/str.h
diff -u src/usr.bin/make/str.h:1.6 src/usr.bin/make/str.h:1.7
--- src/usr.bin/make/str.h:1.6	Mon Apr 12 18:48:00 2021
+++ src/usr.bin/make/str.h	Wed Apr 14 16:59:34 2021
@@ -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 <[email protected]>
@@ -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));
 }
 

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.928 src/usr.bin/make/var.c:1.929
--- src/usr.bin/make/var.c:1.928	Wed Apr 14 16:12:26 2021
+++ src/usr.bin/make/var.c	Wed Apr 14 16:59:34 2021
@@ -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;
 }

Reply via email to