Skip to content

optimization: async dedicated server autosaving#1473

Merged
sylvessa merged 1 commit intosmartcmd:mainfrom
sylvessa:optimize/async-autosave
Apr 5, 2026
Merged

optimization: async dedicated server autosaving#1473
sylvessa merged 1 commit intosmartcmd:mainfrom
sylvessa:optimize/async-autosave

Conversation

@sylvessa
Copy link
Copy Markdown
Collaborator

@sylvessa sylvessa commented Apr 5, 2026

Description

makes autosave async on the dedicated server so it doesnt block the tick thread during compression

Changes

Previous Behavior

calling Flush() on the server would block the entire tick thread while it compressed. No idea why this is ran on main thread especially in a server, I'm pretty suire this was changed later on. for large worlds this caused noticeable lag and made the server practically freeze during autosaves.

from 1 autosave on a 350mb world ran on the dedicated server (latest nightly)

8mb.video-DBu-nhFFpcs7.mp4

Root Cause

Flush() did all compression work synchronously. the tick thread had to sit and wait for zlib to finish compressing the full save buffer before it could continue. this caused the server to freeze for a few seconds depending on the world size and hardware.

New Behavior

Flush() snapshots pvSaveMem into a heap buffer under the critical section, releases the lock immediately, then hands the snapshot off to a seperate thread that does all the compression. once the bacjground thread finishes, the result gets picked up the main thread next tick and commits it to StorageManager.

also when server stops it waits until this all does its thing, i had ran into an issue where it would just do the save in a seperate thread then just stop without actually writing, so it waits until thats done

With this, theres no noticable lag when autosaving, users can still play chat etc while autosave occurs

8mb.video-yhl-N2B3epoD.mp4

3 autosaves happened in this video, on a 350mb world ran on the dedicated server with the async autosave

Fix Implementation

ConsoleSaveFileOriginal now has two new funcs, flushPendingBackgroundSave which gets called bny the tickcoresystems in servermain.cpp, and hasPendingBackgroundSave for checks by servermain.cpp before closing down the server fully

Flush() now gathers the snapshot which contains all the save data and etc, and then writes to disk etc in its own thread, Result sits until flushPendingBackgroundSave gets picked up by the tickcoresystems and then actually commits to StorageManager

StorageManager is Unfortunately not thread safe (oops) so the actual commit has to stay on the main thread.

flush() onlt runs in a seperate thread IF set to dedicated server btw. I made sure not to touch the save stuff when ran as client lol.

AI Use Disclosure

no

Related Issues

@sylvessa sylvessa changed the title optimization: dedicated server autosaving optimization: async dedicated server autosaving Apr 5, 2026
@sylvessa
Copy link
Copy Markdown
Collaborator Author

sylvessa commented Apr 5, 2026

merging since mattsumi has looked through it

@sylvessa sylvessa merged commit 034c313 into smartcmd:main Apr 5, 2026
1 check passed
@sylvessa sylvessa deleted the optimize/async-autosave branch April 5, 2026 17:57
itsRevela pushed a commit to itsRevela/MinecraftConsoles that referenced this pull request Apr 5, 2026
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.

2 participants