Skip to content

Fix game freeze when pressing Play Game with multiple saves#430

Open
MCbabel wants to merge 1 commit intosmartcmd:mainfrom
MCbabel:fix/play-game-freeze
Open

Fix game freeze when pressing Play Game with multiple saves#430
MCbabel wants to merge 1 commit intosmartcmd:mainfrom
MCbabel:fix/play-game-freeze

Conversation

@MCbabel
Copy link
Contributor

@MCbabel MCbabel commented Mar 4, 2026

Description

Fix game freeze when pressing "Play Game" with multiple saved worlds. The freeze duration scaled linearly with the number of saves.

Changes

Previous Behavior

Pressing "Play Game" caused the game to freeze for several seconds. The freeze worsened with more saved worlds (e.g., 3+ saves caused noticeable freezing, 10+ saves caused multi-second hangs).

Root Cause

ReadLevelNameFromSaveFile() in UIScene_LoadOrJoinMenu.cpp was called synchronously for every save in a single frame during the tick() function. This function reads entire save files (10-50+ MB each), decompresses them if compressed, and parses NBT data to extract the level name — all on the main thread in one go.

New Behavior

The save list appears immediately with fallback names (save folder timestamps). Real level names are loaded progressively, one save per tick, using setButtonLabel() to update each entry. No freeze occurs regardless of the number of saves.

Here is a short video showing the behavior before and after: https://youtu.be/5VKAjRbnsX4
It's hard to see, but it really becomes very noticeable with many worlds.

Fix Implementation

  • Added m_iDeferredNameIndex and m_sortedSaveIdx member variables to UIScene_LoadOrJoinMenu to track deferred loading state.
  • Modified the save display loop to show fallback names immediately instead of calling ReadLevelNameFromSaveFile() for each save.
  • Added a new per-tick block that processes one save name per frame: reads the save file, extracts the level name, and updates the button label via setButtonLabel().
  • The sorted index array is retained until all names are loaded, then freed.

Related Issues

@leahanderson1
Copy link

This is an issue with all versions of legacy IIRC, would love to see this fix be ported over to more platforms

@MCbabel
Copy link
Contributor Author

MCbabel commented Mar 4, 2026

Yes, I kind of thought that it was a general problem

@void2012 void2012 added bug Something isn't working gui priority: high divergence Divergence from the main game (Java Edition PC) labels Mar 4, 2026
@codeHusky
Copy link
Collaborator

I'm not sure this is the best fix for this realistically. It'd make more sense to resolve whatever part of the routine is taking so long to simply read a world name rather than potentially having visual lag on loading the world names IMO.

@MCbabel
Copy link
Contributor Author

MCbabel commented Mar 4, 2026

you're right, I'm working on a better fix for this bug.

@MCbabel MCbabel force-pushed the fix/play-game-freeze branch from 0566d15 to f93fbbf Compare March 4, 2026 23:32
Optimize ReadLevelNameFromSaveFile() to avoid reading entire save files into memory.

For uncompressed saves: use seek-based file I/O to read only the header, file table, and level.dat entry (~100 KB instead of 10-50+ MB).

For compressed saves: use a levelName.cache sidecar file so the full decompression only happens once; subsequent loads read the small cache file instantly.

Previously, the function read and decompressed entire save files on the main thread for every save in a single frame, causing a multi-second freeze that scaled with the number of saves.

Fixes smartcmd#409
@MCbabel MCbabel force-pushed the fix/play-game-freeze branch from bb05069 to cb498a9 Compare March 4, 2026 23:43
@MCbabel
Copy link
Contributor Author

MCbabel commented Mar 4, 2026

Well, that should be a better fix. Let me know what you think. @codeHusky

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working divergence Divergence from the main game (Java Edition PC) gui priority: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Main Menu stutter

4 participants