Optimization for multiple sprites moving on screen at once

,

I noticed the sampler indicates almost all of the load is from C code, so I tried the C sampler which says it’s mostly from MC2S and FDC32. From a google search, these appear to be related to MP3 decoding, so I’m guessing there’s something about your music file that the Playdate can barely handle. Sure enough, turning off music results in a constant 30fps with low CPU load. I’m not sure what the proper optimization steps for music are, other than using ADPCM instead of MP3.

That said, I glanced at your code and noticed a few other things that could probably be optimized, although they don’t seem to be causing any problems with music off (with a lot of balls on the screen, CPU load is around 30% and I don’t notice any stuttering), so it probably isn’t necessary to worry about them for now:

  1. Some of your objects create a new image in :init for every instance – I noticed this on at least Projectile, Powerup, and Particle. You could save memory and processing by just creating the image(s) at load time and sharing that between every instance. It looks you’re already doing this for the player and targets by creating a file-local image and having all instances reference that. For Particle, if it’s just a solid rectangle, I’m not sure but it might be slightly faster to implement Particle:draw to just use gfx.fillRect directly rather than using an image at all.

  2. It looks like you’re initializing new sprites frequently during gameplay, most notably Target, Projectile, and Particle. These seem like good candidates for object pooling where you create as many as you might need on load, and then :add() or :remove() them from the draw list as needed (and reset any properties back to their initial values) instead of constantly creating new ones (note: that link isn’t Playdate or Lua-specific, but the principles still apply). At the very least, any performance hit from not pooling objects is worsened by also creating a new image for every instance (see #1).

1 Like