Skip to content

Commit

Permalink
Optimize a termination test in str_map_function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Le Floch committed Nov 15, 2017
1 parent 9541cda commit 4706012
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions l3kernel/l3str.dtx
Expand Up @@ -1315,9 +1315,14 @@
% following space by a braced space and a further call to itself.
% These are received by \cs{@@_map_function:Nn}, which passes
% the space to |#1| and calls \cs{@@_map_function:w} to deal with the
% next space. Of course we need to include a trailing space (the
% question mark is needed to avoid losing the space when \TeX{}
% tokenizes the line).
% next space. The space before the braced space allows to optimize
% the \cs{q_recursion_tail} test. Of course we need to include a
% trailing space (the question mark is needed to avoid losing the
% space when \TeX{} tokenizes the line).
% At the cost of about three more auxiliaries this code could get a $9$
% times speed up by testing only every $9$-th character for whether it
% is \cs{q_recursion_tail} (also by converting $9$ spaces at a time in
% the \cs{str_map_function:nN} case).
% \begin{macrocode}
\cs_new:Npn \str_map_function:nN #1#2
{
Expand All @@ -1329,10 +1334,13 @@
}
\cs_new:Npn \str_map_function:NN
{ \exp_args:No \str_map_function:nN }
\cs_new:Npn \@@_map_function:w #1 ~ { #1 { { ~ } \@@_map_function:w } }
\cs_new:Npn \@@_map_function:w #1 ~
{ #1 { ~ { ~ } \@@_map_function:w } }
\cs_new:Npn \@@_map_function:Nn #1#2
{
\__quark_if_recursion_tail_break:nN {#2} \str_map_break:
\if_meaning:w \q_recursion_tail #2
\exp_after:wN \str_map_break:
\fi:
#1 #2 \@@_map_function:Nn #1
}
\cs_generate_variant:Nn \str_map_function:NN { c }
Expand Down

0 comments on commit 4706012

Please sign in to comment.