Skip to content

One more Map::Units function at 100%: UpdateBallista - #451

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

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

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

One more function at 100%, with no header changes. Units::UpdateBallista 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::UpdateBallista 0x0056ECD0 gynt's draft 32.08% (e44a5a0) 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 code: the last cut writes field_0x58 = 0 for FALSE and adds one comment, and the instruction list is the same line for line), 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/). UpdateBallista 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 (made before the last local copy of the unit ID went; without it the instruction list is the same line for line, 821 instructions in quick_diff): the 133 files of main's /GL list that exist on main, UpdateBallista.cpp and UnitsState/deselectUnit.cpp: 145 functions, average 99.57. UpdateBallista reads 100.0% raw and normalised, with no diff entries, 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 33 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 85.9% to 94.5% with the siege engine shapes of UpdateCatapult, read against the original one block at a time:

  • the crew search's tick test is ((fixedRng ^ ticks ^ 8) & 15) != 0, the original's xor -8; test al, 15;
  • the target building's half size is a signed division ((int)widthOrHeight / 2: cdq; sub; sar);
  • the projectile's height offset is a local that starts at 26 and becomes 0 for a negative shootTargetedUnit;
  • the failed target search of the resting state is written behind its success path, where the original lays it out;
  • the loop that turns two engineers into the crew stores state, check type, cached state and targeting type in the original's order.

Three things were left, all around the engineers that man the ballista.

  • The two returning engineers are set out in a loop. When both of the AI's engineers are back, the original reads the slot again and computes the unit's offset anew (mov ecx, [slot]; imul ecx, ecx, 1168 at 0x0056F045), then sets out the first engineer and the second in two runs of the same stores, keeping 0x6d and 5 in ebp and ebx for the second run. That is a for loop of two passes over manningEngineerRef[i], which the compiler unrolls and which reads the array through the slot global in each pass. Written as two blocks with first and second, as the draft had it, the compiler uses the offset it already holds, and the function is 16 bytes short. The six stores inside the loop are in the order of the second run; in the five other orders we tried, two or three stores are out of place.
  • In the search for a new crew the distance is taken with the engineer's coordinate first: if (units[i].x > own.x) { dx = units[i].x - own.x; } else { … }, as in the first loop. The original compares cmp di, ax; jle (0x0056F1A2). With our coordinate first, the compiler writes cmp ax, di; jge.
  • In the two loops that remove the crew (the ballista destroyed, the ballista dismantled) the engineer's slot is read once into an int const engineer, and the uid is compared through it. With the array read in each access, the compiler keeps the two pointers into the arrays in each other's registers.

quick_diff, with its /GL callees in the unit, reads 0.9836: 821 instructions against 824. The rest are spellings of one mask (and eax, 0x80000007, twelve times) and three alignment jmps that quick_diff does not lay out.

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

  • UpdateBallista.cpp and UnitsState/deselectUnit.cpp. deselectUnit is at 100% on main and was on no /GL list.
  • The crew loop calls deselectUnit, and the compiler has to see its body: the original keeps edx across the call at 0x0056F291 (used at 0x0056F296) and does not reload ecx before 0x0056F2A1. quick_diff reads 0.9836 with the body and 0.9063 without it.
  • hasUnitReachedDestination, the other callee it has to see, is on main's list already (cmake: function-visibility.txt, which callers must see which callees' bodies #398's manifest has both edges): 0.5999 without it, and 0.5601 with no callee bodies at all.
  • 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 dx; and int dy; without a value, twice (four declared without a value CHECKs): each is 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 two crew loops are if / else now; the instruction list is the same line for line.
  • (int)widthOrHeight / 2: widthOrHeight is a uint, and the original divides signed (cdq; sub; sar).
  • Five (char) casts: four reads of frame tables (two into animationFrame, two in the sums for imageIDUnk) and one test of disappearFadeAlphaCountdown. The headers have these as byte, and the original reads them signed.
  • field_0x58 = 0, 4 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 ballista looks for a target again. When there is no target or the target is out of range it is set to -40, -80 or -200, and while it is negative each update counts it up by one and does nothing else.
  • Unit::field_0x58: a short that four of the ballista's states set to 0 (waiting for a crew, resting, aiming and loading).
  • Frame tables in Frames_Shared_UnitClimbingUp: .field_0x1b14 and .field_0x1b50 are loading, for the ballista (gfxNumber) and its second picture (imageIDUnk). .field_0x1b8c and .field_0x1bc8 are firing. The bolt leaves at frame 12 of .field_0x1b8c.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Units::UpdateBallista, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (e44a5a0 at
32.08%), through versions on our dev branch from 85.9% to 94.5%. The forms that matter:
- the siege engine shapes: the crew search's tick test is ((fixedRng ^ ticks ^ 8) & 15) != 0, the target
  building's half size a signed division of an int ((int)widthOrHeight / 2: cdq; sub; sar), the projectile's
  height offset a local that starts at 26 and becomes 0 for a negative shootTargetedUnit, and the failed target
  search of the resting state written behind its success path;
- when both of the AI's engineers are back, the two are set out in a loop of two passes over manningEngineerRef,
  which the compiler unrolls. The original reads the slot again in front of it and computes the unit's offset
  anew (`mov ecx, [slot]; imul ecx, ecx, 1168` at 0x0056F045), and keeps 0x6d and 5 in ebp and ebx for the second
  pass; written as two blocks the function is 16 bytes short. The six stores are in the order of the second pass;
- in the search for a new crew the distance to an engineer is taken with the engineer's coordinate first
  (`cmp di, ax; jle` at 0x0056F1A2), and in the two loops that remove the crew the engineer's slot is read once
  into a local.
Main's style-lint flags every ternary on one line, so the six of the two crew loops are if / else, and the unit is
read through the slot global everywhere, with no local copy of the unit ID; the code is the same.

compiler-flags-gl.txt gets UpdateBallista.cpp and UnitsState/deselectUnit.cpp, which is at 100% on main and was on
no /GL list. The crew loop calls deselectUnit, and the compiler has to see its body: the original keeps edx across
the call at 0x0056F291 (used at 0x0056F296) and does not reload ecx before 0x0056F2A1; quick_diff with the /GL
callees in the unit reads 0.9836 with it and 0.9063 without it. hasUnitReachedDestination, the other callee it has
to see, is on main's list already (0.5999 without it, 0.5601 with no callee at all); sourcehold#398's manifest has both
edges.

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. UpdateBallista 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