Skip to content

Two more Map::Units functions at 100%: UpdateHunterDog and UpdateFireWatch - #433

Open
kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-D-round1-100
Open

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-D-round1-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

Two more functions at 100%, with no header changes. Both are Map::Units functions from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and both commits credit him as co-author.

Function Address Before After
Units::UpdateHunterDog 0x0056B970 gynt's draft 24.90% (76269a6; 23.07% at 7f24f78, 19.74% at 09935e7) 100.0%, under /GL
Units::UpdateFireWatch 0x00543340 gynt's draft 43.29% (9743265; 39.28% at 515a714) 100.0%, under /GL

gynt's numbers are as he recorded them in his commit messages; we did not measure his commits again. His last change to both files is the style commit 974c32f.

The steps in between, on our dev branch (not public), reccmp raw / normalised with the file on the /GL list:

Step UpdateHunterDog UpdateFireWatch
gynt's draft, on the /GL list 44.1 / 38.6 65.5 / 64.0
every access through the slot global 87.0 / 78.7 84.8 / 79.2
branch orders 94.6 / 88.9 92.1 / 84.9
the states as one chain 95.9 / 91.4 94.1 / 88.3
this PR 100.0 / 100.0 100.0 / 100.0

Measured.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • Full build: an all-on build of this branch's source, with both resolver sets on: 0 failed, average 99.58 over 2441 functions. UpdateHunterDog and UpdateFireWatch read 100.0% raw and normalised, and no function on main reads lower or higher than in the main baseline build (99.58 over 2439).
  • Our own narrower run: the 133 files of main's /GL list that exist on main, parseHlp.cpp (the first entry of our local build list, not on the /GL list) and the two new files. UpdateHunterDog and UpdateFireWatch read 100.0% raw and normalised, with no diff entries. The average is 99.57 over 146 functions. The same list without the two files and their /GL lines, which is main (3458fea), gives 99.56 over 144, and each of those 144 functions reads the same in both builds.
  • Six functions read below 100% in both builds of the narrower run, with the same numbers, so this PR does not cause them: updateBuildingPlacementRotationPreview 46.2%, playUnitSelectSpeech 94.1%, InitBlendFilterArraysUnk 97.9%, lightUpBuilding 99.3%, UpdateFireBallistaBuilding 99.6% and parseHlp 99.7%. That run does not build every file, and we did not look further into them. The full build above is the one to go by.
  • Please measure with both resolver switches on (the Globals and the function resolvers), as our numbers are.
  • Checks: openshc-pr-fix finds nothing to change, and openshc-pr-check gives 0 ERROR and 63 CHECK (all explained below). Main's style lint finds nothing.
  • Not run in game.

What got them to 100%

Apart from the dropped unitID copy and UpdateFireWatch's death-state stores, the drafts' statements are all still there; what changed is how they are arranged. The numbers are reccmp raw on our dev branch, on the /GL list (the table above has the normalised ones).

Common to both:

  • They are on the /GL list (below), and every access reads the unit through DAT_CurrentUnitSlotID::instance. The drafts took a copy of the slot at the top (int unitID). The original keeps the global itself in a register and loads it again only behind a call that may change it; a local copy is never loaded again. With no copy at all the compiler arrives at the original's loads (UpdateHunterDog 44.1% to 87.0%, UpdateFireWatch 65.5% to 84.8%).
  • The states are one if / else-if / else chain whose arms fall out of it, where the drafts had a block per state that returns. Many states end in the same few instructions, and the compiler merges such tails: with blocks that return it keeps the earlier copy and makes the later state jump back, with arms that fall out of a chain it keeps the later copy, and the original has the later one (UpdateHunterDog 94.6% to 95.9%, UpdateFireWatch 92.1% to 94.1%).

UpdateHunterDog:

  • Three branch orders. Behind setDestinationForUnit towards the hunter, the long path (totalSizeOfPathPlan > 8) is the first arm. The prey's ID is an int, which gives the original's one movsx where a short gave a movzx and a movsx. In the state that runs to the shot prey, the computeLadderClimbPath that works is the if body and the one that fails is the exit behind it (87.0% to 94.6%).
  • The chain again one level down, in state 6 (US_FIRE_WEAPONUnk, the dog waits while the hunter shoots). Behind the idle test its three outcomes are an if / else-if / else chain too. States 3 and 6 both end in the exit that stores state 6. The original has it once, at 0x0056C2D7 behind state 6's code, and state 3 jumps forward to it (the two je at 0x0056C09F and 0x0056C0A9). With blocks that return in state 6 the compiler kept state 3's copy and made state 6 jump back: the same six instructions, in the other place (95.9% to 100.0%). State 3 keeps the blocks that return it had in the draft; written as a chain too it reads the same.

UpdateFireWatch:

  • Three branches the way round the original has them. In the state that walks to the patrol point, the failing setDestinationForUnit is the else arm, and the found building is handled first with the two exits for no building behind it, the one with a target first. In the state that walks to the fire, the found building is the first arm and the idle state the else (84.8% to 92.1%).
  • The three death states in the idiom the other animation states have. The last-frame arm sets gfxNumber and DAT_UnitHasBecomeIdle, the else arm sets gfxNumber from the frame, and one if (DAT_UnitHasBecomeIdle::instance != 0) behind both stores ULS_REMOVE. The draft's last-frame arm stored logicalState itself and returned. For one state the instructions are the same either way, because the compiler knows the flag is set on that path and threads the test away. But with the store written out in the arm, it merged the identical exits of US_DEATH_01 and US_DEATH_03 (the second jumped back into the first), where the original has one in each state, at 0x005439F7 and 0x00543A47: six instructions short, 485 against 491. Written once behind the flag, each state gets its own exit, and the else parts are still shared the way the original shares them (0x00543A1C jumps forward to 0x00543A6C) (94.1% to 100.0%).

quick_diff, with the /GL callees in the unit, reads 1.0000 for both: 711 instructions against 711, and 491 against 491.

Lines added to cmake/compiler-flags-gl.txt

Kept as they are (the 63 CHECKs)

  • (char) casts (31 CHECKs: 18 in UpdateHunterDog, 13 in UpdateFireWatch): the frame-table reads into animationFrame (16 and 8), the compares of disappearFadeAlphaCountdown (2 and 4) and the fire watch's one compare of engineerManningSiegeStateRef_checkType. The headers have these as byte, and the original reads them signed (see the proposal below).
  • Placeholder names (26 CHECKs: 17 and 9): Unit::field_0x3f4, Unit::field_0x40a, and the reads of the Frames_Shared_UnitClimbingUp.field_0x… tables (16 reads of 13 tables for the dog, 8 reads of 7 tables for the fire watch). What they hold is in the next section.
  • (UnitState)10 in five places in UpdateHunterDog (5 CHECKs): the enum on main has names for 9 and 11 and none for 10 (see the proposal below).
  • (short)DAT_CurrentUnitSlotID::instance in UpdateFireWatch (1 CHECK): the slot, an int global, is stored into the workplace's workers[], an array of short.

Not flagged, but match-driven or worth knowing:

  • The chains have a one-line why in the code: the chain of the states in both functions, and the chain inside state 6 of UpdateHunterDog.
  • UpdateHunterDog: the block for a hunter who sits down is written out twice, in state 3 and in state 6, with the same statements. The original has both copies (from 0x0056C0B9 and from 0x0056C247), so nothing is shared in the source.
  • UpdateFireWatch: the block for a burning building that was found is written out three times (it stores state 3, the building, its uid and DNE_DESTINATION_NEEDED). The original has the three copies (the uid loads at 0x005434B4, 0x0054361E and 0x00543700).
  • != 0 on short fields and on DAT_UnitHasBecomeIdle (an undefined4), as UpdateDeer and UpdateDrunkard on main have them. BOOLEnum results and fields, and the bool field sleeping, are tested by truthiness. buildings[i].logicalState != 0 stays a number: BuildingLogicalState has no name for 0.
  • UpdateHunterDog: the draft's ternary on closestEnemyMicroDistance is two more arms of the if / else-if chain above it (style guide 23). The compiler still emits the original's setg.
  • The state comments (// sniffing around, // throwing water, ...) are gynt's, from his drafts.

What some placeholders hold

  • State names of other units. The dog's states 4 to 8 carry the names the enum has from the ranged units: US_RELOAD_WEAPONUnk (4) is the dog trotting after the hunter, US_AIM_WEAPONUnk (5) running after him, US_FIRE_WEAPONUnk (6) waiting while he shoots, US_LOOK_AROUNDUnk (7) running to the shot prey and US_STAND_UPUnk (8) circling it. US_SIT_DOWNUnk (11) is tested on the hunter, not on the dog. For the fire watch US_RELOAD_WEAPONUnk (4) is throwing water, and US_JESTER_ROAM_TO (109) is a fade in or out over 17 updates that ends in cachedState.
  • Unit::field_0x3f4: above 1 the dog goes to US_DISAPPEAR. UpdateDrunkard on main has the same test.
  • Unit::field_0x40a: the fire watch sets it to 1 on every update. Nothing more is known from this function.
  • Unit::workplaceBuildingID_1 and workplaceBuildingUID: for the dog the hunter's hut it belongs to. The dog is removed when the hut is gone, asleep or without a worker, and otherwise counted in the hut's numberOfAnimals. The fire watch writes its own slot into its workplace's workers[workerIndex] on every update.
  • Unit::resourceToDeposit: for the dog a counter of its visits to shot prey. On every second one it whimpers first (FX_DOG_WHIMPER, state 10) before it goes back.
  • Unit::targetX_2 and targetY_2: the destination of the dog's way back (state 9, FX_DOG_PANT on arrival), and the fire watch's patrol point.
  • Unit::targetedUnitID__OR__engineerMannedSiegeEngineRef and the …_OR_buildingUID_… field next to it: for the fire watch the burning building it walks to and that building's uid.
  • Unit::engineerManningSiegeStateRef_checkType: for the fire watch the signed step of the fade in state 109 (added to disappearFadeAlphaCountdown each tick; above 0 the unit's location is committed at the end).
  • Frames_Shared_UnitClimbingUp, the dog: field_0x75e4, 0x76d4, 0x774c, 0x77bc and 0x7834 are its five frame tables for sitting at the hut, picked by substate; field_0x78ec, 0x792c and 0x7984 standing next to the hunter; field_0x79c8 states 2 and 6; field_0x79e4 whimpering; field_0x7a2c the melee attack; field_0x7a54 and field_0x7aac the two deaths.
  • Frames_Shared_UnitClimbingUp, the fire watch: field_0x82f8, 0x8320, 0x834c and 0x837c are its four look-out tables, picked by substate; field_0x82d0 throwing water; field_0x8234 both US_DEATH_01 and US_DEATH_03; field_0x8284 the third death.
  • hasTunnelerNotFinishedDigging and computeLadderClimbPath are called for the dog here (and for the drunkard and the burning man on main), so both names are narrower than what the functions do. findClosestReachableAlliedBuilding is what the fire watch calls for the building to put out; it then tests that building's fireDuration.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 20, 2026 02:13
Units::UpdateHunterDog, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the
file: 09935e7 at 19.74%, 7f24f78 at 23.07%, 76269a6 at 24.90% "int index, orig branch order", and the
style commit 974c32f), through the forms on our development branches, where its history is.

What the match needed, in the order found (reccmp on the development branch, on the /GL list):

- Every access reads the unit through DAT_CurrentUnitSlotID::instance, and the draft's local copy `int unitID`
  is gone: the original keeps the global itself in a register and loads it again only behind a call that may
  change it, which a local copy never does (44.1 -> 87.0).

- Behind setDestinationForUnit towards the hunter the long path (totalSizeOfPathPlan > 8) is the first arm; the
  prey's ID is an int (one movsx, as in the original, where a short gave a movzx and a movsx); in the state that
  runs to the shot prey the computeLadderClimbPath that works is the if body and the one that fails is the exit
  behind it (-> 94.6).

- The states are one if / else-if / else chain whose arms fall out of it, where the draft had a block per state
  that returns. With blocks that return the compiler keeps the earlier of two identical tails, with arms that
  fall out it keeps the later one, which the original has (-> 95.9).

- Inside state 6 (US_FIRE_WEAPONUnk) the three outcomes behind the idle test are an if / else-if / else chain
  as well. States 3 and 6 both end in the exit that stores state 6; the original has it once, at 0x0056C2D7
  behind state 6's code, and state 3 jumps forward to it (0x0056C09F, 0x0056C0A9). With blocks that return in
  state 6 the compiler kept state 3's copy (-> 100.0).

The text follows main's unit functions: BOOLEnum results by truthiness; the UnitState names main has
(US_STATE_2Unk, US_STATE_3Unk, US_STATE_9Unk, US_UNKNOWN_120; 10 has no name and stays a cast); const on the
five locals that are assigned once; the draft's ternary on closestEnemyMicroDistance as two more arms of its
if / else-if chain (style guide 23; the compiler still emits the setg); the steps by one as ++ and -- (rule
29); and a one-line why at each of the two chains. The instruction stream of the function is the same line for
line with and without these (a /FA compile with its /GL callees in the unit).

compiler-flags-gl.txt gets UpdateHunterDog.cpp. The callees it needs visible are on the list already:
checkIfCitizenUnitIsAliveBasedOnState, hasTunnelerNotFinishedDigging, hasUnitReachedDestination,
makeUnitStopWalkingByClearingPathProgressState and resumeMovementIfNoAttackTarget.

Measured with reccmp, both resolver sets on:
- a /GL build of this file with its callee closure: UpdateHunterDog 100.0% raw and normalised, no diff entry;
- a build of every file of main's /GL list that exists on main (3458fea), with and without this file and its
  /GL line (made on the text before the style changes above, same instruction stream): UpdateHunterDog 100.0%,
  and each of the other 144 functions reads the same in both.
Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Units::UpdateFireWatch, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the
file: 515a714 at 39.28%, 9743265 at 43.29%, and the style commit 974c32f), through the forms on our
development branches, where its history is.

What the match needed, in the order found (reccmp on the development branch, on the /GL list):

- Every access reads the unit through DAT_CurrentUnitSlotID::instance, and the draft's local copy `int unitID`
  is gone: the original keeps the global itself in a register and loads it again only behind a call that may
  change it, which a local copy never does (65.5 -> 84.8).

- Three branches the way round the original has them. In the state that walks to the patrol point the failing
  setDestinationForUnit is the else arm, and the found building is handled first with the two exits for no
  building behind it, the one with a target first. In the state that walks to the fire, where the search for
  another burning building runs again, the found building is the first arm and the idle state the else
  (-> 92.1).

- The states are one if / else-if / else chain whose arms fall out of it, where the draft had a block per state
  that returns. With blocks that return the compiler keeps the earlier of two identical tails, with arms that
  fall out it keeps the later one, which the original has (-> 94.1).

- The three death states (US_DEATH_01, US_DEATH_03 and the alive-check arm) in the idiom the other animation
  states have: the last-frame arm sets gfxNumber and DAT_UnitHasBecomeIdle, the else arm sets gfxNumber from
  the frame, and one `if (DAT_UnitHasBecomeIdle::instance != 0)` behind both stores ULS_REMOVE. The draft's
  last-frame arm stored logicalState itself and returned. For one state the instructions are the same either
  way, because the compiler knows the flag is set on that path. But with the store written out in the arm it
  merged the identical exits of US_DEATH_01 and US_DEATH_03 (the second jumped back into the first), where
  the original has one in each state (0x005439F7 and 0x00543A47): six instructions short (-> 100.0).

The text follows main's unit functions: BOOLEnum results by truthiness, the UnitState names main has
(US_STATE_2Unk, US_STATE_3Unk, US_UNKNOWN_108), const on the ten locals that are assigned once, the steps by one
as ++ and --, and a one-line why at the chain.

compiler-flags-gl.txt gets UpdateFireWatch.cpp. The callees it needs visible are on the list already:
checkIfCitizenUnitIsAliveBasedOnState, commitUnitLocation, hasUnitReachedDestination and
makeUnitStopWalkingByClearingPathProgressState.

Measured with reccmp, both resolver sets on:
- a /GL build of this file with its callee closure: UpdateFireWatch 100.0% raw and normalised, no diff entry;
- a build of every file of main's /GL list that exists on main (3458fea) plus this branch's two files:
  UpdateFireWatch and UpdateHunterDog 100.0% with no diff entry, and each of the other 144 functions reads
  the same as in the same build of main.
Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant