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

Optimize nearest structure border iteration #9638

Merged

Conversation

MartijnMuijsers
Copy link
Contributor

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.
For example, for a cartographer treasure map, where the call to ChunkGenerator.getNearestGeneratedStructure is made from ChunkGenerator.findNearestMapStructure with an increasing radius, this effect can cause $\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.

@MartijnMuijsers MartijnMuijsers requested a review from a team as a code owner August 22, 2023 17:41
@MartijnMuijsers MartijnMuijsers force-pushed the perf/nearest-structure-chunk-iteration branch from b4edeb5 to a03d8b7 Compare August 22, 2023 17:58
@kennytv kennytv force-pushed the perf/nearest-structure-chunk-iteration branch from a03d8b7 to 765b91b Compare August 23, 2023 01:52
Copy link
Member

@kennytv kennytv left a comment

Choose a reason for hiding this comment

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

Thanks!

@kennytv kennytv merged commit fcc5f23 into PaperMC:master Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants