Page 1 of 2
Nil vs False, memory question
Posted: Fri Jan 18, 2013 12:21 am
by Luska72
I am currently working on a very basic 2d side-scroller game. My map is nothing more than a 2-d array, and most of that is just air.
I was wondering if I would it would somehow be more 'memory efficient' to change all the 0s in the table to nil (leave them blank).
I could still check if it was an 'air' block with something like:
Code: Select all
Item = level[y][x]
if item ~= nil then
--draw block and stuff
end
but I'm not sure if that would be more efficient, or plain silly!
Any help would be great, thank you very much!
PS: what is the difference between:
and
Code: Select all
if not num == 1 then print('not 1!')
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 1:00 am
by veethree
That sounds like it should be fine to do that with nil.
As far as memory efficiency goes, I'm not actually sure. But it sounds logical that it would be more efficient as the table will be smaller, so you have to loop through fewer items when drawing and stuff. I don't think the difference is gonna be too major though. (unless you're dealing with huge maps)
As far as the difference between not and ~=, I'm not sure, in fact I'm interested in knowing that too.
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 2:03 am
by Beta Carotene
hmmmm... Actually yeah it should be!
At first glance it sounds silly, but since lua tables are not contiguous arrays (but rather hash tables), the amount of memory taken up is determined by how many non-nil entries are in the table.
One thing I do remember is that, under normal circumstances, lua will not shrink the memory that's already allocated to a table, so you need to make sure that those empty spaces never get allocated in the first place (as opposed to creating all of them, then running back through and setting them all to nil).
I forget if lua offers a function for this from script alone, but I know that in C, there's a function call that lets you get how much memory (in bytes) is being used by the virtual machine. Try to see if you can get that number, divide by 1,000,000 to convert to megabytes, and then compare how much memory is used for filling with nil vs false. The difference should be quite large if you have enough entries.
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 2:59 am
by ejmr
veethree wrote:As far as the difference between not and ~=, I'm not sure, in fact I'm interested in knowing that too.
The 'not' and '~=' operators have different semantics. The first is a logical operator, which is true if the expression it preceeds is equal to false or nil. The second, on the other hand, compares two things by value if they are numbers or strings, and compares everything else by reference (e.g. tables). It is also possible to redifine the meaning of '~=' for a table by writing a custom __eq function for the metatable of that table. This works because Lua treats 'x ~= y' as if it were written 'not (x == y)', which would invoke that custom __eq function.
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 3:10 am
by Luska72
Wow, awesome replies! Thank you very much, I'll try out that megabytes thing and see how much memory I do save, will be fun to see!
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 3:29 am
by MarekkPie
The function is:
To answer the original question, you probably shouldn't really need to check if something is 'air' or not. What is the particular reason you need to do so?
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 3:39 am
by Beta Carotene
collect garbage will perform a full garbage collection run. I don't think it returns info. I did however just find the gcinfo() function. According to some scarce documentation, it returns two arguments: the number of bytes used and the capacity of the collector. That should do it!
EDIT: ah! Yes, collectgarbage can do that with the 'count' argument. Sorry for the confusion lol
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 3:40 am
by MarekkPie
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 3:41 am
by Beta Carotene
yeah, sorry I just edited my message lol. I didn't recall seeing that about the function.
Re: Nil vs False, memory question
Posted: Fri Jan 18, 2013 4:45 am
by Luska72
MarekkPie wrote:The function is:
To answer the original question, you probably shouldn't really need to check if something is 'air' or not. What is the particular reason you need to do so?
If I want to check if I can jump, I need to make sure the block above me is air. Stuff like that, because air has no properties of it's own, it could still be nil (i think)