Skip to content

Commit

Permalink
Legible Lua profiler (#14142)
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxionary committed Jan 3, 2024
1 parent 8e9d761 commit a22b170
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion builtin/profiler/instrumentation.lua
Expand Up @@ -46,11 +46,18 @@ local register_functions = {
register_on_mapblocks_changed = 0,
}

local function regex_escape(s)
return s:gsub("(%W)", "%%%1")
end

---
-- Create an unique instrument name.
-- Generate a missing label with a running index number.
--
local counts = {}
local worldmods_path = regex_escape(core.get_worldpath())
local user_path = regex_escape(core.get_user_path())
local builtin_path = regex_escape(core.get_builtin_path())
local function generate_name(def)
local class, label, func_name = def.class, def.label, def.func_name
if label then
Expand All @@ -65,7 +72,16 @@ local function generate_name(def)
local index_id = def.mod .. (class or func_name)
local index = counts[index_id] or 1
counts[index_id] = index + 1
return format("%s[%d] %s", class or func_name, index, class and func_name or ""):trim()
local info = debug.getinfo(def.func)
local modpath = regex_escape(core.get_modpath(def.mod) or "")
local source = info.source
if modpath ~= "" then
source = source:gsub(modpath, def.mod)
end
source = source:gsub(worldmods_path, "")
source = source:gsub(builtin_path, "builtin" .. DIR_DELIM)
source = source:gsub(user_path, "")
return format("%s[%d] %s#%s", class or func_name, index, source, info.linedefined)
end

---
Expand Down
2 changes: 1 addition & 1 deletion builtin/profiler/reporter.lua
Expand Up @@ -77,7 +77,7 @@ local Formatter = {
end
}

local widths = { 55, 9, 9, 9, 5, 5, 5 }
local widths = { 80, 9, 9, 9, 5, 5, 5 }
local txt_row_format = sprintf(" %%-%ds | %%%ds | %%%ds | %%%ds | %%%ds | %%%ds | %%%ds", unpack(widths))

local HR = {}
Expand Down

0 comments on commit a22b170

Please sign in to comment.