[latex3-commits] [git/LaTeX3-latex3-latex3] main: Speed up seq_map_function and seq_map_tokens (e1b635ebd)

Bruno Le Floch blflatex at gmail.com
Sat May 15 00:55:45 CEST 2021


Repository : https://github.com/latex3/latex3
On branch  : main
Link       : https://github.com/latex3/latex3/commit/e1b635ebdf8a986b44a5e5dcbbff6a629f52f1cf

>---------------------------------------------------------------

commit e1b635ebdf8a986b44a5e5dcbbff6a629f52f1cf
Author: Bruno Le Floch <blflatex at gmail.com>
Date:   Sat May 15 00:55:45 2021 +0200

    Speed up seq_map_function and seq_map_tokens
    
    This speed up is not uniform, and depends on the length of the seq.
    - empty seq: 1.5x slower.
    - one item: 1.25x slower.
    - 2 or 4 items: 1.1x slower.
    - less than 10: same speed.
    - 10-100: 1.1x to 1.2x faster.
    - longer: 1.2x faster
    So it is not a clear win.  However, I think speeding up the case of
    long seq is still worth the moderate slow-down of mapping through
    very short seq.


>---------------------------------------------------------------

e1b635ebdf8a986b44a5e5dcbbff6a629f52f1cf
 l3kernel/CHANGELOG.md |  2 +-
 l3kernel/l3seq.dtx    | 37 +++++++++++++++++++++++++++----------
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index bae75dd08..223a7dfcf 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -20,7 +20,7 @@ this project uses date-based 'snapshot' version identifiers.
 - Checking brace balance in all regex functions (issue #377)
 
 ### Changed
-- Speed up mapping functions in l3tl
+- Speed up mapping functions in l3tl, l3seq
 
 ## [2021-05-11]
 
diff --git a/l3kernel/l3seq.dtx b/l3kernel/l3seq.dtx
index 18e954d9e..36d57bbd4 100644
--- a/l3kernel/l3seq.dtx
+++ b/l3kernel/l3seq.dtx
@@ -1979,10 +1979,11 @@
 %
 % \begin{macro}{\seq_map_function:NN, \seq_map_function:cN}
 % \UnitTested
-% \begin{macro}[rEXP]{\@@_map_function:NNn}
+% \begin{macro}[rEXP]{\@@_map_function:Nw}
 %   The idea here is to apply the code of |#2| to each item in the
 %   sequence without altering the definition of \cs{@@_item:n}.  The
-%   argument delimited by \cs{@@_item:n} is almost always empty, except
+%   even-numbered arguments of \cs{@@_map_function:Nw} delimited by
+%   \cs{@@_item:n} are almost always empty, except
 %   at the end of the loop where it is \cs{prg_break:}.  This allows to
 %   break the loop without needing to do a (relatively-expensive) quark
 %   test.
@@ -1993,13 +1994,21 @@
     \exp_after:wN \@@_map_function:Nw
     \exp_after:wN #2
     #1
-    \prg_break: \@@_item:n { } \prg_break_point:
+    \prg_break:
+    \@@_item:n { } \@@_item:n { } \@@_item:n { } \@@_item:n { }
+    \prg_break_point:
     \prg_break_point:Nn \seq_map_break: { }
   }
-\cs_new:Npn \@@_map_function:Nw #1#2 \@@_item:n #3
+\cs_new:Npn \@@_map_function:Nw #1
+    #2 \@@_item:n #3
+    #4 \@@_item:n #5
+    #6 \@@_item:n #7
+    #8 \@@_item:n #9
   {
-    #2
-    #1 {#3}
+    #2 #1 {#3}
+    #4 #1 {#5}
+    #6 #1 {#7}
+    #8 #1 {#9}
     \@@_map_function:Nw #1
   }
 \cs_generate_variant:Nn \seq_map_function:NN { c }
@@ -2068,14 +2077,22 @@
   {
     \exp_last_unbraced:Nno
       \use_i:nn { \@@_map_tokens:nw {#2} } #1
-    \prg_break: \@@_item:n { } \prg_break_point:
+    \prg_break:
+    \@@_item:n { } \@@_item:n { } \@@_item:n { } \@@_item:n { }
+    \prg_break_point:
     \prg_break_point:Nn \seq_map_break: { }
   }
 \cs_generate_variant:Nn \seq_map_tokens:Nn { c }
-\cs_new:Npn \@@_map_tokens:nw #1#2 \@@_item:n #3
+\cs_new:Npn \@@_map_tokens:nw #1
+    #2 \@@_item:n #3
+    #4 \@@_item:n #5
+    #6 \@@_item:n #7
+    #8 \@@_item:n #9
   {
-    #2
-    \use:n {#1} {#3}
+    #2 \use:n {#1} {#3}
+    #4 \use:n {#1} {#5}
+    #6 \use:n {#1} {#7}
+    #8 \use:n {#1} {#9}
     \@@_map_tokens:nw {#1}
   }
 %    \end{macrocode}





More information about the latex3-commits mailing list.