The cost of drawing from code
In Godot, a _draw() function that calls draw_rect, draw_circle and draw_polygon is cheap to write and costs one draw call per shape, every time the node redraws. A food item is five to ten shapes. A decoration piece, like a sofa with cushions and legs, is around twenty. A tray showing a few dozen of those at once, each redrawn while it scrolls, adds up fast on a phone.
Step one: paint each piece once
The first fix was a small picture cache. The first time a piece of art is asked for, it is painted once into its own small off-screen picture (192 pixels on its longer side). From then on the card draws that picture: one draw call instead of twenty, and pictures of the same art batch together.
Two details mattered:
- A picture is ready two frames later, after the renderer has actually drawn it. Until then the card draws its art shape by shape as before, so nothing ever pops in empty, and it redraws itself once the picture is ready.
- At most two new pictures a frame. A drawer of about 50 cards making all its pictures at once was a single 170 ms frame on my computer. Spreading the work out keeps every frame short; the rest are asked for again later.
Shown larger than its picture, a piece is drawn shape by shape again, so it stays sharp when zoomed in.
Step two: the cache made one case worse
The furniture tab has 230 cards. When it opened, 18 pictures were painted ahead, and the cache made two more a frame. That is about a hundred frames before every picture exists. During those hundred frames, every card still waiting drew itself shape by shape, every frame, including the ones far below the visible part of the tray. The tray did the most expensive drawing exactly when the player was scrolling it.
The fallback that protected the screen from empty cards was now drawing hundreds of cards nobody could see.
The fix: three distances from the view
Each card now asks how far it is from the tray's visible area and falls into one of three bands:
- On screen: as before. Its picture if it is ready, shape by shape until it is.
- Within one screen: asks for its picture, but draws no art until it is ready. It is about to scroll into view, and the picture will be there in time.
- Farther than that: draws its tile, name and price, but no art, and asks for nothing. It marks itself as waiting.
The test is two rectangle checks: does the card intersect the scroll view's rectangle, and if not, does it intersect that rectangle grown by one screen height above and below.
Waking cards up
A waiting card has to be told when it comes near. Three signals do that: the scroll bar's value changed, the scroll area was resized, or the grid sorted its children (a tab switch or a filter). Each runs one small loop over the cards: any card that is waiting and now within one screen stops waiting and redraws. No per-frame polling, nothing runs while the tray is still.
The cache gained one flag for this, fallback: when it is off, the cache draws nothing until the picture is ready instead of drawing shape by shape. And one question, has_picture, so a far card that already has its picture can still show it for free.
The result
The tray scrolls smoothly from the first frame, and pictures are made in the order the player will see them, not in the order of the list. The decorating tests still pass, and the same pattern now applies anywhere a long list of drawn art scrolls.
The phone also got faster in a much bigger way the same day, for a reason that had nothing to do with drawing: switching iOS to Metal took the game from 60 to 120 fps. Both were needed. A renderer cannot save a list that draws a thousand shapes it does not show.
What I would reuse
- Cache art drawn from code into pictures, and limit how many you make in one frame.
- Watch the fallback. The path that runs "until it is ready" can be the most expensive one, and it can run for a hundred frames.
- Give list items a distance from the view, not just visible or not. "Near" is where the work should start.
- Wake on events, not on frames. Scroll, resize and layout signals are enough.