Skip to content

Speedup of canPassThrough() method #2394

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

Merged
merged 4 commits into from
Apr 13, 2020

Conversation

JackRainy
Copy link
Collaborator

I have been playing with JProfiler (amasing tool, btw! Unfortunately, I cannot afford it for myself) and found out that canPassThrough() method - the most frequent called method can be optimized and speed up.

Preliminary investigation:
canPassThrough() method calls canEnterTiles() which calls isAtWarWith() and it takes almost 50% of its own time. Another 49% of the time is spent for HashMap contains checks of the uniques.

I did refactoring of those two methods: canEnterTiles() does not call isAtWarWith() anymore, but uses its checks directly. Also, isAtWarWith() does not get diplomacy manager twice. That's has given quite good throughput already.

Furthermore, I removed one more redundant check isEmpty: method first() does a check isEmpty inside, so no need to call isNotEmpty before it.

And lastly, I used the same trick as we use in other places - caching of the MapUnit uniques as boolean flags - it also gave some portion of speed boost.

Results:

Test set Before After Boost
Test set 1 15-16 sec 11-13 sec 19-27%
Test set 2 12.5 sec 10-12 sec 4-20%

Summing up: canPassThrough() is almost 20% faster now!

And of course, I did 100% unit test coverage for the method before introducing any changes.

@yairm210
Copy link
Owner

Amazing!!!
I cannot tell you how much time I have spent on improving the performance of the basic movement functions, it's great to see that there's always room for more improvement ❤️

@yairm210 yairm210 merged commit bd5c813 into yairm210:master Apr 13, 2020
@uncivbot uncivbot bot mentioned this pull request Apr 13, 2020
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