Can I cram all of this into a table?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Can I cram all of this into a table?

Post by KayleMaster »

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?
User avatar
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?

Post by zorg »

I think raidho was referencing MasterLee's solution, the one that mentions closures.
Me and my stuff :3True 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can I cram all of this into a table?

Post by raidho36 »

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.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Can I cram all of this into a table?

Post by KayleMaster »

Ah, you were referring to MasterLee's code, yeah, got it.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Can I cram all of this into a table?

Post by MasterLee »

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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can I cram all of this into a table?

Post by raidho36 »

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
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Can I cram all of this into a table?

Post by MasterLee »

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 )
Actually it comes very close

Code: Select all

Table Class	91.681640625
Closure Class	164.0078125
Closure	84.0078125
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can I cram all of this into a table?

Post by raidho36 »

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.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Can I cram all of this into a table?

Post by MasterLee »

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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can I cram all of this into a table?

Post by raidho36 »

Ah, right! Fair enough!
Post Reply

Who is online

Users browsing this forum: No registered users and 198 guests