unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [RFC] temporary Lisp_Strings
@ 2014-09-02 12:55 Dmitry Antipov
  2014-09-02 13:21 ` Andreas Schwab
                   ` (2 more replies)
  0 siblings, 3 replies; 39+ messages in thread
From: Dmitry Antipov @ 2014-09-02 12:55 UTC (permalink / raw)
  To: Emacs development discussions

[-- Attachment #1: Type: text/plain, Size: 297 bytes --]

I'm thinking about temporary Lisp_Strings on C stack (allocated with alloca).
Simple implementation (with no check whether it fits on stack) and a few use
cases attached.  Among others, the question is: is there a way to make sure
that an address returned by alloca fits in Lisp_Object?

Dmitry



[-- Attachment #2: alloca_string.patch --]
[-- Type: text/x-patch, Size: 2374 bytes --]

=== modified file 'src/fileio.c'
--- src/fileio.c	2014-09-02 11:41:22 +0000
+++ src/fileio.c	2014-09-02 11:47:45 +0000
@@ -1179,11 +1179,11 @@
 	      char newdir_utf8[MAX_UTF8_PATH];
 
 	      filename_from_ansi (newdir, newdir_utf8);
-	      tem = build_string (newdir_utf8);
+	      tem = alloca_string (newdir_utf8);
 	    }
 	  else
 #endif
-	    tem = build_string (newdir);
+	    tem = alloca_string (newdir);
 	  newdirlen = SBYTES (tem);
 	  if (multibyte && !STRING_MULTIBYTE (tem))
 	    {
@@ -1215,7 +1215,7 @@
 	      /* `getpwnam' may return a unibyte string, which will
 		 bite us since we expect the directory to be
 		 multibyte.  */
-	      tem = build_string (newdir);
+	      tem = alloca_string (newdir);
 	      newdirlen = SBYTES (tem);
 	      if (multibyte && !STRING_MULTIBYTE (tem))
 		{
@@ -1249,7 +1249,7 @@
 	    adir = NULL;
 	  else if (multibyte)
 	    {
-	      Lisp_Object tem = build_string (adir);
+	      Lisp_Object tem = alloca_string (adir);
 
 	      tem = DECODE_FILE (tem);
 	      newdirlen = SBYTES (tem);
@@ -1350,7 +1350,7 @@
 	    getcwd (adir, adir_size);
 	  if (multibyte)
 	    {
-	      Lisp_Object tem = build_string (adir);
+	      Lisp_Object tem = alloca_string (adir);
 
 	      tem = DECODE_FILE (tem);
 	      newdirlen = SBYTES (tem);

=== modified file 'src/lisp.h'
--- src/lisp.h	2014-09-02 06:49:40 +0000
+++ src/lisp.h	2014-09-02 12:31:01 +0000
@@ -4459,6 +4459,25 @@
   memcpy (alloca (SBYTES (string) + 1),		\
 	  SSDATA (string), SBYTES (string) + 1)
 
+/* Create temporary Lisp_String.  Use alloca and do not disturb GC.  */
+
+#define alloca_string(str)					\
+  ({ Lisp_Object string;					\
+     struct Lisp_String *s;					\
+     ptrdiff_t nchars, nbytes, size = strlen (str);		\
+     parse_str_as_multibyte ((const unsigned char *) str,	\
+			     size, &nchars, &nbytes);		\
+     s = alloca (sizeof *s + nbytes + 1);			\
+     s->data = (unsigned char *) s + sizeof (*s);		\
+     memcpy (s->data, str, nbytes);				\
+     s->data[size] = '\0';					\
+     s->intervals = NULL;					\
+     if (nbytes == nchars || nbytes != size)			\
+       s->size = size, s->size_byte = -1;			\
+     else							\
+       s->size = nchars, s->size_byte = nbytes;		\
+     XSETSTRING (string, s); string; })
+  
 /* Set up the name of the machine we're running on.  */
 extern void init_system_name (void);
 


^ permalink raw reply	[flat|nested] 39+ messages in thread
* [RFC] temporary Lisp_Strings
@ 2014-09-02 12:56 Dmitry Antipov
  0 siblings, 0 replies; 39+ messages in thread
From: Dmitry Antipov @ 2014-09-02 12:56 UTC (permalink / raw)
  To: Emacs development discussions

[-- Attachment #1: Type: text/plain, Size: 297 bytes --]

I'm thinking about temporary Lisp_Strings on C stack (allocated with alloca).
Simple implementation (with no check whether it fits on stack) and a few use
cases attached.  Among others, the question is: is there a way to make sure
that an address returned by alloca fits in Lisp_Object?

Dmitry



[-- Attachment #2: alloca_string.patch --]
[-- Type: text/x-patch, Size: 2374 bytes --]

=== modified file 'src/fileio.c'
--- src/fileio.c	2014-09-02 11:41:22 +0000
+++ src/fileio.c	2014-09-02 11:47:45 +0000
@@ -1179,11 +1179,11 @@
 	      char newdir_utf8[MAX_UTF8_PATH];
 
 	      filename_from_ansi (newdir, newdir_utf8);
-	      tem = build_string (newdir_utf8);
+	      tem = alloca_string (newdir_utf8);
 	    }
 	  else
 #endif
-	    tem = build_string (newdir);
+	    tem = alloca_string (newdir);
 	  newdirlen = SBYTES (tem);
 	  if (multibyte && !STRING_MULTIBYTE (tem))
 	    {
@@ -1215,7 +1215,7 @@
 	      /* `getpwnam' may return a unibyte string, which will
 		 bite us since we expect the directory to be
 		 multibyte.  */
-	      tem = build_string (newdir);
+	      tem = alloca_string (newdir);
 	      newdirlen = SBYTES (tem);
 	      if (multibyte && !STRING_MULTIBYTE (tem))
 		{
@@ -1249,7 +1249,7 @@
 	    adir = NULL;
 	  else if (multibyte)
 	    {
-	      Lisp_Object tem = build_string (adir);
+	      Lisp_Object tem = alloca_string (adir);
 
 	      tem = DECODE_FILE (tem);
 	      newdirlen = SBYTES (tem);
@@ -1350,7 +1350,7 @@
 	    getcwd (adir, adir_size);
 	  if (multibyte)
 	    {
-	      Lisp_Object tem = build_string (adir);
+	      Lisp_Object tem = alloca_string (adir);
 
 	      tem = DECODE_FILE (tem);
 	      newdirlen = SBYTES (tem);

=== modified file 'src/lisp.h'
--- src/lisp.h	2014-09-02 06:49:40 +0000
+++ src/lisp.h	2014-09-02 12:31:01 +0000
@@ -4459,6 +4459,25 @@
   memcpy (alloca (SBYTES (string) + 1),		\
 	  SSDATA (string), SBYTES (string) + 1)
 
+/* Create temporary Lisp_String.  Use alloca and do not disturb GC.  */
+
+#define alloca_string(str)					\
+  ({ Lisp_Object string;					\
+     struct Lisp_String *s;					\
+     ptrdiff_t nchars, nbytes, size = strlen (str);		\
+     parse_str_as_multibyte ((const unsigned char *) str,	\
+			     size, &nchars, &nbytes);		\
+     s = alloca (sizeof *s + nbytes + 1);			\
+     s->data = (unsigned char *) s + sizeof (*s);		\
+     memcpy (s->data, str, nbytes);				\
+     s->data[size] = '\0';					\
+     s->intervals = NULL;					\
+     if (nbytes == nchars || nbytes != size)			\
+       s->size = size, s->size_byte = -1;			\
+     else							\
+       s->size = nchars, s->size_byte = nbytes;		\
+     XSETSTRING (string, s); string; })
+  
 /* Set up the name of the machine we're running on.  */
 extern void init_system_name (void);
 


^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2014-09-08 13:30 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-02 12:55 [RFC] temporary Lisp_Strings Dmitry Antipov
2014-09-02 13:21 ` Andreas Schwab
2014-09-02 13:47   ` Dmitry Antipov
2014-09-02 14:14     ` Andreas Schwab
2014-09-02 15:00     ` Eli Zaretskii
2014-09-02 14:00 ` Stefan Monnier
2014-09-02 15:13   ` Dmitry Antipov
2014-09-02 17:32     ` Stefan Monnier
2014-09-03 10:23       ` Benchmarking temporary Lisp objects [Was: Re: [RFC] temporary Lisp_Strings] Dmitry Antipov
2014-09-03 13:14         ` Stefan Monnier
2014-09-03 14:39         ` Paul Eggert
2014-09-03 15:39           ` Dmitry Antipov
2014-09-03 16:42             ` Paul Eggert
2014-09-03 17:47               ` Stefan Monnier
2014-09-03 18:00                 ` Paul Eggert
2014-09-04  4:59               ` Dmitry Antipov
2014-09-04  5:13                 ` Paul Eggert
2014-09-04  5:51                   ` Dmitry Antipov
2014-09-04  6:45                     ` Paul Eggert
2014-09-04 13:11                     ` Stefan Monnier
2014-09-04 13:37                       ` Dmitry Antipov
2014-09-04 14:46                         ` Dmitry Antipov
2014-09-04 16:03                           ` Paul Eggert
2014-09-05  4:00                             ` Dmitry Antipov
2014-09-05  4:24                               ` Stefan Monnier
2014-09-05  9:28                                 ` Dmitry Antipov
2014-09-05  7:15                               ` Paul Eggert
2014-09-05  9:16                                 ` Dmitry Antipov
2014-09-05 14:35                                   ` Paul Eggert
2014-09-05 15:35                                 ` Stefan Monnier
2014-09-08 10:33                                   ` Dmitry Antipov
2014-09-08 12:01                                     ` Dmitry Antipov
2014-09-08 13:30                                       ` Stefan Monnier
2014-09-08 12:44                                     ` Stefan Monnier
2014-09-08 13:30                                       ` Stefan Monnier
2014-09-02 14:37 ` [RFC] temporary Lisp_Strings Paul Eggert
2014-09-02 15:24   ` Dmitry Antipov
2014-09-02 15:28   ` Dmitry Antipov
  -- strict thread matches above, loose matches on Subject: below --
2014-09-02 12:56 Dmitry Antipov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).