summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/luat-cbk.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/luat-cbk.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/luat-cbk.lua101
1 files changed, 83 insertions, 18 deletions
diff --git a/Master/texmf-dist/tex/context/base/luat-cbk.lua b/Master/texmf-dist/tex/context/base/luat-cbk.lua
index 3cb63ad6ec5..031a24e0d85 100644
--- a/Master/texmf-dist/tex/context/base/luat-cbk.lua
+++ b/Master/texmf-dist/tex/context/base/luat-cbk.lua
@@ -6,19 +6,23 @@ if not modules then modules = { } end modules ['luat-cbk'] = {
license = "see context related readme files"
}
-local insert, remove, find = table.insert, table.remove, string.find
+local insert, remove, find, format = table.insert, table.remove, string.find, string.format
local collectgarbage, type, next = collectgarbage, type, next
local round = math.round
local trace_checking = false trackers.register("memory.checking", function(v) trace_checking = v end)
+local report_callbacks = logs.reporter("system","callbacks")
+local report_memory = logs.reporter("system","memory")
+
--[[ldx--
<p>Callbacks are the real asset of <l n='luatex'/>. They permit you to hook
your own code into the <l n='tex'/> engine. Here we implement a few handy
auxiliary functions.</p>
--ldx]]--
-callbacks = callbacks or { }
+callbacks = callbacks or { }
+local callbacks = callbacks
--[[ldx--
<p>When you (temporarily) want to install a callback function, and after a
@@ -27,14 +31,55 @@ functions.</p>
--ldx]]--
local trace_callbacks = false trackers.register("system.callbacks", function(v) trace_callbacks = v end)
+local trace_calls = false -- only used when analyzing performance and initializations
+
+local register_callback, find_callback, list_callbacks = callback.register, callback.find, callback.list
+local frozen, stack, list = { }, { }, callbacks.list
+
+if not callbacks.list then -- otherwise counters get reset
+
+ list = utilities.storage.allocate(list_callbacks())
+
+ for k, _ in next, list do
+ list[k] = 0
+ end
+
+ callbacks.list = list
+
+end
+
+local delayed = table.tohash {
+ "buildpage_filter",
+}
+
+
+if trace_calls then
+
+ local functions = { }
+ local original = register_callback
-local register_callback, find_callback = callback.register, callback.find
-local frozen, stack = { }, { }
+ register_callback = function(name,func)
+ if type(func) == "function" then
+ if functions[name] then
+ functions[name] = func
+ return find_callback(name)
+ else
+ functions[name] = func
+ local cnuf = function(...)
+ list[name] = list[name] + 1
+ return functions[name](...)
+ end
+ return original(name,cnuf)
+ end
+ else
+ return original(name,func)
+ end
+ end
-callback.original_register_callback = register_callback
+end
local function frozen_message(what,name)
- logs.report("callbacks","not %s frozen '%s' (%s)",what,name,frozen[name])
+ report_callbacks("not %s frozen '%s' (%s)",what,name,frozen[name])
end
local function frozen_callback(name)
@@ -52,14 +97,17 @@ local function state(name)
end
end
+function callbacks.known(name)
+ return list[name]
+end
+
function callbacks.report()
- local list = callback.list()
- for name, func in table.sortedhash(list) do
+ for name, _ in table.sortedhash(list) do
local str = frozen[name]
if str then
- logs.report("callbacks","%s: %s -> %s",state(name),name,str)
+ report_callbacks("%s: %s -> %s",state(name),name,str)
else
- logs.report("callbacks","%s: %s",state(name),name)
+ report_callbacks("%s: %s",state(name),name)
end
end
end
@@ -67,7 +115,7 @@ end
function callbacks.table()
local NC, NR, verbatim = context.NC, context.NR, context.type
context.starttabulate { "|l|l|p|" }
- for name, func in table.sortedhash(callback.list()) do
+ for name, _ in table.sortedhash(list) do
NC() verbatim(name) NC() verbatim(state(name)) NC() context(frozen[name] or "") NC() NR()
end
context.stoptabulate()
@@ -75,11 +123,9 @@ end
function callbacks.freeze(name,freeze)
freeze = type(freeze) == "string" and freeze
---~ print(name)
if find(name,"%*") then
- local pattern = name -- string.simpleesc(name)
- local list = callback.list()
- for name, func in next, list do
+ local pattern = name
+ for name, _ in next, list do
if find(name,pattern) then
frozen[name] = freeze or frozen[name] or "frozen"
end
@@ -98,6 +144,9 @@ function callbacks.register(name,func,freeze)
elseif freeze then
frozen[name] = (type(freeze) == "string" and freeze) or "registered"
end
+ if delayed[name] and environment.initex then
+ return nil
+ end
return register_callback(name,func)
end
@@ -138,6 +187,18 @@ function callbacks.pop(name)
end
end
+if trace_calls then
+ statistics.register("callback details", function()
+ local t = { } -- todo: pass function to register and quit at nil
+ for name, n in table.sortedhash(list) do
+ if n > 0 then
+ t[#t+1] = format("%s -> %s",name,n)
+ end
+ end
+ return t
+ end)
+end
+
--~ -- somehow crashes later on
--~
--~ callbacks.freeze("find_.*_file","finding file")
@@ -203,9 +264,13 @@ restart the collector. Okay, experimental code has been removed,
because messing aroudn with the gc is too unpredictable.</p>
--ldx]]--
-garbagecollector = garbagecollector or { }
+-- For the moment we keep this here and not in util-gbc.lua or so.
+
+utilities = utilities or { }
+utilities.garbagecollector = utilities.garbagecollector or { }
+local garbagecollector = utilities.garbagecollector
-garbagecollector.enabled = false
+garbagecollector.enabled = false -- could become a directive
garbagecollector.criterium = 4*1024*1024
-- Lua allocates up to 12 times the amount of memory needed for
@@ -238,7 +303,7 @@ function garbagecollector.check(size,criterium)
local b = collectgarbage("count")
collectgarbage("collect")
local a = collectgarbage("count")
- logs.report("memory","forced sweep, collected: %s MB, used: %s MB",round((b-a)/1000),round(a/1000))
+ report_memory("forced sweep, collected: %s MB, used: %s MB",round((b-a)/1000),round(a/1000))
else
collectgarbage("collect")
end