Nil vs False, memory question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Luska72
Prole
Posts: 18
Joined: Thu Jan 03, 2013 6:14 pm

Nil vs False, memory question

Post 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:

Code: Select all

 if num ~= 1 then print('not 1!') 
and

Code: Select all

 if not num == 1 then print('not 1!') 
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Nil vs False, memory question

Post 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.
Beta Carotene
Prole
Posts: 7
Joined: Thu Jan 17, 2013 11:28 pm

Re: Nil vs False, memory question

Post 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.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Nil vs False, memory question

Post 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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Luska72
Prole
Posts: 18
Joined: Thu Jan 03, 2013 6:14 pm

Re: Nil vs False, memory question

Post 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!
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Nil vs False, memory question

Post by MarekkPie »

The function is:

Code: Select all

collectgarbage('count')
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?
Beta Carotene
Prole
Posts: 7
Joined: Thu Jan 17, 2013 11:28 pm

Re: Nil vs False, memory question

Post 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
Last edited by Beta Carotene on Fri Jan 18, 2013 3:40 am, edited 1 time in total.
Beta Carotene
Prole
Posts: 7
Joined: Thu Jan 17, 2013 11:28 pm

Re: Nil vs False, memory question

Post by Beta Carotene »

yeah, sorry I just edited my message lol. I didn't recall seeing that about the function.
Luska72
Prole
Posts: 18
Joined: Thu Jan 03, 2013 6:14 pm

Re: Nil vs False, memory question

Post by Luska72 »

MarekkPie wrote:The function is:

Code: Select all

collectgarbage('count')
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)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests