neoforged / NeoForge

Neo Modding API for Minecraft, based on Forge
https://projects.neoforged.net/neoforged/neoforge
Other
1.1k stars 161 forks source link

Forge rebuilds blockstate cache on every registry bake #31

Closed embeddedt closed 9 months ago

embeddedt commented 1 year ago

Forge (and by extension NeoForge) currently rebuilds all blockstates' shape caches inside the block registry's onBake callback:

https://github.com/neoforged/NeoForge/blob/ed6d3e5612766c9e887f9bcf6b312b233683b48e/src/main/java/net/minecraftforge/registries/GameData.java#L492-L496

The issue is that baking can occur multiple times during the launch process, which leads to a lot of unnecessary work during startup as this cache is rebuilt multiple times over.

An attempt was previously made to solve this in https://github.com/MinecraftForge/MinecraftForge/pull/7496, but was shot down due to compatibility concerns. It is not really clear to me what mods that change would break though, as most if not all methods in the BlockState classes are designed to work without the cache being initialized, and the cache would be built by the time the server/client begin ticking anyway.

As a side note, the Mojang approach of building ALL shape caches at once is very inefficient and does not scale well to mods (since not all blocks are seen in the world right away). In ModernFix the current solution I've used to solve the issue is to simply implement dynamic cache building where the cache for a blockstate is only computed the first time it's accessed. The call to initCache is then replaced by setting an invalidation flag that triggers the cache to be rebuilt on next access. This has worked reliably with no reported issues, and completely eliminates the launch delay of building the cache up front.

Technici4n commented 9 months ago

Will be fixed in #257.