Minecolonies - Cleaning up performance Issues #2
Hey everyone, after cleaning up our core classes we found some very important performance issues which are related how minecolonies works.
Therefore, I'll first explain how we did it before, then why this is bad and then how we're doing it now.
What we want to do:
We want to determinate if a certain piece of land in the Minecraft world is part of a certain colony, besides that we want to be able to get the closest colony to this location and we want to be able to check if, depending on the close colonies, it is possible to place a colony there.
How we did it before:
We stored a list of all colonies in the ColonyManager (A static class).
And then we brute forced through the whole list of colonies and checked if the distance is bigger than the size of the colony.
We did that on the following occasions:
- When players want to know more about close colonies.
- When players want to place a building
- When guards change paths
- Occasionally on any player interaction
- Every few seconds to determine which players are inside the colony range to notify them about the newest colony changes.
What the problem is:
As you might imagine, checking every few seconds or even less, through 200-1000 of colonies and calculating the square distance of it, is computationally quite heavy and for sure generates certain lag on our official server.
As the word said "brute-force", that's almost never a good approach.
What we do now:
Minecraft itself is organized in chunks of 16 /* 16 blocks, those chunks also have certain capabilities since a few updates (in 1.12) which allow us to add certain data to the chunk itself.
We, therefore, created something which is called a "Capability" which we can easily append to each chunk and which contains the owning colony and all close colonies.
With the help of that, we're able to easily ask each chunk if it is owned by a colony which makes our whole permission system way easier and we don't have to calculate expensive distance operations.
Generating this is also not as difficult.
First on creation, deletion or load (of the old colony) we calculate the range of the colony. Inside this range, we take all chunks and put each of them in a file.
We don't put this on a list to avoid having to store them in memory and take care of persistency to save the progress.
In case we want to add something to a similar location already we modify the existing file and then override it.
In order to store those things to NBT, we created a storage class for this.
Which stores the colonId which owns it, the colonies which should be removed and the colonies which should be stored in that location as well as a long describing the x and z and the dimension.
This will then be written and read from the file as soon as we require the data.
After this each time a chunk loads we check if there exists a storage file for it and load it and write it to its capability.
Additionally, we do this when the player enters a chunk, in case the chunk has been loaded before the file has been created.
To improve the performance we created the missingChunksToLoad.
This variable takes track of how many chunks have to be loaded still.
When all chunks loaded we don't even need to spend IO to check if the files exist.
Last but not least we detect the player on entering or leaving a colony quite easily now!!
Using this we just compare the chunk he left with the chunk he is in and if the claims are different we detect that he entered or left the colony!
I hope you guys enjoyed this new addition to our mod, see you the next time!
PS: Link not showing up to the pull request: https://github.com/Minecolonies/minecolonies/pull/1991
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @raycoms I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
nice work
Good analysis with traceable proves This is a good contribution and a fastest meaning of getting a result through coded languages.
Yes!
I enjoyed it, Thanks for your excellent article.
Amazing Post I really agree with you (Website developer , Crypto Expert)
Sangat bagus dan bermanfaat
good approach, is a very practical and enjoyable way to find practical solutions to such complex issues. congratulations. I invite you to read my last post and count on your vote.