Environment: Lua, Windows, Simulator
In one of my scenes there are shooting turrets. And I noticed framerate degradation over time.
Of course I double-checked the number of sprites and entities and it was stable (as designed)
But still framerate was dropping. I tried to use the Sampler and it was obvious that cpu load was growing. And the top most function was some untitled (C) and then it were calls into a native Rect. __index accessing its fields and offsetBy method I was calling several times.
I have two questions:
- The most important one: Why can performance degrade while the number of entities stays stable?
- Why calling native methods of the Rect are slow?
Here is the minimal example of the problem:
local rc = playdate.geometry.rect.new(0, 0, 100, 100)
function playdate.update()
local sumx = 0
local sumy = 0
local sumw = 0
local sumh = 0
for i = 1, 300 do
local copy = rc:offsetBy(10, 10)
sumx += copy.x
sumy += copy.y
sumw += copy.width
sumh += copy.height
end
end
And its sampling over time:
NOTE: I checked collectgarbage("count") and memory is stable but FPS drops with time.
UPDATE:
Rewriting example to pure Lua code gives similar picture, so looks like I was wrong assuming that Lua can properly handle garbage created every frame (only 300 tables per frame here). Strange thing is this example crashes or hangs Sampler for me:
local rc = { x = 0, y = 0, w = 100, h = 100 }
function playdate.update()
local sumx = 0
local sumy = 0
local sumw = 0
local sumh = 0
for i = 1, 300 do
local copy = { x = rc.x + 10, y = rc.y + 10, w = rc.w, h = rc.h }
sumx += copy.x
sumy += copy.y
sumw += copy.w
sumh += copy.h
end
end
