[latex3-commits] [latex3/latex3] main: Speed up tl_if_head_is_N_type as suggested by Skillmon (closes #1127) (71e9705ea)
github at latex-project.org
github at latex-project.org
Fri Jan 12 22:59:49 CET 2024
Repository : https://github.com/latex3/latex3
On branch : main
Link : https://github.com/latex3/latex3/commit/71e9705ea0cd6c4c6347edf1765a44c7c3df4669
>---------------------------------------------------------------
commit 71e9705ea0cd6c4c6347edf1765a44c7c3df4669
Author: Bruno Le Floch <blflatex at gmail.com>
Date: Fri Jan 12 22:57:22 2024 +0100
Speed up tl_if_head_is_N_type as suggested by Skillmon (closes #1127)
Doing this as a single commit on main seems much simpler than revising
this old pull request a bit, squashing etc. I've clarified our
explanations of what is going on in the code.
>---------------------------------------------------------------
71e9705ea0cd6c4c6347edf1765a44c7c3df4669
l3kernel/l3tl.dtx | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/l3kernel/l3tl.dtx b/l3kernel/l3tl.dtx
index 105cc7581..787713045 100644
--- a/l3kernel/l3tl.dtx
+++ b/l3kernel/l3tl.dtx
@@ -3208,17 +3208,22 @@
% \begin{macro}[EXP]
% {
% \@@_if_head_is_N_type_auxi:w ,
-% \@@_if_head_is_N_type_auxii:nn ,
-% \@@_if_head_is_N_type_auxiii:n
+% \@@_if_head_is_N_type_auxii:n ,
% }
% A token list can be empty, can start with an explicit space
% character (catcode 10 and charcode 32), can start with a begin-group
% token (catcode 1), or start with an \texttt{N}-type argument. In
-% the first two cases, the line involving \cs{@@_if_head_is_N_type_auxi:w}
-% produces~|f| (and otherwise nothing). In the third case
-% (begin-group token), the lines involving \cs{token_to_str:N} produce a
-% single closing brace. The category code test is thus true exactly
-% in the fourth case, which is what we want. One cannot optimize by
+% the first two cases, and when |#1~| starts with |{}~|,
+% \cs{@@_if_head_is_N_type_auxi:w} receives an empty argument hence
+% produces |f| and removes everything before the first
+% \cs{scan_stop:}. In the third case (except when |#1~| starts with
+% |{}~|), the second auxiliary removes the first copy of~|#1| that was
+% used for the space test, then expands \cs{token_to_str:N} which hits
+% the leading begin-group token, leaving a single closing brace to be
+% compared with \cs{scan_stop:}. In the last case,
+% \cs{token_to_str:N} does not change the brace balance so that only
+% \cs{scan_stop:} \cs{scan_stop:} remain, making the character code
+% test true. One cannot optimize by
% moving one of the \cs{scan_stop:} to the beginning: if |#1| contains
% primitive conditionals, all of its occurrences must be dealt with before
% the \cs{if:w} tries to skip the \texttt{true} branch of the
@@ -3227,7 +3232,7 @@
\prg_new_conditional:Npnn \tl_if_head_is_N_type:n #1 { p , T , F , TF }
{
\if:w
- \if_false: { \fi: \@@_if_head_is_N_type_auxi:w \prg_do_nothing: #1 ~ }
+ \if_false: { \fi: \@@_if_head_is_N_type_auxi:w #1 ~ }
{ \exp_after:wN { \token_to_str:N #1 } }
\scan_stop: \scan_stop:
\prg_return_true:
@@ -3237,7 +3242,7 @@
}
\exp_args:Nno \use:n { \cs_new:Npn \@@_if_head_is_N_type_auxi:w #1 ~ }
{
- \tl_if_empty:oTF { #1 }
+ \tl_if_empty:nTF {#1}
{ f \exp_after:wN \use_none:nn }
{ \exp_after:wN \@@_if_head_is_N_type_auxii:n }
\exp_after:wN { \if_false: } \fi:
More information about the latex3-commits
mailing list.