-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How about move Yue auto generated anonymous function to upvalue with name prefix '__' #162
Comments
It is a brilliant idea! And it seems the TypescriptToLua compiler is doing similar optimizations too.
f = ->
func if cond
print 123
true
else
false is generating to: local _anon_func_0 = function(cond, print)
if cond then
print(123)
return true
else
return false
end
end
local f
f = function()
-- passing the accessed global variable "print" from the call site
return func(_anon_func_0(cond, print))
end
onEvent "start", ->
-- the "with" expression below that generating anonymous function can be optimized
gameScene\addChild with ScoreBoard!
gameScore = 100
\schedule (deltaTime) ->
.updateScore gameScore compiles to: local _anon_func_0 = function(ScoreBoard)
local _with_0 = ScoreBoard()
local gameScore = 100
_with_0:schedule(function(deltaTime)
return _with_0.updateScore(gameScore)
end)
return _with_0
end
onEvent("start", function()
return gameScene:addChild(_anon_func_0(ScoreBoard))
end) But in another case: onEvent "start", ->
gameScore = 100
-- the "with" expression below can not be optimized due to capturing the upvalue "gameScore"
gameScene\addChild with ScoreBoard!
\schedule (deltaTime) ->
.updateScore gameScore compiles to: onEvent("start", function()
local gameScore = 100
return gameScene:addChild((function()
local _with_0 = ScoreBoard()
_with_0:schedule(function(deltaTime)
return _with_0.updateScore(gameScore)
end)
return _with_0
end)())
end)
-- the case to optimize
exe_func = (func, env) ->
ok, ... = try
debug_env_before(env)
func(env)
debug_env_after(env)
catch ex
-- accessed both 'ex' and 'error'
error ex
return ex
if ok
return ...
else
os.exit(1) compiles to: local _anon_func_0 = function(os, _arg_0, ...)
do
local ok = _arg_0
if ok then
return ...
else
return os.exit(1)
end
end
end
local _anon_func_1 = function(debug_env_after, debug_env_before, env, func)
do
debug_env_before(env)
func(env)
return debug_env_after(env)
end
end
local exe_func
exe_func = function(func, env)
-- get no way to pass the global variable 'error'
-- so we have to keep this anonymous callback function below
return _anon_func_0(os, xpcall(_anon_func_1, function(ex)
error(ex)
return ex
end, debug_env_after, debug_env_before, env, func))
end
|
Yes! There is the case that Yue does not call the function itself, but if function require access and modify the closure, then we can not do anything about it. |
Yue have some nice features/syntax that make it a joy to work with like existence ?, nil coalescing ??, backcalls, destruct vargs... . But it also has big draw back that make it a bad choice for me to writing performance code with it because it auto create a new function every time a function called, this is a big performance issue. So I intent to avoid using it as much as possible.
It is a big shame that I can not use it when it very nice to have. So I intent to make a proposal to fix this issue.
We can move yue auto generated anonymous function to a upvalue named with prefix '__' in the same scope with parent function so that it with not generate a new function every time parent called, this generate a more better performance.
In many cases, we could send variables of closures as function parameters and get modified variable as function return results.
Well... let take the look at the example below to see what I mean.
To default Lua:
Maybe to Lua without inner function:
Another more complex example:
To Lua with poor performance:
To Lua with better performance:
I may make some mistake in the rush, but I hope you can get the idea.
Well... this solution is not work with all cases but aleast it will save a lot of performance in some cases. And I think it is worth to try.
Thanks and regards.
The text was updated successfully, but these errors were encountered: