Skip to content

Handhelds: the later levels fit (only used textures, level freed before the next cutscene, smaller renderer and task state) - #3

Merged
finger563 merged 1 commit into
mainfrom
perf/df-level-memory
Nov 22, 2026
Merged

finger563 merged 1 commit into
mainfrom
perf/df-level-memory

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

On the ESP32-S3-BOX, JABSHIP (mission 10) couldn't be loaded at all: loading KELL.WAX ran out of memory. Finishing NARSHADA crashed in the next mission's cutscene. All changes are under TFE_ESPBOX:

  • Only the textures a level uses are loaded (level_loadUsedTextures()). Loading happens once the sectors and walls are read.
    • A level's texture list can name many textures it never draws: JABSHIP lists 359 and draws 51, which is 1.1 MB.
    • The other slots stay null, like <NoTexture>.
    • INF only copies textures from one sector or wall to another, so it never needs an unused slot.
    • Old save states, which list every texture, still load, as do new saves with null slots.
  • The level is freed before the next mode starts at mission end, not after. The next mission's cutscene (jabba1, about 1 MB) no longer has to fit next to the level that just ended (NARSHADA, 1.7 MB).
  • adjoinEdgeList has MAX_ADJOIN_SEG entries. wall_addAdjoinSegment() keeps the index below that for the whole frame. That's −253 KB of renderer state.
  • Task stacks are 2 KB. They hold the local contexts of up to 16 nested task functions, and none is over 64 bytes, so the worst case is 1 KB. That's −96 KB per chunk of 16 tasks, and ctxAllocate() still asserts that they fit.
  • Loading a save state frees the loaders' read buffers (espbox_free_loader_scratch()), as a level load does.

Testing on the box

This was tested with the code overlay (esp-cpp/esp-box-emu#145). A temporary test hook, not committed, typed cheats into the game.

Before and after, PSRAM free:

before after
Talay 1275 KB 1778 KB (render 13.0 ms/frame, unchanged)
finishing NARSHADA (LASKIP) crash in the jabba1 cutscene jabba1 plays, JABSHIP loads (1329 KB)
JABSHIP directly (warp, or from the agent menu) crash (out of memory) 1762 KB, 51 of 359 textures

The whole campaign goes from the Talay save, LASECBASE, then LASKIP mission after mission, through the ending cutscenes and the credits. Each line is free PSRAM in the level (largest free block):

SECBASE 1962 KB (1760) · TALAY 1637 (1536) · SEWERS 1914 (1536) · TESTBASE 1689 (1536) · GROMAS 1700 (1536) · DTENTION 1461 (1216) · RAMSHED 1766 (1632) · ROBOTICS 1425 (976) · NARSHADA 1341 (992) · JABSHIP 1317 (592) · IMPCITY¹ 1156 (848) · FUELSTAT 1469 (1024) · EXECUTOR 1222 (880) · ARC 787 (400) · ending 1802

There was no crash and no failed allocation in the whole run.

  • Saves: a save made in ARC loads back in ARC (795 KB) and from the title (925 KB).

¹ In JABSHIP no typed cheat had any effect (LASKIP, LAPOSTAL), while they worked in the nine levels before it. So the chain continued with a warp to IMPCITY, and the JABSHIP → IMPCITY transition is the one that wasn't exercised. This only affects the test hook's typed cheats, which the handhelds can't reach otherwise. I haven't found the cause.

Review

A separate read-only review traced every user of level texture slots: INF stops and triggers, animated textures, serialization in all three old/new directions. It also traced every startNextMode() branch after the earlier free, the adjoin index bound, and the worst-case task context size. It found no defects.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

…re the next cutscene, smaller renderer and task state)

On the ESP32-S3-BOX, JABSHIP (mission 10) could not be loaded at all (out of
memory loading KELL.WAX), and finishing NARSHADA crashed in the next
mission's cutscene. With TFE_ESPBOX:

- Only the textures the sectors and walls use are loaded, once they are read
  (level_loadUsedTextures()): a level's list can name many it never draws
  (JABSHIP lists 359 and draws 51: 1.1MB). The other slots stay null like
  <NoTexture>; INF only copies textures from one sector or wall to another,
  and old save states (which list every texture) still load.
- At mission end the level is freed before the next mode starts, not after:
  the next mission's cutscene (jabba1: ~1MB) no longer has to fit next to the
  level that just ended (NARSHADA: 1.7MB).
- adjoinEdgeList has MAX_ADJOIN_SEG entries: wall_addAdjoinSegment() keeps the
  index below that for the whole frame (-253KB of renderer state).
- Task stacks are 2KB: they hold the local contexts of up to 16 nested task
  functions, none over 64 bytes (-96KB per chunk of 16 tasks; ctxAllocate()
  still asserts that they fit).
- Loading a save state frees the loaders' read buffers too, as a level load
  does (espbox_free_loader_scratch()).

On the box, with the code overlay (PSRAM free):
- Talay 1275 KB -> 1778 KB, render time unchanged (13.0 ms)
- NARSHADA 1080 KB -> 1431 KB; finishing it with LASKIP plays jabba1 and
  loads JABSHIP (1329 KB free), where it crashed before
- JABSHIP directly (warp from Talay, or from the agent menu): crashed ->
  1762 KB free, 51 of 359 textures loaded
- ARC (mission 14) 937 KB free; a save made in ARC loads back in ARC
  (795 KB free) and from the title (925 KB)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant