Skip to content

One more Map::Units function at 100%: UpdateCageDog - #438

Open
kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-D-round2-100
Open

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-D-round2-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateCageDog is from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and the commit credits him as co-author.

Function Address Before After
Units::UpdateCageDog 0x0056F9B0 gynt's draft 54.68% (a3e1ab0; 54.52% at 10e5688, 50.04% at 91be34d) 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 the file 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 UpdateCageDog
the draft as it stood on our dev branch, on the /GL list 73.7 / 71.8
every access through the slot global 88.4 / 83.5
four conditions the other way round 95.3 / 89.4
the enemy's ID as a short and an int 96.0 / 90.4
this PR (state 206 per arm, tryAttackUnitID visible) 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 811ccf2fca with both resolver sets on: 0 failed, average 99.57 over 2440 functions against 99.58 over 2439 for main. UpdateCageDog reads 100.0% raw and normalised with no diff entry, and tryAttackUnitID stays at 100.0% under /GL. No function on main reads differently except Menu::handleMenuItems (97.9%) and Menu::Constructor_Menu (98.4%), and those two only through a reccmp printing artefact: five compares with the constant 0x11000000, which reccmp prints as a symbol name on our side because one of our DLL's symbols lies at exactly that address in this layout; the instructions and bytes are the same, and the same two show it in UI: three more functions at 100%: the building menu's DoInitial, the credits' prepare and activateModalDialog2 #435, UI: the population report's graph at 100% #436 and UI: the credits scroll's fade and the win/defeat window at 100% #437. 811ccf2fca compiles to the same instructions as this PR's commit: the review changed only how the fade's -2 is written and passed one call result straight on (quick_diff 1.0000, 589 = 589, for both).
  • 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 files this PR puts on the list. UpdateCageDog reads 100.0% raw and normalised, with no diff entries. The average is 99.57 over 146 functions. The same build without UpdateCageDog.cpp and with main's /GL list (tryAttackUnitID built, but not under /GL), which is main (3458fea), gives 99.57 over 145, and each of those 145 functions reads the same in both builds. Both builds ran one style step before the committed text (0xfe for -2, and state 0's enemy in a local); the instruction stream is the same line for line, and the committed text reads 100.0% with no diff entries in a build of the file with its callee closure.
  • Five 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% 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 37 CHECK (all explained below).
  • Not run in game.

What got it to 100%

Apart from the dropped unitID copy, the draft's statements are all still there; what changed is how they are arranged, one compare, and what the compiler can see. The numbers are reccmp raw on our dev branch, on the /GL list (the table above has the normalised ones).

  • It is on the /GL list, and every access reads the unit through DAT_CurrentUnitSlotID::instance. The draft 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 (73.7% to 88.4%).
  • One compare changes what the function does: the dog is let out when the enemy troop value near the kennel is >= 25 (the cmp at 0x0056FB6C, the jge at 0x0056FB73). The draft had > 25. This was in the 73.7% already.
  • Four conditions the other way round, in three places of the walking state (US_MOVE_TO_DESTINATION and US_UNKNOWN_120). The frame table of its animation is chosen with field_0x3c2 != 0 first. The animation handles the last frame (animationFrame <= 0) first and has the two gfxNumber stores behind it, again with != 0 first. The predator test has the predator arm first (88.4% to 95.3%).
  • In state 207 the enemy's ID is a short for the test against 0 and an int copy, taken ahead of the test, for the index. The original tests the 16-bit value and sign-extends it ahead of the branch (movzx ecx, cmp cx, bx, movsx eax, cx, je, from 0x0056FD11). With one short the extension came behind the branch (95.3% to 96.0%). UpdateLion's chase state (Map::Units::UpdateLion at 100% #425) has the same shape.
  • In state 206 the animation block is written out in each arm of the seated test. Each arm stores its frame and then holds the whole if / else that adds to gfxNumber; the draft had one if / else behind the two arms.
    • The original loads the facing in each arm of that if / else: into eax in the last-frame arm (0x00570063) and into edx in the other (0x0057007B). eax is the register the frame was in, free again in the last-frame arm.
    • With one if / else behind the join, the frame is a value with two definitions. The compiler then keeps eax for it through the last-frame arm, takes edx for the facing in both arms, and moves the two identical loads above the branch.
    • Written out per arm, each frame has one definition and the registers are the original's. The compiler merges the two identical tails itself, so the code has the block once, as the original (the two frame loads at 0x00570049 and 0x00570052, the join at 0x00570059).
    • quick_diff 0.9890 to 0.9949, 589 instructions against 589. Tried before and without effect: the frame as a local or through a conditional expression (worse), the flag first, = for +=, other operand orders, the idle test inside the else arm.
  • UnitsState::tryAttackUnitID has to be visible (next section). quick_diff 0.9949 to 1.0000; with both, 96.0% to 100.0%.

The states stay blocks that return, as in the draft: this function matches so. quick_diff, with its /GL callees in the unit, reads 1.0000: 589 instructions against 589.

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

  • UpdateCageDog.cpp.

  • UnitsState/tryAttackUnitID.cpp. The original loads the slot into esi in front of the call (0x0056FACC), calls it (0x0056FAD9) and still uses esi behind it (0x0056FAE6). So its compiler knew that the call leaves DAT_CurrentUnitSlotID alone, and ours knows that only when it sees the body. tryAttackUnitID is a leaf function, at 100.0% on main and not on main's /GL list; it reads 100.0% on the list as off it.

  • Leaving one callee out at a time (quick_diff, 1.0000 with all of them), UpdateCageDog needs four:

    Callee quick_diff without it Its line
    checkIfCitizenUnitIsAliveBasedOnState 0.7162 on main's list
    hasUnitReachedDestination 0.4142 on main's list
    makeUnitStopWalkingByClearingPathProgressState 0.8651 on main's list
    tryAttackUnitID 0.9949 added here
  • Three more of its callees are on main's list and make no difference to it either way (1.0000 without each): buildingIsAccessible, commitUnitLocation and resumeMovementIfNoAttackTarget.

  • One callee must stay off the list for this function: with playSFXAtLocation visible it reads 0.9527. It is on no /GL list today. Five more finished callees that are on no list make no difference (1.0000 with each): resetUnitMovementState, setupUnitSharingCurrentTilePosition, commitPendingUnitPosition, resumeMovementAfterInterruption and changeDestinationByLeftover.

  • What the two lines do to other functions: nothing that our narrower run can see: with and without them, each of the 145 other functions reads the same, tryAttackUnitID among them (100.0% on the list as off it).

Kept as they are (the 37 CHECKs)

  • (char) casts (17 CHECKs): the eleven frame-table reads into animationFrame, the four compares of disappearFadeAlphaCountdown and the two of engineerManningSiegeStateRef_checkType. The headers have these as byte, and the original reads them signed (see the proposal below).
  • Placeholder names (14 CHECKs): Unit::field_0x3c2 three times, MapAndTimeState::field3172_0x27ac, and ten Frames_Shared_UnitClimbingUp.field_0x… tables. What they hold is in the next section.
  • (UnitState)206, 207, 208, 210 and 211 (6 CHECKs, 211 twice): the enum on main has names up to 205 and for 209, and none for these (see the proposal below).

Not flagged, but match-driven or worth knowing:

  • State 206's animation block is written out twice, with a why in the code. The two copies are the same statements; the reason is above.
  • No compare with TRUE or FALSE. BOOLEnum results and the BOOLEnum field animationCycleNumberHasJustIncremented are tested by truthiness. != 0, == 0 and <= 0 stay on short fields, on DAT_UnitHasBecomeIdle, on predatorNearby and on buildingIsAccessible, which returns int.
  • The fade's step is written -2 where the draft had 0xfe, as main's UpdateMerchant writes the same field. The field is a byte in the header and the original reads it signed. The dog fades in when it is let out (disappearFadeAlphaCountdown starts at 31 and goes down by 2) and fades out when it is back at the kennel (from 0, up by 2).
  • State 0's enemy goes straight into tryAttackUnitID; the draft had a local that was used once. The code is the same either way.
  • The state comments (// waiting in the kennel until enough enemies are close, // chasing an enemy, ...) are gynt's, from his draft.

What some placeholders hold

  • The states. 210 is waiting in the kennel until enough enemies are close, 211 returning to the kennel, 207 chasing an enemy, 206 an animation that ends in state 0 with seated set, and 208 stops the dog and goes to state 0 once movementRelated is 8 or more. US_MOVE_TO_DESTINATION and US_UNKNOWN_120 are the walking state. US_MELEE_ATTACK is the bite (FX_WOLF_ATTACK), US_UNKNOWN_117 a wait of 51 updates between the death animation and US_DISAPPEAR, and US_JESTER_ROAM_TO (109) the fade in or out that ends in cachedState, as for the fire watch.
  • The release test of state 210: updateTickTracker runs from cagedogReleaseCheckMoment * 8 to 32 above it, so about every 33 updates the dog counts the enemy units around its tile (computeTotalUnitsWithinDistance with a distance of 16) and is let out when ALGO_TotalTroopValue >= 25 or ALGO_TotalTroopCount > 5. cagedogReleaseCheckMoment only shifts where the counter starts.
  • MapAndTimeState::field3172_0x27ac is incremented once on every update of a cage dog.
  • Unit::field_0x3c2: in the walking state it picks one of two frame tables and one of two gfxNumber bases (624 or 688); state 206 sets it to 1.
  • Unit::unknownBool01 is counted down to 0, one step per update.
  • Unit::workplaceBuildingID_1 and workplaceBuildingUID: the kennel. When its uid no longer matches, the dog goes to US_DISAPPEAR.
  • Unit::engineerManningSiegeStateRef_checkType: the signed step of the fade in state 109, as for the fire watch and the miller: -2 fades the dog in, 2 fades it out.
  • Frames_Shared_UnitClimbingUp: field_0x6d98 and field_0x6db8 are the two tables of the walking state's animation (by field_0x3c2), field_0x6d7c and field_0x7134 those of state 206 (by seated), field_0x7060, 0x7078, 0x7098, 0x70c0 and 0x70e0 the five of the bite (by substate), and field_0x6c64 the second death; ANIM_Worker_Shared1 is the first.
  • FX_WOLF_ATTACK and FX_WOLF_DIE are the cage dog's sounds too, so the names are narrower than their use. tryAttackUnitID, resumeMovementIfNoAttackTarget and findClosestEnemyByAreaAndRange are what the dog attacks with.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Units::UpdateCageDog, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on the
file: 91be34d at 50.04%, 10e5688 at 54.52%, a3e1ab0 at 54.68%, the build fix 6565c1b, which names
field133_0x278 predatorNearby, and the style commit 974c32f), through the forms on our development branches,
where its history is.

What the match needed, against the draft (reccmp raw on the development branch, on the /GL list; the draft as
it stood there read 73.7):

- 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 (73.7 -> 88.4).

- The dog is let out when the enemy troop value near the kennel is >= 25 (the cmp at 0x0056FB6C, the jge at
  0x0056FB73); the draft had > 25. This one changes what the function does; it was in the 73.7 already.

- Four conditions the other way round, in three places of the walking state (US_MOVE_TO_DESTINATION and 120):
  the frame table of its animation is chosen with field_0x3c2 != 0 first; the animation handles the last frame
  (animationFrame <= 0) first and has the two gfxNumber stores behind it, again with != 0 first; and the
  predator test has the predator arm first (88.4 -> 95.3).

- In state 207 the enemy's ID is a `short` for the test against 0 and an `int` copy, taken ahead of the test,
  for the index. The original tests the 16-bit value and sign-extends it ahead of the branch (from 0x0056FD11);
  with one short the extension came behind the branch (95.3 -> 96.0).

- In state 206 the animation block is written out in each arm of the seated test: each arm stores its frame
  and then holds the whole if / else that adds to gfxNumber. The original loads the facing in each arm of that
  if / else, into eax in the last-frame arm (0x00570063) and into edx in the other (0x0057007B); eax is the
  register the frame was in, free again in the last-frame arm. With one if / else behind the join of the two
  frame arms, the frame is a value with two definitions: the compiler keeps eax for it through the last-frame
  arm, takes edx for the facing in both arms, and then moves the two identical loads above the branch. Written
  out per arm, each frame has one definition, the registers are the original's, and the compiler merges the two
  identical tails itself, so the code has the block once, as the original (the two frame loads at 0x00570049
  and 0x00570052, the join at 0x00570059). quick_diff 0.9890 -> 0.9949, 589 instructions against 589.

- UnitsState::tryAttackUnitID has to be visible. The original loads the slot into esi in front of the call
  (0x0056FACC), calls it (0x0056FAD9) and still uses esi behind it (0x0056FAE6), so its compiler knew that the
  call leaves DAT_CurrentUnitSlotID alone. It is a leaf function, at 100.0% on main and on no /GL list so far;
  with its body visible ours keeps the slot too (quick_diff 0.9949 -> 1.0000; 96.0 -> 100.0 with both).

The states stay blocks that return, as in the draft: this function matches so.

The text follows main's unit functions: no compare with TRUE or FALSE (BOOLEnum results by truthiness), main's
names US_UNKNOWN_117 and US_UNKNOWN_120, const on the locals that are assigned once, the steps by one as ++ and
--, the fade's step as -2 where the draft had 0xfe (main's UpdateMerchant writes the same field so; the dog
fades in when it is let out), state 0's enemy passed straight into tryAttackUnitID where the draft had a local
that was used once, and a why at the block written out twice.

compiler-flags-gl.txt gets UpdateCageDog.cpp and UnitsState/tryAttackUnitID.cpp. Leaving one callee out at a
time (quick_diff with the /GL callees in the unit, 1.0000 with all of them), it needs four:
checkIfCitizenUnitIsAliveBasedOnState (0.7162 without it), hasUnitReachedDestination (0.4142) and
makeUnitStopWalkingByClearingPathProgressState (0.8651), which are on main's list, and tryAttackUnitID (0.9949).
Three more on main's list make no difference to it (1.0000 without each): buildingIsAccessible,
commitUnitLocation and resumeMovementIfNoAttackTarget. One callee that is on no list must stay off it for this
function: with playSFXAtLocation visible it reads 0.9527.

Measured with reccmp, both resolver sets on:
- a /GL build of this file with its callee closure (15 files): UpdateCageDog 100.0% raw and normalised, no diff
  entry; tryAttackUnitID reads 100.0% on the list as off it;
- a build of parseHlp.cpp and every file of the /GL list that exists, with this commit 136 files: 146
  functions, average 99.57, UpdateCageDog 100.0% with no diff entry; the same build without this file and with
  main's /GL list (tryAttackUnitID built, but not under /GL), which is main (3458fea): 145 functions, average
  99.57, and each of them reads the same in both, tryAttackUnitID among them. Five functions read below 100% in
  both, with the same numbers: updateBuildingPlacementRotationPreview 46.2, playUnitSelectSpeech 94.1,
  InitBlendFilterArraysUnk 97.9, lightUpBuilding 99.3 and parseHlp 99.7.
  Those two builds ran on the text one style step before this one (0xfe for -2, and the enemy of state 0 in a
  local). The instruction stream is the same line for line: on this text the closure build above reads 100.0%
  with no diff entry, and quick_diff reads 1.0000 with 589 instructions against 589, as on the text before.
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