Speedup of canPassThrough() method #2394
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.
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 callscanEnterTiles()
which callsisAtWarWith()
and it takes almost 50% of its own time. Another 49% of the time is spent for HashMapcontains
checks of theuniques
.I did refactoring of those two methods:
canEnterTiles()
does not callisAtWarWith()
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
: methodfirst()
does a checkisEmpty
inside, so no need to callisNotEmpty
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:
Summing up:
canPassThrough()
is almost 20% faster now!And of course, I did 100% unit test coverage for the method before introducing any changes.