As I've been reading around game dev boards, seeing a lot about front-loading arrays (tables) with common objects when the game boots. Then just cycling through those arrays to find an unused object and update its variables...essentially just recycling the same objects over and over. I get it and I'm doing that but...
Right now for my shooter game I'm building my array of objects as-needed. So rather than seed a table with 50 bullet objects at the start, I'm adding to that array as bullets are created, then reusing slots as they get deactivated.
Both methods use recycling so that's good. My method is working fine but wondering if there are any big gains in doing the pre-load at boot over build-as-you-go. Or if it's negligible to even worry about.
Allocating memory is mildly expensive. Depending on the language/environment/os doing it hundreds of times per frame could be very expensive. Doing it hundreds of times throughout a program's runtime is not.
The other issue that pre-allocation prevents is memory fragmentation, and that's not something you need to worry about with Pico8.
Pico 8 use so little memory than garbage collect doesnt take a lot of time. So creating and destroying object is prety fast. Plus everything in lua is dictionnaries, so there is not realy much sense to preallocate an object of one type. The way you do your bullet is good, but you could try to just delete and add bullet to the array instead, and you will probably not lose much. Sometimes, it's even slower, because you have to search an empty place manualy in lua instead of leaving that to the add/del function.
Adding to that: My general advice about memory pooling is make sure you are actually solving a problem instead of creating new ones. ;)
It's really easy to make mistakes when pooling memory where on object doesn't get properly reset. These bugs tend to be hard to find since they usually don't happen until an object is used a couple times. On the other hand, adding memory pooling where it's not needed will have minimal performance improvements.
I'd like to add this to what slembcke and NuSan have said: http://c2.com/cgi/wiki?PrematureOptimization
[Please log in to post a comment]