createafunc()
local newfunc = function()
-- do lots of stuff
end
return newfunc
end
Does the newfunc's code get recreated everytime internally when I call createfunc? Or is there only a simply variable assignment that happens behind the scenes when createafunc is called? (ie, the internal function is already defined when the file was executed, so newfunc is simply assigned to point to that location)
Basically what I want to know, is if there is going to be a big overhead every time I call createafunc if the newfunc in it is huge.
So im wondering if the internal operations is
newfunc = [function at this address]
return newfunc
or
newfunc = function that does this, this, then this and so on (as the functoin is defined)
return newfunc
I hope its understandable what I mean :).
Lua function nesting question
If I have a function that creates a function.
I put these functions in:
The first function fills a table with 10000 copies of the result of a function and prints the difference in memory use before and after.
f1 returns a function.
f2 returns a number.
f3 returns a more complex function than f1.
Then in the Lua interpreter I did:
The size of the function (f1 compared to f3) doesn't seem to make a difference so it looks like it's created once and referenced multiple times.
This analysis may be wrong, if so could someone point out the error or errors in my reasoning please.
Fulby
function testmem(func) collectgarbage(0) local store = {} local mem1 = gcinfo() for i = 1, 10000 do table.insert(store, func()) end local mem2 = gcinfo() print(mem2 - mem1)endfunction f1() local newfunc = function() end return newfuncendfunction f2() local newfunc = 1.0 return newfuncendfunction f3() local newfunc = function() local t = {} table.insert(t, 1) local text for a = 1, 200 do text = text .. tostring(a) .. " " end end return newfuncend
The first function fills a table with 10000 copies of the result of a function and prints the difference in memory use before and after.
f1 returns a function.
f2 returns a number.
f3 returns a more complex function than f1.
Then in the Lua interpreter I did:
> dofile("func.lua")> testmem(f1)607> testmem(f2)256> testmem(f3)607
The size of the function (f1 compared to f3) doesn't seem to make a difference so it looks like it's created once and referenced multiple times.
This analysis may be wrong, if so could someone point out the error or errors in my reasoning please.
Fulby
Thanks, I think your reasoning is correct.
Whats interesting though, is how both functions take up the same about of space in dynamic memory. Obviously the bytecode for a function is not stored in dynamic memory then?
Edit: thinking about it, it makes sense. Its just like in any other programming language. But in lua, there are only function pointers, since all functions can be re-assigned. These function pointers are stored in dynamic memory.
Whats interesting though, is how both functions take up the same about of space in dynamic memory. Obviously the bytecode for a function is not stored in dynamic memory then?
Edit: thinking about it, it makes sense. Its just like in any other programming language. But in lua, there are only function pointers, since all functions can be re-assigned. These function pointers are stored in dynamic memory.
If I remember the explanation in the Lua book right, each function is just a block of code...you're not recreating the function each time, but you ARE creating "closures" each time. Any variables that are local to that particular instance of the function are stored somewhere else.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement