Skip to content

Commit

Permalink
optimization: memoize cs.x.y (Lua) to x_y (C) function binders
Browse files Browse the repository at this point in the history
  • Loading branch information
nikki93 committed Jul 22, 2014
1 parent a386f2b commit d2e0d17
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions data/script/cgame/system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ local serpent = require 'serpent'
-- cg.systems (shortcut cs) is a special table such that cs.sys.func evaluates
-- to C function sys_func, eg. cs.transform.rotate(...) becomes
-- transform_rotate(...)
local system_binds = {}
local systems_mt = {
__index = function (t, k)
local v = rawget(t, k)

if v == nil then
local mt = {
__index = function (_, k2)
return cg[k .. '_' .. k2]
end,
}
return setmetatable({}, mt)
local bind = system_binds[k]
if bind == nil then
bind = setmetatable({}, {
__index = function (_, k2)
return cg[k .. '_' .. k2]
end,
})
system_binds[k] = bind
end
return bind
end
return v
end,
Expand Down

0 comments on commit d2e0d17

Please sign in to comment.