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

perf: improve performance of getSmallestEnclosure #41

Closed
wants to merge 1 commit into from

Conversation

wafarm
Copy link
Contributor

@wafarm wafarm commented Jul 9, 2024

Motivation

image
Though not on every tick, still a significant impact on MSPT.

Benchmark

Look up 1,000,000 random positions with enclosures data in our 1.21 server.

Code:

dispatcher.register(
    CommandManager.literal("bench")
        .executes {
            val posList: MutableList<BlockPos> = ArrayList(1000000)
            val random = Random()
            for (i in 1..1000000) {
                val x = random.nextInt(-1000, 1001)
                val z = random.nextInt(-1000, 1001)
                val y = random.nextInt(-10, 101)
                posList.add(BlockPos(x, y, z))
            }

            val time = measureTimeMillis {
                for (pos in posList) {
                    getSmallestEnclosure(it.source.world, pos)
                }
            }

            it.source.sendMessage(Text.literal("Time elapsed: $time ms"))
            1
        }
)

Result (10 tests each):

before          (ms): 666 650 650 628 628 627 656 642 649 659
after(w/ locks) (ms): 388 398 388 321 364 408 336 357 364 333
after(w/o locks)(ms): 151 125 138 136 137 145 131 132 132 135

Changes

HashMap.values iterations is slow so used a list to cache the areas

Also removed the lockChecker for coordinates as it is not used. Possible reason: https://stackoverflow.com/questions/39152264/kotlin-how-can-i-avoid-autoboxing-garbage-in-delegated-properties but I can't fix the problem with the code in the link

@zly2006
Copy link
Owner

zly2006 commented Jul 9, 2024

HashMap.values iterations is slow so used a list to cache the areas

How about to use LinkedHashMap?

@wafarm
Copy link
Contributor Author

wafarm commented Jul 9, 2024

How about to use LinkedHashMap?

About 40ms slower than current implementation. If you want I will change to that.

@zly2006
Copy link
Owner

zly2006 commented Jul 9, 2024

Also I didn't consider too much when designing this data structure. The best way might be save all interacting areas in a chunk

@zly2006
Copy link
Owner

zly2006 commented Jul 9, 2024

OK I am going to implement a lookup based on chunks. Thanks so much for figuring out this performance issue.

@zly2006
Copy link
Owner

zly2006 commented Jul 10, 2024

May I close this PR since #42 has been merged?

@wafarm wafarm closed this Jul 10, 2024
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.

2 participants