Skip to content

Instantly share code, notes, and snippets.

@joinr
Last active March 12, 2022 19:48
Show Gist options
  • Save joinr/59dbbac1204e8cf5d6cd7b556e7d7b46 to your computer and use it in GitHub Desktop.
Save joinr/59dbbac1204e8cf5d6cd7b556e7d7b46 to your computer and use it in GitHub Desktop.
minor changes
diff --git a/gamedata/lua/lua/sim/Profiler.lua b/gamedata/lua/lua/sim/Profiler.lua
index 89ef924..d3d106d 100644
--- a/gamedata/lua/lua/sim/Profiler.lua
+++ b/gamedata/lua/lua/sim/Profiler.lua
@@ -22,12 +22,25 @@ local thread = false
--- Data that we send over to the UI
local data = CreateEmptyProfilerTable()
+-- change:
+
+-- Added information about lambdas....
+-- map of {name count}
+local lambdas = {}
+-- the minimum number of evaluations for registering lambdas.
+local threshold = 60
+
+
--- Toggles the profiler on / off
function ToggleProfiler(army, forceEnable)
-- basic checks for toggling the profiler
local cheatsEnabled = CheatsEnabled()
+ -- change:
+ -- lambda count for naming.
+ local lcount = 0
+
-- return if conditions are not met
if not cheatsEnabled then
return
@@ -65,7 +78,8 @@ function ToggleProfiler(army, forceEnable)
sethook(function(event)
-- quite expensive, returns a table
- local i = getinfo(2, "Sn")
+ local i = getinfo(2, "Snf") -- change: add in function for lambda analysis.
+ --local i = getinfo(2, "Sn")
-- because of "n"
-- i.name = A reasonable name for the function
@@ -82,19 +96,61 @@ function ToggleProfiler(army, forceEnable)
local scope = i.namewhat or "other"
local name = i.name or "lambda"
+ -- change: compound data for lambda info.
+ local entry = {}
+
+ -- change: create an internal mapping from the catch-all "lambda"
+ -- to a specific dataset tracking lambdas by their function name
+ -- which is really a stringified address space.
+
+ --see if our func object already exists; if so yield its name.
+ --
+ if name == "lambda" then
+ fname = tostring(i.func)
+ if lambdas[fname] then
+ entry = lambdas[fname]
+ entry.count = entry.count + 1
+ --SPEW(entry.name .. " count: " .. entry.count)
+ --let's not spam lambdas, only the big boys.
+ --name = entry.name
+ --otherwise we generate a name for it.
+ else
+ local newname = "lambda_" .. lcount
+ -- SPEW("tracking lambda " .. fname .. "as " .. newname)
+ lcount = lcount + 1
+ entry.name = newname
+ entry.count=1
+ entry.source=source
+ entry.scope=scope
+ entry.func=fname
+ entry.small=true
+ lambdas[fname] = entry
+ --cut down on lambda spam
+ --name = newname
+ end
+ if entry.count > threshold and entry.small then
+ entry.small = false
+ SPEW(name .. " has excedeed threshold")
+ entry.info = debug.traceback()
+ SPEW(entry.info)
+ end
+ if not entry.small then
+ name = entry.name
+ end
+ end
+
-- prevent an empty scope
if scope == "" then
scope = "other"
end
- -- keep track
+ -- keep track
local count = data[source][scope][name]
- if not count then
- data[source][scope][name] = 1
- else
- data[source][scope][name] = count + 1
+ if not count then
+ data[source][scope][name] = 1
+ else
+ data[source][scope][name] = count + 1
end
-
end
-- only track on function calls
, "c")
@@ -113,7 +169,7 @@ end
local yield = coroutine.yield
function SyncThread()
- while true do
+ while true do
if isProfiling then
-- pass along the profiler information
@@ -121,6 +177,9 @@ function SyncThread()
-- reset data collection
data = CreateEmptyProfilerTable()
+
+ --reset lambdas
+ lambdas = {}
end
-- hold up a frame
@@ -261,4 +320,4 @@ function RunBenchmarks(info)
-- sync to UI
Sync.BenchmarkOutput = output
-end
\ No newline at end of file
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment