Optimize nearest structure border iteration #9638
Merged
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.
Getting the nearest generated structure calls
ChunkGenerator.getNearestGeneratedStructure
, which contains a nested set of loops that iterate over all chunks at a specific chessboard distance. It does this by iterating over the entire square of chunks within that distance, and checking if the coordinates are at exactly the right distance to be on the border.This patch optimizes the iteration by only iterating over the border chunks. This evaluated chunks are the same, and in the same order, as before, to ensure that the returned found structure (which may for example be a buried treasure that will be marked on a treasure map) is the same as in vanilla.
Since the radius may be quite large (50 for buried treasure maps in chests, 100 for cartographer treasure maps, or any arbitrary value for plugin calls or data pack functions) this effect is noticeable.$\sum_\limits{i=1}^{100} (2i+1)^2-8i = 1,333,300$ unnecessary loop iterations, of which the bookkeeping can easily take half a millisecond, which is 1/100th of the main thread tick time already, spent just on pointless variable incrementing.
For example, for a cartographer treasure map, where the call to
ChunkGenerator.getNearestGeneratedStructure
is made fromChunkGenerator.findNearestMapStructure
with an increasing radius, this effect can cause