emacs-diffs
[Top][All Lists]
Advanced

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

master f941cc76df 1/3: mapconcat fast path with `identity` function argu


From: Mattias Engdegård
Subject: master f941cc76df 1/3: mapconcat fast path with `identity` function argument
Date: Thu, 15 Sep 2022 03:43:50 -0400 (EDT)

branch: master
commit f941cc76df7476a055350b3b1b7e9e61d1ddb246
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>

    mapconcat fast path with `identity` function argument
    
    This makes (mapconcat #'identity SEQ) slightly faster than
    (apply #'concat SEQ), which used to be much faster.
    Notably, `string-join` benefits from this change as it uses mapconcat.
    
    * src/fns.c (Fmapconcat): Speed up execution when the function
    argument is `identity`.
---
 src/fns.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/fns.c b/src/fns.c
index 2f4808be3d..9dd10fe443 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -2930,15 +2930,37 @@ FUNCTION must be a function of one argument, and must 
return a value
     return empty_unibyte_string;
   Lisp_Object *args;
   SAFE_ALLOCA_LISP (args, args_alloc);
+  if (EQ (function, Qidentity))
+    {
+      /* Fast path when no function call is necessary.  */
+      if (CONSP (sequence))
+       {
+         Lisp_Object src = sequence;
+         Lisp_Object *dst = args;
+         do
+           {
+             *dst++ = XCAR (src);
+             src = XCDR (src);
+           }
+         while (!NILP (src));
+         goto concat;
+       }
+      else if (VECTORP (sequence))
+       {
+         memcpy (args, XVECTOR (sequence)->contents, leni * sizeof *args);
+         goto concat;
+       }
+    }
   ptrdiff_t nmapped = mapcar1 (leni, args, function, sequence);
-  ptrdiff_t nargs = 2 * nmapped - 1;
   eassert (nmapped == leni);
 
+ concat: ;
+  ptrdiff_t nargs = args_alloc;
   if (NILP (separator) || (STRINGP (separator) && SCHARS (separator) == 0))
-    nargs = nmapped;
+    nargs = leni;
   else
     {
-      for (ptrdiff_t i = nmapped - 1; i > 0; i--)
+      for (ptrdiff_t i = leni - 1; i > 0; i--)
         args[i + i] = args[i];
 
       for (ptrdiff_t i = 1; i < nargs; i += 2)



reply via email to

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