Page MenuHomeWildfire Games

Improve AttackEntitiesByPreference by short-circuiting on best possible preference.
ClosedPublic

Authored by wraitii on May 30 2023, 6:15 PM.

Details

Summary

Similar trick to D3446 / rP25102 - we can short-circuit if we find units that match our best possible preference.

I think there's a chance this doesn't change hashes, but haven't checked.

This is a huge optimisation on CombatDemoHuge, but it applies elsewhere as well. Should be profiled in a variety of cases for sanity.

Test Plan

Validate the new behaviour is working correctly, profile.

Diff Detail

Repository
rP 0 A.D. Public Repository
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

wraitii created this revision.May 30 2023, 6:15 PM

Successful build - Chance fights ever on the side of the prudent.

Link to build: https://jenkins.wildfiregames.com/job/vs2015-differential/8103/display/redirect

Build failure - The Moirai have given mortals hearts that can endure.

Link to build: https://jenkins.wildfiregames.com/job/macos-differential/7012/display/redirect

wraitii published this revision for review.May 30 2023, 7:29 PM
marder added a subscriber: marder.May 31 2023, 8:17 AM

This doesn't seem to give the same result when we have another entity with preference 0, but I guess that is kind of undefined anyway. ^^

This doesn't seem to give the same result when we have another entity with preference 0, but I guess that is kind of undefined anyway. ^^

As far as I know we either return the entities in either sorted-by-distance or random-order from the range manager, so this should be 'good enough still'.

Yeah, indeed.

binaries/data/mods/public/simulation/components/UnitAI.js
6442 ↗(On Diff #21847)

I don't think this line adds much in the long run? But meh. :)

wraitii added inline comments.Jun 2 2023, 8:17 AM
binaries/data/mods/public/simulation/components/UnitAI.js
6442 ↗(On Diff #21847)

If we ever change the preferences to target a 'less common' class first it might be a performance problem, which is what I kinda dislike about this optimisation though it's a huge speed boost.

Guess what, this doesn't actually change hashes on CombatDemoHuge because we always short-circuit.
It's probably the single biggest improvement we've ever had on that particular map.


Note quite 'playable' just yet, but it's getting closer.

Guess what, this doesn't actually change hashes on CombatDemoHuge because we always short-circuit.
It's probably the single biggest improvement we've ever had on that particular map.

Do you have not logarithmic but linear per-turn stats?

Guess what, this doesn't actually change hashes on CombatDemoHuge because we always short-circuit.
It's probably the single biggest improvement we've ever had on that particular map.

Do you have not logarithmic but linear per-turn stats?

Those graphs aren't logarithmic, they're using an updated Profiler2. On average, frames with the patch take literally 2x less time.

real_tabasco_sauce added inline comments.
binaries/data/mods/public/simulation/components/UnitAI.js
6442 ↗(On Diff #21847)

Would ships then be be less performant if they attacked with unitAI? I guess there won't be as many ships as soldiers typically, so in that case its likely still better.

wraitii added inline comments.Jun 6 2023, 8:45 AM
binaries/data/mods/public/simulation/components/UnitAI.js
6442 ↗(On Diff #21847)

Mh, guess the comment is unclear. The reason this is an optimisation is that regular units usually are around 'Human' units, and so there usually is one around them. So they can use this short-circuit.

Ships are similar: their first preference is 'Ship', which is what usually will be around them. So they will benefit from this optimisation in general.

Furthermore, this doesn't really slow down the code in case there are no such units around, since it's just as simpl 'if', so it's basically a free win.

binaries/data/mods/public/simulation/components/UnitAI.js
6442 ↗(On Diff #21847)

i was referencing ships being used to attack a lot of soldiers on shore, but yeah it seems like huge win.

This revision was not accepted when it landed; it landed in state Needs Review.Jun 14 2023, 9:53 AM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.