Reuse the biome and the list of potential mob spawns #1474
+113
−1
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.
A spark profile showing allocations over 30s shows that NaturalSpawner#spawnForChunk allocates a lot of data:
Most of this is spent in NaturalSpawner#mobsAt, which is called twice in the process of computing which mob(s) to spawn in a given chunk. So, to alleviate this, I've implemented a simple change which simply pre-computes that list, and reuses it across the two methods it would normally be used, NaturalSpawner#canSpawnMobAt and NaturalSpawner#getRandomSpawnMobAt. In the process of doing this, I also moved the getBiome call here too, as it's the third most expensive call in NaturalSpawner#mobsAt, at 75MB allocated.
While I've attempted to keep binary compatibility, one potentially breaking change in this is that we no longer call EventHooks#getPotentialSpawns twice. This hook fires the LevelEvent.PotentialSpawns event, which also has a fairly expensive overhead (~190MB, though this may be related to other mods on the server) but my assumption is that most mods were likely just returning the same data twice in this event, potentially doing expensive calculations more than necessary.
This is certainly not all of the allocations in NaturalSpawner#spawnForChunk, but by percentage, it should approximately halve its allocation rate: