Skip to content

Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) - #541

Merged
gynt merged 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-w4-1-100
Dec 2, 2026
Merged

gynt merged 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-w4-1-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Two UnitsState functions that read 100% until #238 (merged after both) put their files on the /GL list. Off the list both read 100.0% / 100.0% (reccmp raw / normalised) again, with OPENSHC_FUNCTION_VISIBILITY_UNITS off and on. No .cpp changes: the PR is two lines of cmake/compiler-flags-gl.txt, one commit each.

Function Address Before (raw / normalised) Now What the /GL build did
clearAllUnits 0x0052E7B0 91.7% / 80.0% 100.0% / 100.0% dropped the second load of LowLevelMemory into ecx (0x0052E80E)
computeLadderClimbPath 0x00533A10 96.9% / 93.8% off, 100.0% on 100.0% / 100.0% dropped the load of LowLevelMemory into ecx after the copyData call (0x00533A69)

"Before" is main's reading (0cea04d). The status lines of both already say 100.0% | Reimplemented (from #271 and #378), so they stay as they are.

What happens

  • clearAllUnits calls fillMemory_ByteValue once per unit in a loop and once more for the unit layer. The original loads ecx with LowLevelMemory in front of that last call too (0x0052E80E). fillMemory.cpp is on the /GL list; with clearAllUnits.cpp on it as well, MSVC knows the call leaves ecx alone and reuses it: one instruction less than the original. So the original was apparently compiled without fillMemory_ByteValue's body in view.
  • computeLadderClimbPath calls copyData and then fillMemory_ByteValue. The original loads ecx again between the two (0x00533A69). With the option on it already read 100%: copyData is then a member of a wrapper unit and off the /GL list. With the option off both were under /GL and the load went.
  • Why these two files, not the callees. fillMemory.cpp and copyData.cpp stay on the list: taking them off would change what every other /GL caller of fillMemory_ByteValue and copyData sees. These two lines touch only the two functions, their callers and their callees, and all of those are measured below.

Callers

A file that leaves the list no longer shows its body to the /GL files that call it. Every caller reads as before:

  • clearAllUnits: Constructor_UnitsState (0x0053B8B0) 100.0% / 100.0% and setupAllMapSections (0x00512100) 97.9% / 93.0%, as on main. What setupAllMapSections lacks is two loads of the same kind in its own code (0x0051229B, 0x005122C8). Its file is in TileMapState, and whether it can leave the list as well has not been measured, so this PR leaves it on.
  • computeLadderClimbPath: UpdateBurningMan (0x0053BC20), UpdateChild (0x005477C0), UpdateDrunkard (0x00567320), UpdateFireEater (0x00549650), UpdateHunterDog (0x0056B970), UpdateJester (0x0056E190), UpdateMother (0x00546BC0) and applyLadderDestructionToTribeUnits (0x00524E20, not on the list), all 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), sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. All builds are on main 0cea04d. The small builds are of this branch together with the change to processMeleeInitiation.cpp of #543 (that file calls neither function and is called by neither); the full build is of this branch alone.

  • Small builds, both modes (519 files, 529 functions: every /GL file of main, the two functions, every caller of either and the callee closures, every file of a function-visibility group that touches them): BUILD_OK and 0 failures in both. clearAllUnits and computeLadderClimbPath read 100.0% / 100.0% in both.
  • Against main with the option on (the same 519 files): every one of the 529 functions reads as on main but clearAllUnits (91.7% / 80.0% there) and processMeleeInitiation, the second PR's function.
  • Against main with the option off (main's small build has 436 of the files, 446 functions; the 83 other functions are compared with a full build of main): the only functions whose generated code differs are the two of this PR and processMeleeInitiation. Five more read differently with the build's file list, not with this change:
    • UpdateChild, UpdateFireEater and UpdateMother read 100.0% / 100.0% here and in main's full build, 99.5%, 98.7% and 99.5% raw in main's small build, whose list lacked some of their callees.
    • processEntityDamageToUnit reads 77.3% / 69.0% here and in main's full build, 71.5% / 67.9% in main's small one.
    • UpdateCatapultBuilding reads 99.6% / 95.7% here, 99.6% / 95.5% in main's small build and 100.0% / 100.0% in main's full build: its code is unchanged, a known tie with UpdateFireBallistaBuilding that goes either way with the build's file set.
  • Full build, option off, of exactly this branch (3041 files, 3059 functions, the 12 files that don't compile on main left out): BUILD_OK, 0 failures. clearAllUnits and computeLadderClimbPath read 100.0% / 100.0%, and against main's full build with the option off they are the only functions that read differently.
  • Checks: nothing to fix automatically; the style checks give no errors and two notes, the two deleted lines themselves.
  • Not run in game.

Other PRs

#536, #539 and #543 are ours and open. #543 changes processMeleeInitiation.cpp and its status line only. #536 and #539 change cmake/compiler-flags-gl.txt in other lines (#536 adds six units, #539 takes isTowerTileOvercrowdedByCurrentPlayer.cpp off); this branch merges cleanly with each. #501 and #528 (TroopValueState) add lines to the same file at the end of the file as it was when they branched; they conflict with main there already, and this branch does not touch those lines.

🤖 Generated with Claude Code

kotijekriv and others added 2 commits December 2, 2026 07:04
UnitsState::clearAllUnits (0x0052E7B0) reads 91.7% / 80.0% on main since sourcehold#238 put its file on the /GL list. It was at
100% from sourcehold#271 on, and its status line still says so. Off the list it reads 100.0% / 100.0% again; the .cpp is unchanged.

The function calls fillMemory_ByteValue once per unit in a loop and once more for the unit layer. The original loads
LowLevelMemory into ecx in front of that last call too (0x0052E80E). fillMemory.cpp is on the /GL list, so with
clearAllUnits.cpp on it as well MSVC knows that the call leaves ecx alone and drops the load: one instruction less than
the original.

Its two callers are on the /GL list and no longer see its body. Both read as before: Constructor_UnitsState
(0x0053B8B0) 100.0% / 100.0%, setupAllMapSections (0x00512100) 97.9% / 93.0%.

Measured with real builds on main 0cea04d (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp raw /
normalised): 100.0 / 100.0 in a small build with
OPENSHC_FUNCTION_VISIBILITY_UNITS off and in one with it on (519 files, 529 functions each: every /GL file of main, the
two functions of this branch, their callers and callee closures).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…100% with the units option off)


UnitsState::computeLadderClimbPath (0x00533A10) reads 96.9% / 93.8% with OPENSHC_FUNCTION_VISIBILITY_UNITS off since sourcehold#238
put its file on the /GL list, and 100% with the option on. It was at 100% in sourcehold#378, and its status line still says so. Off
the list it reads 100.0% / 100.0% in both modes; the .cpp is unchanged.

The function calls copyData and then fillMemory_ByteValue. The original loads LowLevelMemory into ecx again between the
two (0x00533A69). With the option on, copyData.cpp is a member of a wrapper unit and off the /GL list, so MSVC cannot see
that it leaves ecx alone, and the load is there. With the option off both files were under /GL and the load went.

Eight functions call it. The seven Update* functions among them are on the /GL list and no longer see its body with the option off
(with it on they never did). All read 100.0% / 100.0%, as in main's full build: UpdateBurningMan, UpdateChild, UpdateDrunkard,
UpdateFireEater, UpdateHunterDog, UpdateJester, UpdateMother and TribesState::applyLadderDestructionToTribeUnits.

Measured with real builds on main 0cea04d (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp raw /
normalised): 100.0 / 100.0 in a small build with the
option off and in one with it on (519 files, 529 functions each: every /GL file of main, the two functions of this
branch, their callers and callee closures).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@gynt
gynt added this pull request to the merge queue Dec 2, 2026
Merged via the queue into sourcehold:main with commit d7b115a Dec 2, 2026
5 checks passed
gynt added a commit to gynt/OpenSHC that referenced this pull request Dec 2, 2026
…#543)

UnitsState::processMeleeInitiation from 85.9% (gynt's text, on main since sourcehold#238) to the original's code. reccmp reads
99.0%: the seven lines it still shows are the same instructions with the same operand under another label (below), and
the status line counts the function as 100% with a note that says so, as main's file does for 0x004D9290.

quick_diff below is tools/reimplementation-helper/batch/quick_diff.py: this file compiled alone against the original's
disassembly, every global address folded to one token. A ratio from 0 to 1, not a reccmp percentage.

- The scan of the unit's own tile is a do loop behind its own test. The original loads `this` into edi in front of the
  loop's test (0x00549F4E) and uses it up to 0x0054A458. As a for loop MSVC loads it inside the loop and again behind the
  24 tile tests, and keeps a zero in edi from the loop's end to the function's end (quick_diff 0.8976 -> 0.9399).
- goto attack: when the unit already fought is still in reach, the original turns towards it and jumps into the code
  that starts the attack: the jmp at 0x0054A52B lands at 0x0054A6BA, which is also reached by falling through from
  0x0054A6B9. With a flag set in the loop and tested behind it MSVC keeps `this` in ebx over the rest of the function,
  where the original reads it from the stack for each call and has 0x6a and 8 in edi and edx (0x0054A6C5).
- goto noTarget: "nobody found on the 24 tiles" and "no enemy around" end in the same three stores and the same return,
  which the original has once: the je at 0x00549FCB lands at 0x0054A652, which is also reached by falling through from
  0x0054A650. Written twice, both copies stay.
- The three together: 0.9796, with the original's 734 instructions. Other forms, in quick_diff only: 0.9640 with one
  goto or with none (a flag that skips the second search, the attack nested in an if), 0.9244 with the attack written
  out at both places. The real build is of the form with the two goto.
- _adjacentTiles[13] adds [SomeY + 1][5] in front of [SomeY + 1][6], as _adjacentTiles[17] does with [2] and [3]: with
  [6] first MSVC put the two loads the other way round than the original has them (0x0054A2C0). quick_diff folds global
  addresses and did not show it; the first real build did.

What reccmp still shows: seven instructions (0x0054A20F, 0x0054A233, 0x0054A239, 0x0054A3D9, 0x0054A3DF, 0x0054A403,
0x0054A425) read directionTranslationMatrix one row above the current one, six written [SomeY][-1] to [-7] (gynt's
negative columns of the current row) and one [SomeY - 1][0]. The matrix is the first member of TileMapState, so their
displacement is TileMapState's address less 4 to 32. In the original TileMapState is at 0x01A93208 and MinimapViewState
(0x01A31610, 400376 bytes) ends where it starts, so reccmp prints MinimapViewState+400372 for TileMapState - 4. In the
DLL TileMapState's instance has other data in front of it and reccmp prints that: ViewportRenderState+1699844 (it is
1699848 bytes long) with the units option on, function-resolver objects with it off. The compiler's listing has
TileMapState's symbol less 4 to 32 in all seven, and all 85 global memory operands of the function are the original's
in the original's order. Any form that reads the row above ([SomeY][-1], [SomeY - 1][7]) comes to the same displacement
(both were compiled), and the label comes from the DLL's data layout, so no source form changes these lines; main's
build shows the same seven. The status line counts the function the way the file does for 0x004D9290 (sourcehold#510: the
instance[i - 1] address label) and for 0x00405130, 0x004274F0 and 0x00429A40 (sourcehold#408).

The rest is gynt's, the handwritten block included (7005c48 79.0%, 951fc13 84.5%, ff6859a 85.9%; main's status
line still said 84.5%). The second search loses one level of nesting, so its lines show as changed. The bound of both
searches is countof(field_0x11cf4[0]) in place of 24, the column count of the table they index: same object code.

Measured with real builds on main 0cea04d (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp), of
this file together with the two /GL lines of sourcehold#541: 99.0% in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS on
(519 files, 529 functions), where every other function but sourcehold#541's clearAllUnits reads as in the same build of main.
With the option off the same list reads 99.0% as well, the same seven lines (and 98.8% for the text before the
_adjacentTiles[13] change: those seven and that one sum). Two comments and the two loop bounds changed after the
builds; every section of the object file has the same bytes and relocations as before.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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.

2 participants