[latex3-commits] [git/LaTeX3-latex3-latex2e] shared_callback: Better interface for shared engine callbacks (1732c7da)
Marcel Fabian Krüger
tex at 2krueger.de
Sun Aug 14 11:15:13 CEST 2022
Repository : https://github.com/latex3/latex2e
On branch : shared_callback
Link : https://github.com/latex3/latex2e/commit/1732c7da1532cf7b5430e715f87bf1d155574d42
>---------------------------------------------------------------
commit 1732c7da1532cf7b5430e715f87bf1d155574d42
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date: Mon May 16 20:16:03 2022 +0200
Better interface for shared engine callbacks
>---------------------------------------------------------------
1732c7da1532cf7b5430e715f87bf1d155574d42
base/changes.txt | 5 ++
base/doc/ltnews36.tex | 7 +++
base/ltluatex.dtx | 65 +++++++++++++++++-----
base/testfiles/tlb-ltluatex-001.luatex.tlg | 2 +-
...luatex.tlg => tlb-mathcallbacks-001.luatex.tlg} | 0
...mathcallbacks.lvt => tlb-mathcallbacks-001.lvt} | 1 +
...mathcallbacks.tlg => tlb-mathcallbacks-001.tlg} | 0
base/testfiles/tlb-mathcallbacks-002.luatex.tlg | 26 +++++++++
base/testfiles/tlb-mathcallbacks-002.lvt | 31 +++++++++++
...b-0060.luatex.tlg => tlb-mathcallbacks-002.tlg} | 0
10 files changed, 123 insertions(+), 14 deletions(-)
diff --git a/base/changes.txt b/base/changes.txt
index cef69f97..735ac8f4 100644
--- a/base/changes.txt
+++ b/base/changes.txt
@@ -6,6 +6,11 @@ completeness or accuracy and it contains some references to files that
are not part of the distribution.
================================================================================
+2022-08-13 Marcel Krüger <Marcel.Krueger at latex-project.org>
+
+ * ltluatex.dtx:
+ Unregister mlist_to_hlist callback when no related callbacks are registered
+
2022-07-23 Joseph Wright <Joseph.Wright at latex-project.org>
* ltkeys.dtx:
diff --git a/base/doc/ltnews36.tex b/base/doc/ltnews36.tex
index 88f2b855..9d53ac3e 100644
--- a/base/doc/ltnews36.tex
+++ b/base/doc/ltnews36.tex
@@ -181,6 +181,13 @@ font instead.
\githubissue{879}
+\subsection{\LuaTeX\ callback efficancy improvement}
+
+The mechanism for providing the \texttt{pre/post\_mlist\_to\_hlist\_filter} callbacks in \LuaTeX\ has been improved to make it more reusable and to avoid overhead if these callbacks are not used.
+%
+\githubissue{820}
+
+
\section{Bug fixes}
diff --git a/base/ltluatex.dtx b/base/ltluatex.dtx
index 18623fd6..ab81c362 100644
--- a/base/ltluatex.dtx
+++ b/base/ltluatex.dtx
@@ -28,7 +28,7 @@
\ProvidesFile{ltluatex.dtx}
%</driver>
%<*tex>
-[2021/12/27 v1.1x
+[2022/08/13 v1.1y
%</tex>
%<plain> LuaTeX support for plain TeX (core)
%<*tex>
@@ -1398,9 +1398,7 @@ local callbacktypes = callbacktypes or {
ligaturing = simple,
kerning = simple,
insert_local_par = simple,
- pre_mlist_to_hlist_filter = list,
- mlist_to_hlist = exclusive,
- post_mlist_to_hlist_filter = reverselist,
+% mlist_to_hlist = exclusive,
new_graf = exclusive,
% \end{macrocode}
% Section 8.5: information reporting callbacks.
@@ -1461,6 +1459,30 @@ local callbacktypes = callbacktypes or {
luatexbase.callbacktypes=callbacktypes
% \end{macrocode}
%
+% \changes{v1.1y}{2022/08/13}{shared\_callbacks added}
+% Sometimes multiple callbacks correspond to a single underlying engine level callback.
+% Then the engine level callback should be registered as long as at least one of these
+% callbacks is in use. This is implemented though a shared table which counts how many
+% of the involved callbacks are currently in use. The enging level callback is registered
+% iff this count is not 0.
+%
+% We add |mlist_to_hlist| directly to the list to demonstrate this, but the handler gets
+% added later when it is actually defined.
+%
+% All callbacks in this list are treated as user defined callbacks.
+%
+% \begin{macrocode}
+local shared_callbacks = {
+ mlist_to_hlist = {
+ callback = "mlist_to_hlist",
+ count = 0,
+ handler = nil,
+ },
+}
+shared_callbacks.pre_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist
+shared_callbacks.post_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist
+% \end{macrocode}
+%
% \begin{macro}{callback.register}
% \changes{v1.0a}{2015/09/24}{Function modified}
% Save the original function for registering callbacks and prevent the
@@ -1639,11 +1661,7 @@ local defaults = {
% If a default function is not required, it may be declared as |false|.
% First we need a list of user callbacks.
% \begin{macrocode}
-local user_callbacks_defaults = {
- pre_mlist_to_hlist_filter = list_handler_default,
- mlist_to_hlist = node.mlist_to_hlist,
- post_mlist_to_hlist_filter = list_handler_default,
-}
+local user_callbacks_defaults = {}
% \end{macrocode}
%
% \begin{macro}{create_callback}
@@ -1738,9 +1756,19 @@ local function add_to_callback(name, func, description)
l = { }
callbacklist[name] = l
% \end{macrocode}
+% \changes{v1.1y}{2022/08/13}{Adapted code for shared\_callbacks}
+% Handle count for shared engine callbacks.
+% \begin{macrocode}
+ local shared = shared_callbacks[name]
+ if shared then
+ shared.count = shared.count + 1
+ if shared.count == 1 then
+ callback_register(shared.callback, shared.handler)
+ end
+% \end{macrocode}
% If it is not a user defined callback use the primitive callback register.
% \begin{macrocode}
- if user_callbacks_defaults[name] == nil then
+ elseif user_callbacks_defaults[name] == nil then
callback_register(name, handlers[callbacktypes[name]](name))
end
end
@@ -1778,6 +1806,7 @@ luatexbase.add_to_callback = add_to_callback
% \changes{v1.0k}{2015/12/02}{adjust initialization of cb local (PHG)}
% \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
% \changes{v1.1m}{2020/03/07}{Do not call callback.register for user-defined callbacks}
+% \changes{v1.1y}{2022/08/13}{Adapted code for shared\_callbacks}
% Remove a function from a callback. First check arguments.
% \begin{macrocode}
local function remove_from_callback(name, description)
@@ -1823,7 +1852,13 @@ local function remove_from_callback(name, description)
)
if #l == 0 then
callbacklist[name] = nil
- if user_callbacks_defaults[name] == nil then
+ local shared = shared_callbacks[name]
+ if shared then
+ shared.count = shared.count - 1
+ if shared.count == 0 then
+ callback_register(shared.callback, nil)
+ end
+ elseif user_callbacks_defaults[name] == nil then
callback_register(name, nil)
end
end
@@ -1917,10 +1952,14 @@ luatexbase.uninstall = uninstall
% \end{macro}
% \begin{macro}{mlist_to_hlist}
% \changes{v1.1l}{2020/02/02}{|pre/post_mlist_to_hlist| added}
+% \changes{v1.1y}{2022/08/13}{Use shared\_callback system for pre/post/mlist_to_hlist}
% To emulate these callbacks, the ``real'' |mlist_to_hlist| is replaced by a
% wrapper calling the wrappers before and after.
% \begin{macrocode}
-callback_register("mlist_to_hlist", function(head, display_type, need_penalties)
+create_callback('pre_mlist_to_hlist_filter', 'list')
+create_callback('mlist_to_hlist', 'exclusive', node.mlist_to_hlist)
+create_callback('post_mlist_to_hlist_filter', 'list')
+function shared_callbacks.mlist_to_hlist.handler(head, display_type, need_penalties)
local current = call_callback("pre_mlist_to_hlist_filter", head, display_type, need_penalties)
if current == false then
flush_list(head)
@@ -1933,7 +1972,7 @@ callback_register("mlist_to_hlist", function(head, display_type, need_penalties)
return nil
end
return post
-end)
+end
% \end{macrocode}
% \end{macro}
% \endgroup
diff --git a/base/testfiles/tlb-ltluatex-001.luatex.tlg b/base/testfiles/tlb-ltluatex-001.luatex.tlg
index 53c7483d..719637d9 100644
--- a/base/testfiles/tlb-ltluatex-001.luatex.tlg
+++ b/base/testfiles/tlb-ltluatex-001.luatex.tlg
@@ -7,7 +7,7 @@ stack traceback:
^^I[C]: in function 'error'
^^I./ltluatex.lua:110: in upvalue 'module_error'
^^I./ltluatex.lua:117: in upvalue 'luatexbase_error'
-^^I./ltluatex.lua:428: in field 'create_callback'
+^^I./ltluatex.lua:430: in field 'create_callback'
^^I[\directlua]:1: in main chunk.
l. ...}
The lua interpreter ran into a problem, so the
diff --git a/base/testfiles/tlb-mathcallbacks.luatex.tlg b/base/testfiles/tlb-mathcallbacks-001.luatex.tlg
similarity index 100%
rename from base/testfiles/tlb-mathcallbacks.luatex.tlg
rename to base/testfiles/tlb-mathcallbacks-001.luatex.tlg
diff --git a/base/testfiles/tlb-mathcallbacks.lvt b/base/testfiles/tlb-mathcallbacks-001.lvt
similarity index 96%
rename from base/testfiles/tlb-mathcallbacks.lvt
rename to base/testfiles/tlb-mathcallbacks-001.lvt
index 7c231d4b..0bb92469 100644
--- a/base/testfiles/tlb-mathcallbacks.lvt
+++ b/base/testfiles/tlb-mathcallbacks-001.lvt
@@ -1,4 +1,5 @@
\input{test2e}
+\documentclass{minimal}
\begin{document}
\START
\ifx\directlua\undefined\END\expandafter\stop\fi
diff --git a/base/testfiles/tlb-mathcallbacks.tlg b/base/testfiles/tlb-mathcallbacks-001.tlg
similarity index 100%
rename from base/testfiles/tlb-mathcallbacks.tlg
rename to base/testfiles/tlb-mathcallbacks-001.tlg
diff --git a/base/testfiles/tlb-mathcallbacks-002.luatex.tlg b/base/testfiles/tlb-mathcallbacks-002.luatex.tlg
new file mode 100644
index 00000000..d4cf21ba
--- /dev/null
+++ b/base/testfiles/tlb-mathcallbacks-002.luatex.tlg
@@ -0,0 +1,26 @@
+This is a generated file for the LaTeX2e validation system.
+Don't change this file in any respect.
+Removing `l3build.shift' from `post_mlist_to_hlist_filter'.
+false
+Inserting `filter' at position 1 in `pre_mlist_to_hlist_filter'.
+true
+Removing `filter' from `pre_mlist_to_hlist_filter'.
+false
+Inserting `filter' at position 1 in `pre_mlist_to_hlist_filter'.
+Inserting `filter' at position 1 in `post_mlist_to_hlist_filter'.
+true
+Removing `filter' from `pre_mlist_to_hlist_filter'.
+true
+Removing `filter' from `post_mlist_to_hlist_filter'.
+false
+Inserting `filter' at position 1 in `mlist_to_hlist'.
+true
+Removing `filter' from `mlist_to_hlist'.
+false
+Inserting `filter' at position 1 in `mlist_to_hlist'.
+Inserting `filter' at position 1 in `post_mlist_to_hlist_filter'.
+true
+Removing `filter' from `mlist_to_hlist'.
+true
+Removing `filter' from `post_mlist_to_hlist_filter'.
+false
diff --git a/base/testfiles/tlb-mathcallbacks-002.lvt b/base/testfiles/tlb-mathcallbacks-002.lvt
new file mode 100644
index 00000000..5d139f3d
--- /dev/null
+++ b/base/testfiles/tlb-mathcallbacks-002.lvt
@@ -0,0 +1,31 @@
+\input{test2e}
+\documentclass{minimal}
+\begin{document}
+\START
+\ifx\directlua\undefined\END\expandafter\stop\fi
+\directlua{luatexbase.remove_from_callback('post_mlist_to_hlist_filter', 'l3build.shift')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(...) print(...) return true end, 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.remove_from_callback('pre_mlist_to_hlist_filter', 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(...) print(...) return true end, 'filter')}
+\directlua{luatexbase.add_to_callback('post_mlist_to_hlist_filter', function(...) print(...) return true end, 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.remove_from_callback('pre_mlist_to_hlist_filter', 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.remove_from_callback('post_mlist_to_hlist_filter', 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.add_to_callback('mlist_to_hlist', function(...) print(...) return true end, 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.remove_from_callback('mlist_to_hlist', 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.add_to_callback('mlist_to_hlist', function(...) print(...) return true end, 'filter')}
+\directlua{luatexbase.add_to_callback('post_mlist_to_hlist_filter', function(...) print(...) return true end, 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.remove_from_callback('mlist_to_hlist', 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\directlua{luatexbase.remove_from_callback('post_mlist_to_hlist_filter', 'filter')}
+\directlua{texio.write_nl(tostring(callback.list().mlist_to_hlist))}
+\END
+\end{document}
diff --git a/base/testfiles/github-0060.luatex.tlg b/base/testfiles/tlb-mathcallbacks-002.tlg
similarity index 100%
copy from base/testfiles/github-0060.luatex.tlg
copy to base/testfiles/tlb-mathcallbacks-002.tlg
More information about the latex3-commits
mailing list.