Restrict total number of chunk update threads based off heap size #1918
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposed Changes
With the right combination of optimization mods and configs it is possible to run the game on very small heap sizes (<=256MB). However, due to Sodium allowing users to configure the number of chunk builder threads as high as they would like, it is possible to starve the game of memory by having too many threads attempting to update chunks at once.
This PR solves that issue by introducing additional logic that caps the number of threads based off maximum heap size. Vanilla's chunk builder uses its own heuristic dependent on the
RenderLayer
s' expected buffer sizes. Here, we use simpler logic, and allow one thread for every 64MB of heap. On the default vanilla RAM allocation (2GB) this would permit 32 threads, which should still be plenty (Sodium doesn't even try to use more than 10 by default).Example screenshot attached showing that the maximum number of threads is now 2 even on a 4-thread system due to the low heap size.