The really good thing about Luau is that they have been going hard in the paint to make sure that the most standard practices are efficient/fast.
But yeah, the fact that assert
must evaluate the arguments causes the slowness. In Google’s Flogger logging library, they solve this same issue in Java by passing a closure (basically anonymous function) to the log in order to avoid evaluation unless absolutely necessary. I wonder if this would work in Lua, or if the creation of the function slows it down?
local function AssertMaybeFast(condition, func)
if not condition then
error(func(), 2)
end
return condition
end
...
AssertMaybeFast(x > 0, function() return ("x should be > 0 but it is %f"):format(x) end)
Again, I would need to benchmark that to see if there’s actually any improvement at all, or if the allocation for a new function slows it down at all.
Update: Preliminary tests show that my AssertMaybeFast
is ~6x faster than the normal assert
when needing to do a string format, but normal assert
with just a straight literal is around 3x faster than my AssertMaybeFast
version.