emacs-devel
[Top][All Lists]
Advanced

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

Re: inline build_string performance


From: Paul Eggert
Subject: Re: inline build_string performance
Date: Tue, 26 Jun 2012 11:46:34 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

On 06/26/2012 10:58 AM, Dmitry Antipov wrote:
> build_string is a macro which expands to
> build_literal (S, strlen (S)) if S is a compile-time constant,
> and to make_string (S, strlen (S)) otherwise.

Yes, but in the latter case there is code bloat
but no compensating performance benefit.

How about the following instead?

=== modified file 'src/alloc.c'
--- src/alloc.c 2012-06-26 14:41:01 +0000
+++ src/alloc.c 2012-06-26 18:43:25 +0000
@@ -2496,6 +2496,20 @@
 }
 
 
+/* Make a string from the data at STR, treating it as multibyte if the
+   data warrants.  */
+
+Lisp_Object
+build_string_1 (const char *str)
+{
+  return make_string (str, strlen (str));
+}
+
+/* Make the build_string macro visible to GDB.  */
+extern Lisp_Object (build_string) (const char *) EXTERNALLY_VISIBLE;
+Lisp_Object (build_string) (const char *s) { return build_string (s); }
+
+
 /* Return an unibyte Lisp_String set up to hold LENGTH characters
    occupying LENGTH bytes.  */
 
@@ -2533,6 +2547,27 @@
   return string;
 }
 
+#ifdef __GNUC__
+/* Fast version used when both STR and NBYTES are compile-time
+   constants, and all characters from STR are ASCII.  */
+
+Lisp_Object
+build_literal (const char *str, ptrdiff_t nbytes)
+{
+  Lisp_Object string;
+  struct Lisp_String *s;
+
+  eassert (nbytes > 0);
+
+  s = allocate_string ();
+  allocate_string_data (s, nbytes, nbytes);
+  memcpy (s->data, str, nbytes);
+  string_chars_consed += nbytes;
+
+  XSETSTRING (string, s);
+  return string;
+}
+#endif
 
 
 /***********************************************************************

=== modified file 'src/lisp.h'
--- src/lisp.h  2012-06-26 05:00:30 +0000
+++ src/lisp.h  2012-06-26 18:36:28 +0000
@@ -2715,12 +2715,19 @@
 
 /* Make a string from the data at STR, treating it as multibyte if the
    data warrants.  */
-
-static inline Lisp_Object
-build_string (const char *str)
-{
-  return make_string (str, strlen (str));
-}
+extern Lisp_Object build_string_1 (const char *);
+#ifdef __GNUC__
+extern Lisp_Object build_literal (const char *, ptrdiff_t);
+# define build_string(str)                                     \
+   ({ Lisp_Object __val;                                       \
+     if (__builtin_constant_p (str) && strlen (str) > 0)       \
+       __val = build_literal (str, strlen (str));              \
+     else                                                      \
+       __val = build_string_1 (str);                           \
+     __val; })
+#else
+# define build_string(str) build_string_1 (str)
+#endif
 
 extern Lisp_Object pure_cons (Lisp_Object, Lisp_Object);
 EXFUN (Fgarbage_collect, 0);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]