From 470601224e330fd81c4573660476ffb1a7cc7f45 Mon Sep 17 00:00:00 2001 From: Bruno Le Floch Date: Tue, 14 Nov 2017 22:16:24 -0500 Subject: [PATCH] Optimize a termination test in str_map_function --- l3kernel/l3str.dtx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/l3kernel/l3str.dtx b/l3kernel/l3str.dtx index 649d269461..b190721469 100644 --- a/l3kernel/l3str.dtx +++ b/l3kernel/l3str.dtx @@ -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 { @@ -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 }