Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move SavedData.save to IO worker to avoid synchronous IO on main thread #1796

Merged
merged 8 commits into from
Feb 12, 2025

Conversation

thiakil
Copy link
Contributor

@thiakil thiakil commented Dec 24, 2024

The new Atomic Move changes to SavedData are causing synchronous IO on the main/server thread, even when saving should be asynchronous.
This is adds up in large modpacks

Ref 1 2

This PR moves that to an IO worker and adds waiting in the appropriate places.

@neoforged-automation neoforged-automation bot added the 1.21.1 Targeted at Minecraft 1.21.1 label Dec 24, 2024
@neoforged-pr-publishing
Copy link

  • Publish PR to GitHub Packages

@Technici4n
Copy link
Member

Does vanilla not perform this IO on the main thread already?

@Technici4n
Copy link
Member

We need to be a bit careful I think:

  • Moving IO to a separate thread just to block until completion will not improve global performance unless IO is performed in parallel.
  • It looks like the bottleneck is the actual disk write. Doing that in parallel or on a different thread shouldn't bring improvements?

@thiakil
Copy link
Contributor Author

thiakil commented Dec 24, 2024

Does vanilla not perform this IO on the main thread already?

Yes, but it does not force a flush to disk and an atomic move. Plus scaling.

to block until completion

Not quite what's happening. That's only when /save-all flush is used. Normally the io will complete in the background, which is why only one thread

marchermans
marchermans previously approved these changes Dec 24, 2024
Copy link
Contributor

@marchermans marchermans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @Technici4n That we need to be majorly carefull here.

We introduced the default always flush/atomic-move behaviour to make sure that capabilities and modded savedata are always written.
This change in and of itself seems to be fine, but future changes might re-open that can of worms.

Copy link
Member

@Technici4n Technici4n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanations. Moving IO off-thread is quite nice since we are already doing atomic moves that should be safe to perform concurrently.

Co-authored-by: Bruno Ploumhans <[email protected]>
Copy link
Member

@XFactHD XFactHD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally managed to take a closer look at this. The proposed solution is fine and my initial worries turned out to be completely unjustified. However, I would like to simplify the implementation by making it closer to what Mojang did in 1.21.4: Instead of spinning up a custom executor, manually adding the tasks and then shutting it down to wait on the remaining tasks in case of a flush, the individual IO tasks can be chained with CompletableFuture#thenRunAsync() using the Util.ioPool() executor (non-daemon, so it's safe to use for critical stuff like this and it's what vanilla uses in 1.21.4), storing the latest future in a field and then joining on it when necessary. If desired, the last future can also be cleared after then join by setting the field to CompletableFuture.completedFuture(null).

@thiakil thiakil requested a review from XFactHD February 9, 2025 04:11
@thiakil thiakil requested a review from XFactHD February 11, 2025 14:00
@XFactHD XFactHD requested a review from Technici4n February 12, 2025 01:03
Copy link
Member

@Technici4n Technici4n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Technici4n Technici4n merged commit 1acab57 into neoforged:1.21.1 Feb 12, 2025
6 checks passed
@neoforged-releases
Copy link

🚀 This PR has been released as NeoForge version 21.1.118.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.21.1 Targeted at Minecraft 1.21.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants