Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make Var* are generally very liberal with memory, wi...



details:   https://anonhg.NetBSD.org/src/rev/0f44f2ab8ceb
branches:  trunk
changeset: 778997:0f44f2ab8ceb
user:      sjg <sjg%NetBSD.org@localhost>
date:      Tue Apr 24 20:26:58 2012 +0000

description:
Var* are generally very liberal with memory, with the expectation
that none of it persists for long.
This isn't always true - for example a long running .for loop.

Buf_DestroyCompact() is used by Var_Subst(), rather than Buf_Destroy().
If it looks like we can save BUF_COMPACT_LIMIT (128) or more bytes,
call realloc.  This can reduce memory consumption by about 20%
Setting BUF_COMPACT_LIMIT to 0 dissables this.

diffstat:

 usr.bin/make/buf.c |  47 ++++++++++++++++++++++++++++++++++++++++++++---
 usr.bin/make/buf.h |   3 ++-
 usr.bin/make/var.c |   8 ++++----
 3 files changed, 50 insertions(+), 8 deletions(-)

diffs (122 lines):

diff -r 02de5f080bff -r 0f44f2ab8ceb usr.bin/make/buf.c
--- a/usr.bin/make/buf.c        Tue Apr 24 20:12:16 2012 +0000
+++ b/usr.bin/make/buf.c        Tue Apr 24 20:26:58 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $     */
+/*     $NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $";
+static char rcsid[] = "$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)buf.c      8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: buf.c,v 1.24 2009/01/17 13:29:37 dsl Exp $");
+__RCSID("$NetBSD: buf.c,v 1.25 2012/04/24 20:26:58 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -248,3 +248,44 @@
 
     return data;
 }
+
+
+/*-
+ *-----------------------------------------------------------------------
+ * Buf_DestroyCompact --
+ *     Nuke a buffer and return its data.
+ *
+ * Input:
+ *     buf             Buffer to destroy
+ *
+ * Results:
+ *     Data buffer
+ *
+ * Side Effects:
+ *     If the buffer size is much greater than its content,
+ *     a new buffer will be allocated and the old one freed.
+ *
+ *-----------------------------------------------------------------------
+ */
+#ifndef BUF_COMPACT_LIMIT
+# define BUF_COMPACT_LIMIT 128          /* worthwhile saving */
+#endif
+
+Byte *
+Buf_DestroyCompact(Buffer *buf)
+{
+#if BUF_COMPACT_LIMIT > 0
+    Byte *data;
+
+    if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
+       /* We trust realloc to be smart */
+       data = bmake_realloc(buf->buffer, buf->count + 1);
+       if (data) {
+           data[buf->count] = 0;
+           Buf_Destroy(buf, FALSE);
+           return data;
+       }
+    }
+#endif
+    return Buf_Destroy(buf, FALSE);
+}
diff -r 02de5f080bff -r 0f44f2ab8ceb usr.bin/make/buf.h
--- a/usr.bin/make/buf.h        Tue Apr 24 20:12:16 2012 +0000
+++ b/usr.bin/make/buf.h        Tue Apr 24 20:26:58 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: buf.h,v 1.16 2009/01/17 13:55:42 dsl Exp $     */
+/*     $NetBSD: buf.h,v 1.17 2012/04/24 20:26:58 sjg Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -114,5 +114,6 @@
 void Buf_Empty(Buffer *);
 void Buf_Init(Buffer *, int);
 Byte *Buf_Destroy(Buffer *, Boolean);
+Byte *Buf_DestroyCompact(Buffer *);
 
 #endif /* _BUF_H */
diff -r 02de5f080bff -r 0f44f2ab8ceb usr.bin/make/var.c
--- a/usr.bin/make/var.c        Tue Apr 24 20:12:16 2012 +0000
+++ b/usr.bin/make/var.c        Tue Apr 24 20:26:58 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $    */
+/*     $NetBSD: var.c,v 1.168 2012/04/24 20:26:58 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.168 2012/04/24 20:26:58 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.167 2011/06/03 21:10:42 sjg Exp $");
+__RCSID("$NetBSD: var.c,v 1.168 2012/04/24 20:26:58 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -4075,7 +4075,7 @@
        }
     }
 
-    return Buf_Destroy(&buf, FALSE);
+    return Buf_DestroyCompact(&buf);
 }
 
 /*-



Home | Main Index | Thread Index | Old Index