Use a common null stringbuf in `scm_i_make_string'
authorMark H Weaver <mhw@netris.org>
Tue, 10 Jan 2012 15:13:43 +0000 (10:13 -0500)
committerMark H Weaver <mhw@netris.org>
Tue, 10 Jan 2012 15:13:43 +0000 (10:13 -0500)
* libguile/strings.c (scm_i_make_string): Use a common null stringbuf
  for newly-allocated empty strings.

libguile/strings.c

index 5dcb321..8491b1e 100644 (file)
@@ -267,8 +267,22 @@ SCM scm_nullstr;
 SCM
 scm_i_make_string (size_t len, char **charsp, int read_only_p)
 {
-  SCM buf = make_stringbuf (len);
+  static SCM null_stringbuf = SCM_BOOL_F;
+  SCM buf;
   SCM res;
+
+  if (len == 0)
+    {
+      if (SCM_UNLIKELY (scm_is_false (null_stringbuf)))
+        {
+          null_stringbuf = make_stringbuf (0);
+          SET_STRINGBUF_SHARED (null_stringbuf);
+        }
+      buf = null_stringbuf;
+    }
+  else
+    buf = make_stringbuf (len);
+
   if (charsp)
     *charsp = (char *) STRINGBUF_CHARS (buf);
   res = scm_double_cell (read_only_p ? RO_STRING_TAG : STRING_TAG,