I'm doing a basic look into how coroutines work, and have a question about performance.
As has been noted elsewhere, in general we want to optimize our Playdate code to not require any lengthy loading screens when possible... but in this case it's a simple application that needs all of its image data loaded first before it can be functional.
In this example, I'm using the :setImage()
function to load sequential image table data manually in a for loop (instead of using imagetable.new()
directly to load all at once), just so that I have access to the progress of the loading and can expose that in a progress bar of some sort.
The issue is performance. On device testing:
- with a folder containing 720 images, about 8mb worth of 250x250 12kb images
- Without using a coroutine, it takes the Playdate about 3 seconds or less to load the
imagetable.new()
into memory - Using the coroutine and
:setImage()
instead, it takes the Playdate 25 seconds to complete (equivalent performance on Simulator as well, if that's relevant information)
So the obvious question is, does :setImage(i, path)
run notably slower than imagetable.new()
, or is the coroutine/yield causing the slowdown?
Partial test code below. If anyone has any insight into this, let me know. Thank you!
function playdate.update()
if not imagesLoaded then
for i=1, 720 do
local progress = i/720
local path = gfx.image.new('assets/images/images-test/image' .. i)
imageTable:setImage(i, path)
gfx.drawArc(kDisplayWidth/2, kDisplayHeight/2, 105, 0, playdate.math.lerp(0, 360, progress))
coroutine.yield()
end
imagesLoaded = true
else
gfx.sprite.update()
-- ... rest of update code
end
end