Permalink
Browse files

optimization: memoize cs.x.y (Lua) to x_y (C) function binders

  • Loading branch information...
nikki93 committed Jul 22, 2014
1 parent a386f2b commit d2e0d17e344c02443c6139e0787490e1c2cf24d9
Showing with 11 additions and 6 deletions.
  1. +11 −6 data/script/cgame/system.lua
@@ -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,

0 comments on commit d2e0d17

Please sign in to comment.