English is not my native language so can you please expand on that? "every time closure is generated" what is a closure in this scenario? usernameBox ?
And I assume the functions which you are talking about are new,draw and init? Do you mean new ones are created when I create usernameBox for example?
Can I cram all of this into a table?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 234
- Joined: Mon Aug 29, 2016 8:51 am
- zorg
- Party member
- Posts: 3444
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Can I cram all of this into a table?
I think raidho was referencing MasterLee's solution, the one that mentions closures.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Can I cram all of this into a table?
Every time the code that defines a function runs, new function is generated. So if you put such code in top level, it only runs once, creating one function instance. But inside another function it runs every time that function runs, creating new function instance every time. Same as with tables!
If a function uses locals that were defined out of it's scope (e.g. just before it), it's called a closure, and the external variables are called upvalues. The closure will remember its upvalues, even if they are no longer accessible or even stopped existing. And if closure changes their value in runtime, it will remember that too the next time you call it.
If a function uses locals that were defined out of it's scope (e.g. just before it), it's called a closure, and the external variables are called upvalues. The closure will remember its upvalues, even if they are no longer accessible or even stopped existing. And if closure changes their value in runtime, it will remember that too the next time you call it.
-
- Party member
- Posts: 234
- Joined: Mon Aug 29, 2016 8:51 am
Re: Can I cram all of this into a table?
Ah, you were referring to MasterLee's code, yeah, got it.
Re: Can I cram all of this into a table?
But keep in mind that tables take more memory amount then using upvalues. So when you use many variable and have less code you may save memory.
Re: Can I cram all of this into a table?
No actually even very simple closures consume way more memory than tables.
Code: Select all
local array, memory
local ClassT, ClassC = { }, { }
ClassT.__index, ClassC.__index = ClassT, ClassC
function ClassT.new ( x, y )
local self = setmetatable ( { }, ClassT )
self.x = x
self.y = y
return self
end
function ClassT:print ( )
print ( self.x, self.y )
end
function ClassC.new ( x, y )
local self = setmetatable ( { }, ClassT )
function self:print ( ) print ( x, y ) end
return self
end
array = { }
collectgarbage ( )
memory = collectgarbage ( 'count' )
for i = 1, 2 ^ 10 do
array[ i ] = ClassT.new ( math.random ( ), math.random ( ) )
end
print ( 'Table Class', collectgarbage ( 'count' ) - memory )
array = { }
collectgarbage ( )
memory = collectgarbage ( 'count' )
for i = 1, 2 ^ 10 do
array[ i ] = ClassC.new ( math.random ( ), math.random ( ) )
end
print ( 'Closure Class', collectgarbage ( 'count' ) - memory )
Code: Select all
Table Class 91.7314453125
Closure Class 164.0078125
Re: Can I cram all of this into a table?
Code: Select all
local array, memory
local ClassT, ClassC = { }, { }
ClassT.__index, ClassC.__index = ClassT, ClassC
function ClassT.new ( x, y )
local self = setmetatable ( { }, ClassT )
self.x = x
self.y = y
return self
end
function ClassT:print ( )
print ( self.x, self.y )
end
function ClassC.new ( x, y )
local self = setmetatable ( { }, ClassC )
function self:print ( ) print ( x, y ) end
return self
end
function makeClosure ( x, y )
return function ( ) print ( x, y ) end
end
array = { }
collectgarbage ( )
memory = collectgarbage ( 'count' )
for i = 1, 2 ^ 10 do
array[ i ] = ClassT.new ( math.random ( ), math.random ( ) )
end
print ( 'Table Class', collectgarbage ( 'count' ) - memory )
array = { }
collectgarbage ( )
memory = collectgarbage ( 'count' )
for i = 1, 2 ^ 10 do
array[ i ] = ClassC.new ( math.random ( ), math.random ( ) )
end
print ( 'Closure Class', collectgarbage ( 'count' ) - memory )
array = { }
collectgarbage ( )
memory = collectgarbage ( 'count' )
for i = 1, 2 ^ 10 do
array[ i ] = makeClosure ( math.random ( ), math.random ( ) )
end
print ( 'Closure', collectgarbage ( 'count' ) - memory )
Code: Select all
Table Class 91.681640625
Closure Class 164.0078125
Closure 84.0078125
Re: Can I cram all of this into a table?
But you didn't use the class it was supposed to go with. The argument was that you could use a closure instead of metamethod and save memory, which is not true: class instance with closure on it consumes more memory than class with extra variables and metatable.
Re: Can I cram all of this into a table?
Class instance with closure makes no sense at all. Use Classes or Closures. For the code the was in opening post it is an alternative.
Re: Can I cram all of this into a table?
Ah, right! Fair enough!
Who is online
Users browsing this forum: No registered users and 198 guests