Lua 当sethook设置为空函数时,是否会对性能造成相当大的影响?
我正在为基于hook的lua代码编写一个小型的评测库,因为我不能使用任何现有的公司策略 我正在考虑,只需将变量设置为true,就可以允许对所有脚本始终按需进行评测,这是否有意义,例如Lua 当sethook设置为空函数时,是否会对性能造成相当大的影响?,lua,luajit,Lua,Luajit,我正在为基于hook的lua代码编写一个小型的评测库,因为我不能使用任何现有的公司策略 我正在考虑,只需将变量设置为true,就可以允许对所有脚本始终按需进行评测,这是否有意义,例如 function hook(event) if prof_enabled then do_stuff() end end --(in main context) debug.sethook(hook, "cr") 所以问题是,如果prof_enabled=false并且hook总是设置好的话,我
function hook(event)
if prof_enabled then
do_stuff()
end
end
--(in main context)
debug.sethook(hook, "cr")
所以问题是,如果prof_enabled=false并且hook总是设置好的话,我是否应该期望显著的性能下降?我并不期望得到确切的答案,但可能会有一些见解,例如lua解释器会优化它吗
我知道最好的解决方案是只在需要的时候设置钩子,但我不能在这里这样做。没有钩子函数也可以这样做。一种方法是用生成的函数替换感兴趣的函数,该函数进行分析,如计算调用数,然后调用实际函数。像这样:
-- set up profiling
local profile = {}
-- stub function to profile an existing function
local function make_profile_func (fname, f)
profile [fname] = { func = f, count = 0 }
return function (...)
profile [fname].count = profile [fname].count + 1
local results = { f (...) } -- call original function
return unpack (results) -- return results
end -- function
end -- make_profile_func
function pairsByKeys (t, f)
local a = {}
-- build temporary table of the keys
for n in pairs (t) do
table.insert (a, n)
end
table.sort (a, f) -- sort using supplied function, if any
local i = 0 -- iterator variable
return function () -- iterator function
i = i + 1
return a[i], t[a[i]]
end -- iterator function
end -- pairsByKeys
-- show profile, called by alias
-- non-profiled functions
local npfunctions = {
string_format = string.format,
string_rep = string.rep,
string_gsub = string.gsub,
table_insert = table.insert,
table_sort = table.sort,
pairs = pairs,
}
function show_profile (name, line, wildcards)
print (npfunctions.string_rep ("-", 20), "Function profile - alpha order",
npfunctions.string_rep ("-", 20))
print ""
print (npfunctions.string_format ("%25s %8s", "Function", "Count"))
print ""
for k, v in pairsByKeys (profile) do
if v.count > 0 then
print (npfunctions.string_format ("%25s %8i", k, v.count))
end -- if
end -- for
print ""
local t = {}
for k, v in npfunctions.pairs (profile) do
if v.count > 0 then
npfunctions.table_insert (t, k)
end -- if used
end -- for
npfunctions.table_sort (t, function (a, b) return profile [a].count > profile [b].count end )
print (npfunctions.string_rep ("-", 20), "Function profile - count order",
npfunctions.string_rep ("-", 20))
print ""
print (npfunctions.string_format ("%25s %8s", "Function", "Count"))
print ""
for _, k in ipairs (t) do
print (npfunctions.string_format ("%25s %8i", k, profile [k].count))
end -- for
print ""
end -- show_profile
-- replace string functions by profiling stub function
for k, f in pairs (string) do
if type (f) == "function" then
string [k] = make_profile_func (k, f)
end -- if
end -- for
-- test
for i = 1, 10 do
string.gsub ("aaaa", "a", "b")
end -- for
for i = 1, 20 do
string.match ("foo", "f")
end -- for
-- display results
show_profile ()
在这个特定的测试中,我调用了string.gsub 10次,调用了string.match 20次。现在输出为:
-------------------- Function profile - alpha order --------------------
Function Count
gsub 10
match 20
-------------------- Function profile - count order --------------------
Function Count
match 20
gsub 10
如果你有一个高精度的计时器,你可以做其他事情,比如时间函数
我从一篇文章改编了这篇文章,在那里我也得到了精确的时间安排
make_profile_func所做的是在upvalue中保留原始函数指针的副本,并返回一个函数,该函数将一个指针添加到调用计数中,调用原始函数并返回其结果。这样做的好处在于,只需不使用生成的对应函数替换函数,就可以省去开销
换句话说,省略这些行,将不会产生开销:
for k, f in pairs (string) do
if type (f) == "function" then
string [k] = make_profile_func (k, f)
end -- if
end -- for
代码中有一点乱七八糟地使用用于生成概要文件的函数的非概要文件版本,否则您会得到一个稍微有点误导性的读数。为什么不对其进行概要文件分析并找出答案呢?另外,既然您将其标记为LuaJIT,我应该提到调试挂钩在JIT编译器中不起作用。我不认为钩子在编译的代码中被调用。作为一个考虑,考虑调试.SETHOOKType挂钩= =函数和钩子或函数结束,CR SO钩子不必被定义。@ Tom BuldGET——为什么这个解决方案会更好?@上校三十二- THX的洞察力-我不知道钩子没有被调用编译函数,但我注意到,Jit的结果并不完整。关于你的第一条评论——我已经对它进行了分析,但显然这取决于所使用的软件——在一个脚本上我得到了20%的性能命中率,而在另一个脚本上我几乎什么都没有得到——因此我想了解一些关于这方面的见解,例如关于这在jit上不太有效的信息。