Skip to content

One more Map::Units function at 100%: UpdateFireBallista - #452

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

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-fireballista-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateFireBallista is from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and the commit credits him as co-author. It is the fire ballista next to UpdateBallista and shares most of its siege engine code.

Function Address Before After
Units::UpdateFireBallista 0x00577CC0 gynt's draft 51.30% (b66f4b3; 47.90% at c9a76ac) 100.0%, under /GL

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 the previous cut of this branch on main 3458fea (the same compiled code: the last cut names a constant, changes how a few branches are spelled and adds comments, and a build of it reads the same), with its two /GL lines and with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,424 compile steps, 99.58 over 2,440 functions, against 99.58 over 2,439 for main alone (built at f489b72; 3458fea only adds files under tools/). UpdateFireBallista reads 100.0 raw and normalised there too, with no diff entry, and deselectUnit stays at 100.0 under /GL. UpdateFireBallistaBuilding (0x0041E240) reads 100.0 raw and normalised, as it does in main's own build. Every main function reads exactly what it read before (unrounded 99.5761 against 99.5759).
  • Our own narrower run: the 133 files of main's /GL list that exist on main, UpdateFireBallista.cpp and UnitsState/deselectUnit.cpp: 145 functions, average 99.57. UpdateFireBallista reads 100.0% raw and normalised, with no diff entries (of this cut), and deselectUnit 100.0%. The same build without the two files has 143 functions. One of them, UpdateFireBallistaBuilding (on main's list), reads 99.6% / 95.5% in that narrow build and 100.0% / 100.0% with the two lines; in main's own full build it reads 100.0% already, so the 99.6% is a reading of our narrow build, not something these lines fix. Each of the other 142 reads the same.
  • 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 36 CHECK (explained below).
  • Not run in game.

What got it to 100%

Every number here is with the file on the /GL list (below). On our dev branch (not public) it went from 83.3% to 91.9% with the siege engine shapes of UpdateCatapult (the ones listed on #451) and the states as one chain. Then it took the same three changes that brought UpdateBallista to 100% (the two returning engineers set out in a loop of two passes that the compiler unrolls, the distance to an engineer with the engineer's coordinate first, the engineer's slot read once in the loop that removes the crew), which gave 92.6%. Five things were the fire ballista's own.

  • The original reads a loop index after its loop. createTribe gets units[i].owner, with i where the crew search ended: the original multiplies the search's counter (imul ebx, ebx, 1168; movsx ecx, [ebx + owner] at 0x00578339), not the slot. So the state was written C-style, with one int i for its loops. That is also what the last two lines needed: with one index for the state's four loops, the link-time code generation puts 0x6d and 5 for the crew's stores into ebx and edi, as the original does, and with an index per loop it swaps them (99.4% against 100.0%). quick_diff cannot see that difference, also not with /Zi and the precompiled header; only the real build shows it. The two comments in the code say why.
  • Each reason to stop the engineer check is an if (...) { break; } of its own (the uid differs, the engineer has not arrived, the heights differ by more than 16). The original keeps the zero for the stores behind that loop in edx from in front of the loop (xor edx, edx at 0x00577FC1, again at the loop's head (0x00577FD1) and where the loop is left early (0x0057809E)), and stores the fade byte as an immediate. With the three reasons in one || condition, the zero is set behind the loop (quick_diff 0.9704 and 943 instructions, against 0.9874 and 947).
  • The search for another player's crew returns straight from the end of its loop: for (crewOwner = 1;; ++crewOwner) { if (crewOwner >= 9) return; if (crewCount[crewOwner] == 2) break; }. With the test in the loop's header (crewOwner < 9) and if (crewOwner >= 9) return; behind it, the compiler tests crewOwner a second time (quick_diff 0.9863 and 949 instructions, against 0.9874 and 947).
  • The moving state stores animationSheetFrameOffset at the start of both arms of if (tracker == 0) { … } else { if (tracker > 70) … }. The compiler hoists the store out of the two arms to between the compare and its jump, where the original has it (cmp word [tracker], bp at 0x00578BCB, then the store and je). As one statement in front of an if / else-if chain, the store stays above the compare. The compiler hoists only from the two arms of one if, not across an else if chain.
  • No function-wide copy of the unit ID (the draft's unitID): the state of US_MOVE_TO_DESTINATION and US_IDLEUnk reads the unit through DAT_CurrentUnitSlotID::instance; with the local, the tracker is read once into a register where the original reads it from memory twice. State 9 keeps its current, which the original holds in edi across its two calls (0x00578639 to 0x00578672).

quick_diff, with its /GL callees in the unit, reads 0.9874: 947 instructions against 951. The rest are four alignment jmps that quick_diff does not lay out, the spellings of one mask and of 0xffff, and the tile-cost table's address in one operand.

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

  • UpdateFireBallista.cpp and UnitsState/deselectUnit.cpp. deselectUnit is at 100% on main and was on no /GL list. The captured ballista is deselected, and the compiler has to see deselectUnit's body: behind deselectUnit at 0x005782F2 the original pushes edx (the slot, loaded at 0x005782E6) again at 0x00578300, and calls clearOrDeselectUnitFromSelection at 0x00578302 without loading ecx again. quick_diff reads 0.9874 with it and 0.8796 without it.
  • hasUnitReachedDestination, the other callee it has to see, is on main's list already (0.5758 without it).
  • What the lines do to other functions: nothing in our narrower run (above), and nothing on main moves in the full build either (see Measured).

Kept as they are

  • int i;, int dx; and int dy; without a value (five declared without a value CHECKs) and five loops without their counter declared in the header (five CHECKs): the state's four loops share the index above, and the search for another player's crew counts with crewOwner, which is used behind it; dx and dy are assigned in both arms of the if / else that replaces the ternary. Main's style-lint flags every line with a ? and a :, so the six ternaries of the return check and the crew search are if / else now; the instruction list is the same line for line.
  • The capture path's two calls, as the original makes them: createTribe gets units[i].owner with i = maxUnitCount, where the crew search ended (0x00578339). addUnitToTribe gets the tribe first and the unit second (0x00578359 to 0x00578360), the other way round from its parameters (unitID, tribeID); 0x00522590 indexes the units with its first one. Both look like bugs of the original, and the code keeps them for the match, each with a fixme:.
  • units[slot].owner read again although owner holds it (three times), because the original reads it again too (movsx at 0x00577DA5, 0x00577F98 and 0x005781C6).
  • (int)widthOrHeight / 2: widthOrHeight is a uint, and the original divides signed (cdq; sub; sar).
  • Three (char) casts: two frame-table reads into animationFrame and one test of disappearFadeAlphaCountdown. The headers have these as byte, and the original reads them signed.
  • field_0x58 = 0, 3 times (the SG 5 CHECKs): the checker takes the name for a BOOLEnum of another struct; in Unit.hpp it is a short, and the original stores a word.
  • Placeholder names (19 CHECKs): below.

What some placeholders hold

  • Unit::field_0x3bc: a countdown before the fire ballista looks for a target again, as in UpdateBallista: set to -60 or -80 when there is no target, to 0 by the two moving states, and counted up by one per update while negative.
  • Unit::field_0x58: a short that three of its states set to 0 (waiting for a crew, aiming and loading).
  • PathFindingState::field_0x9c: read after each call of pathFindingRelated in the state that moves towards the enemy. While it is 2 or less, the next call tries a wider radius (10, 20, 60, 120).
  • Frame tables in Frames_Shared_UnitClimbingUp: .field_0x35dc is loading, .field_0x3654 firing.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Units::UpdateFireBallista, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (b66f4b3 at
51.30%, after c9a76ac at 47.90%), through versions on our dev branch from 83.3% to 92.6%. The forms that
matter, beyond the siege engine shapes it shares with UpdateBallista:
- the waiting state has one `int i` for its four loops. The original reads the crew search's index after the
  loop: createTribe gets units[i].owner with i where the search ended (`imul ebx, ebx, 1168` on the search's
  counter at 0x00578339), not the ballista's owner. With the shared index the link-time code generation also gives
  the crew's stores the original's registers (0x6d and 5 in ebx and edi); with an index per loop it swaps them;
- the two returning engineers are set out in a loop of two passes, which the compiler unrolls, as in
  UpdateBallista, and in the loop that checks them each reason to stop is an `if (...) { break; }` of its own. As
  one `||` condition the zero for the stores behind the loop is set behind it; the original keeps it in edx from
  in front of the loop (0x00577FC1);
- the search for another player's crew returns straight from the end of its loop:
  `for (crewOwner = 1;; ++crewOwner) { if (crewOwner >= 9) return; if (crewCount[crewOwner] == 2) break; }`;
- the moving state stores animationSheetFrameOffset at the start of both arms of
  `if (tracker == 0) { ... } else { if (tracker > 70) ... }`, which the compiler hoists to between the compare and
  its jump (0x00578BCB), and reads the unit through the slot global, with no function-wide copy of the unit ID
  (state 9 keeps its own, which the original holds in edi across its two calls);
- the distances to an engineer are taken with the engineer's coordinate first, in the crew search the y distance
  through two locals.
Each of these has a one-line comment in the code. The capture path keeps two calls as the original makes them,
each with a fixme: createTribe gets the owner of units[i] (i is maxUnitCount there), and addUnitToTribe gets the
tribe first and the unit second (0x00578359 to 0x00578360; 0x00522590 indexes the units with its first argument).
Main's style-lint flags every ternary on one line, so the six of the return check and the crew search are if /
else; the code is the same. Numbers and pointer arithmetic that have names in main's headers are written with
those names, as in UpdateKnight (sourcehold#444).

compiler-flags-gl.txt gets UpdateFireBallista.cpp and UnitsState/deselectUnit.cpp, which is at 100% on main and was
on no /GL list. The captured ballista is deselected, and the compiler has to see deselectUnit's body: behind the
call at 0x005782F2 the original pushes edx (the slot, loaded at 0x005782E6) again at 0x00578300 and calls
clearOrDeselectUnitFromSelection at 0x00578302 without loading ecx again. quick_diff with the /GL callees in the
unit reads 0.9874 with it and 0.8796 without it. hasUnitReachedDestination, the other callee it has to see, is on
main's list already (0.5758 without it).

reccmp in a build of main's whole /GL list (the 133 files that exist) and the two files, with every resolver of
those files on: 145 functions, average 99.57. UpdateFireBallista 100.0% raw and normalised, no differing line;
deselectUnit 100.0%. In the same narrow build of main (3458fea) without the two lines (143 functions),
UpdateFireBallistaBuilding reads 99.6% (normalised 95.5), and 100.0% with them; main's own full build reads it at
100.0% already, so that is a reading of the narrow build. Each of the other 142 functions reads the same.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <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