Inefficiencies with loading data

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.
Post Reply
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Inefficiencies with loading data

Post by Mr. Strange »

Alright, my latest love app is getting to the stage where I have (potentially) a whole lot of data to load. So I'm trying to get my data to be as efficient as possible.

Some background:

My application deals with "cards" of information. There are 2000+ cards, each with unique data, including multiple unique images. However, each running instance of the application needs only a subset of about 100 cards loaded. My goal is to get the data for those 100 cards loaded, without simply loading all data for every card.

In early development, when I was just loading a few images, I had a stack of load calls like this:

love.graphics.newImage("fooA")
love.graphics.newImage("fooB")

But if I stick with this system, I'll need to load every image, since I don't know at this point which images I'll need. My idea is to load the images later, when I've already isolated my set of cards. I'm saving data in each card object like this:

imageFilename = "fooA"

And then later when setting up my table of cards I do:

image = love.graphics.newImage(card[cardID].imageFilename)

But this has me wondering - if I instantiate multiple copies of the same card, will I be creating redundant copies internally? Should I try to do some sort of check, and point each new image property at previous loads of that filename, if any? I hope that question is clear.

My second question is - is there a way in lua to unload data? Would io.close be of use? I've never used it before, because my memory footprint has never been significant in any of my past lua projects.

Thanks in advance-
--Mr. Strange
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Inefficiencies with loading data

Post by Kuromeku »

Code: Select all

-- Get an image or create it if it doesn't exist.
function envy.util.getImage(image, full)
	if (not full) then image = "game/"..image; end;
	
	-- Check if the image is valid.
	if ( envy.util.images[image] ) then
		return envy.util.images[image];
	else
		envy.util.images[image] = love.graphics.newImage(image);
		
		-- Return the image.
		return envy.util.images[image];
	end;
end;

-- Get music or create it if it doesn't exist.
function envy.util.getMusic(music, full)
	if (not full) then music = "game/"..music; end;
	
	-- Check if the music is valid.
	if ( envy.util.music[music] ) then
		return envy.util.music[music];
	else
		envy.util.music[music] = love.audio.newMusic(music);
		
		-- Return the music.
		return envy.util.music[music];
	end;
end;

-- Get a sound or create it if it doesn't exist.
function envy.util.getSound(sound, full)
	if (not full) then sound = "game/"..sound; end;
	
	-- Check if the sound is valid.
	if ( envy.util.sounds[sound] ) then
		return envy.util.sounds[sound];
	else
		envy.util.sounds[sound] = love.audio.newSound(sound);
		
		-- Return the sound.
		return envy.util.sounds[sound];
	end;
end;
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Inefficiencies with loading data

Post by surtic »

Just a tiny suggestion (to Kudomiku):
Your code is something like

Code: Select all

  if (x) then
    return x
  else
    x = ...
    return x
  end
That can be written as

Code: Select all

  if not x then
    x = ...
  end

  return x
Then you don't repeat the same line twice.

Also, I don't think you need those semi-colons (or the brackets around the condition).
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: Inefficiencies with loading data

Post by Mr. Strange »

My question is whether or not calling newImage() on the same file eats up memory or not. It's not a question of how to avoid redundancy, but rather a question of whether or not my script as it stands creates any memory redundancy already.

And my question about "unloading" data from memory is still open.

--Mr. Strange
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Inefficiencies with loading data

Post by Kuromeku »

surtic wrote:Just a tiny suggestion (to Kudomiku):
Your code is something like

Code: Select all

  if (x) then
    return x
  else
    x = ...
    return x
  end
That can be written as

Code: Select all

  if not x then
    x = ...
  end

  return x
Then you don't repeat the same line twice

Also, I don't think you need those semi-colons (or the brackets around the condition).
Thanks for the suggestion but not only is that my coding style, it is also good habbit for C++.

(I'm referring to the semi-colons and the brackets around the conditions).
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Inefficiencies with loading data

Post by osgeld »

Kudomiku wrote: Thanks for the suggestion but not only is that my coding style, it is also good habbit for C++.

(I'm referring to the semi-colons and the brackets around the conditions).
i do it too
User avatar
Merkoth
Party member
Posts: 186
Joined: Mon Feb 04, 2008 11:43 pm
Location: Buenos Aires, Argentina

Re: Inefficiencies with loading data

Post by Merkoth »

Mr. Strange wrote:My question is whether or not calling newImage() on the same file eats up memory or not. It's not a question of how to avoid redundancy, but rather a question of whether or not my script as it stands creates any memory redundancy already.

And my question about "unloading" data from memory is still open.

--Mr. Strange
When you call newImage LÖVE will load the file and store it as a texture in video RAM. By the way, io.close closes file handlers, not the memory buffer where you put the loaded data. Most languages behave this way. And you should use LÖVE's filesystem functions (love.filesystem.blah).

I'm not sure if LÖVE keeps track of already loaded images, but I'm sure there's no way to "unload" an image.

PS: Asking something and receiving replies about something different is not very cool. And this is a cool forum. A very cool one.
User avatar
mike
Administrator
Posts: 364
Joined: Mon Feb 04, 2008 5:24 pm

Re: Inefficiencies with loading data

Post by mike »

As far as I know (not being the one who coded this), we used smart pointers to do this:

Code: Select all

hello = love.graphics.newImage("hello.jpg") -- creates Image object that contains info about hello
hello = love.graphics.newImage("goodbye.jpg") -- creates new image object, clearing out any memory used by the original
However, since images are loaded into the GPU, I have no idea about how OpenGL handles such things or if we handle such things. I am surprised that rude hasn't voiced his opinion here yet since he knows more than all of us...
Now posting IN STEREO (where available)
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

Re: Inefficiencies with loading data

Post by subrime »

In general (for lua) if you want to 'free up' memory that you don't need anymore, you assign nil to the variable. eg:

Code: Select all

file=io.open('love_manual') -- file is just a way to specify your io source
text=file:read('*a') -- put the contents of file into text... this chews up memory
file:close()  -- done with the file. text is still chewing up memory
text=nil  -- lua can now reuse the memory
In theory this is enough, and the automatic garbage collection system in lua will (at some point) reclaim the memory. In practice this may not happen soon enough, and you can force lua to do its reclamation cycle with:

collectgarbage('collect')
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], casual_dodo, Google [Bot] and 5 guests