Skip to content

Map assets instead of reading the whole file on every open - #157

Open
xRookieFight wants to merge 1 commit into
minecraft-linux:masterfrom
xRookieFight:fix/asset-mmap
Open

xRookieFight wants to merge 1 commit into
minecraft-linux:masterfrom
xRookieFight:fix/asset-mmap

Conversation

@xRookieFight

Copy link
Copy Markdown

Joining a server has been taking 15 to 20 seconds (sometimes much longer) on the "Loading resources" / "Connecting to server" screen since roughly 1.26.20. Other launchers on the same machine and network join instantly.

Cause

Newer game versions pack textures into large .brarchive files (for example vanilla/__brarchive/textures/ui.brarchive is about 16 MB and vanilla_1.21.90/__brarchive/textures/blocks.brarchive about 5.7 MB). While applying the server's resource pack stack, the game opens these archives once per entry through AAssetManager_open.

fake_assetmanager read the entire file into a std::string on every open and then copied it once more into the AAsset. With strace attached during a single join I counted around 19k archive opens, which adds up to roughly 73 GB read from disk plus the same amount copied in memory. Sampling /proc/<pid>/io without strace showed rchar growing by about 1.4 GB/s for the whole loading screen and stopping exactly when the player spawned.

On Android, uncompressed assets are memory mapped, so these repeated opens cost almost nothing there.

Fix

AAssetManager_open now mmaps the file read only and AAsset reads from the mapping. AAsset_getBuffer returns the mapped pointer, so there is no read and no copy per open, and pages are only touched when the game actually reads them.

AAsset_getBuffer used to return a NUL terminated buffer, and some callers may treat it as text. The zero filled tail of the last mapped page keeps that guarantee, except when the size is an exact multiple of the page size. Those files and empty files are still read into a buffer.

Testing

Built inside the flatpak SDK (org.kde.Sdk 6.10, clang 20) from mcpelauncher-manifest ef9ceb8e and ran it with game version 1.26.51.1 against a server with a custom resource pack. The loading screen went from tens of seconds to an instant join, and textures, UI and the server pack render normally.

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