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
Inefficiencies with loading data
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: 101
- Joined: Mon Aug 11, 2008 5:19 am
Re: Inefficiencies with loading data
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;
Re: Inefficiencies with loading data
Just a tiny suggestion (to Kudomiku):
Your code is something like
That can be written as
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).
Your code is something like
Code: Select all
if (x) then
return x
else
x = ...
return x
end
Code: Select all
if not x then
x = ...
end
return x
Also, I don't think you need those semi-colons (or the brackets around the condition).
-
- Party member
- Posts: 101
- Joined: Mon Aug 11, 2008 5:19 am
Re: Inefficiencies with loading data
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
And my question about "unloading" data from memory is still open.
--Mr. Strange
Re: Inefficiencies with loading data
Thanks for the suggestion but not only is that my coding style, it is also good habbit for C++.surtic wrote:Just a tiny suggestion (to Kudomiku):
Your code is something likeThat can be written asCode: Select all
if (x) then return x else x = ... return x end
Then you don't repeat the same line twiceCode: Select all
if not x then x = ... end return x
Also, I don't think you need those semi-colons (or the brackets around the condition).
(I'm referring to the semi-colons and the brackets around the conditions).
Re: Inefficiencies with loading data
i do it tooKudomiku 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).
Re: Inefficiencies with loading data
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).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
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.
Teh Blog -> http://cryodreams.com.ar
Re: Inefficiencies with loading data
As far as I know (not being the one who coded this), we used smart pointers to do this:
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...
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
Now posting IN STEREO (where available)
Re: Inefficiencies with loading data
In general (for lua) if you want to 'free up' memory that you don't need anymore, you assign nil to the variable. eg:
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')
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
collectgarbage('collect')
Who is online
Users browsing this forum: Amazon [Bot] and 1 guest