Skip to content

Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher - #421

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

kotijekriv wants to merge 3 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-C-round1-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Three more functions at 100%, with no header changes. All three are Map::Units functions from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), and all three commits credit him as co-author. The third, UpdateMinerIronFetcher, was added on top of the first two because it needs ConsiderHavingABreakNowUnk from this PR.

Function Address Before After
Units::ConsiderHavingABreakNowUnk 0x00530C40 gynt's draft 97.56% (84dce4b) 100.0%
Units::UpdateOx 0x0053F940 gynt's draft 67.64% (dab4c1d; 62.84% at c703e13) 100.0%, under /GL
Units::UpdateMinerIronFetcher 0x00566010 gynt's draft 49.78% (a2fddde; 44.16% at b3d9191) 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 this branch's source, with both resolver sets on: 0 failed, average 99.58 over 2442 functions, and no function on main reads lower or higher than in the main baseline build (99.58 over 2439). UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher read 100.0% raw and normalised, with no diff entry, and so does each of the eight callees UpdateMinerIronFetcher has to see: the six that the third commit puts on the /GL list and the two that were on it already.
  • Our own narrower run for the first two, made before const was added to seven locals (both functions' instruction streams are the same line for line before and after): UpdateOx 100.0% raw and normalised, ConsiderHavingABreakNowUnk 100.0% raw and normalised, its caller UpdateWorker_ConsiderTakingABreakUnk 100.0%. That was one run of 151 functions: the 133 files of main's /GL list that exist on main, the three files just named, and two files outside that set, a local draft of takeScreenshot.cpp (which main's list names but main does not have) and ResourceManager/paths.cpp (which it needs to link). Leaving out the five functions of those two files, 142 of 146 read 100.0%. The four below are lightUpBuilding 99.3%, updateBuildingPlacementRotationPreview 46.2%, playUnitSelectSpeech 94.1% and InitBlendFilterArraysUnk 97.9%: readings of this narrow file set, not regressions (main's status file has three of them at 100.0% and playUnitSelectSpeech at 82.4%), and this branch does not touch them. The full build above is the one to go by.
  • Our narrower run for UpdateMinerIronFetcher: the 133 files of main's /GL list that exist on main, parseHlp.cpp (the first entry of our local build list), UpdateOx.cpp and the third commit's seven files: 152 functions, average 99.59. UpdateMinerIronFetcher reads 100.0% raw and normalised, with no diff entries. The same build without that file and with its six callees built but off the /GL list, which is this PR before the third commit, gives 99.58 over 151, and each of those 151 functions reads the same in both builds. The six callees read 100.0% under /GL as without it, UpdateOx stays at 100.0%, and the functions below 100% are the four named above plus parseHlp 99.7%, with the same numbers in both.
  • 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 18 CHECK (9 on the first two commits, 9 on the third; explained below).
  • Not run in game.

What got them to 100%

  • ConsiderHavingABreakNowUnk: the draft was one instruction out of order. The original loads the unit's owner before the branch on destinationNeeded (the load at 0x00530C52 sits between the cmp and the je), although only the path behind that branch uses it; in the draft the compiler folded the read into its use behind the branch. Now the owner is read into an int const local above the test, and the player's productivity is read twice through it, with no local for the productivity. A value with two uses keeps its load where it is declared. Measured with quick_diff: the draft 0.9756, the owner as a short 0.9639, the local declared behind the test 0.8293, a PlayerData pointer 0.7952, this form 1.0000.

  • UpdateOx: three things.

    • It is on the /GL list (below).
    • Most accesses to the unit go through DAT_CurrentUnitSlotID::instance, the rest through the copy of the slot taken at the top. The original loads the global again behind a call that may change it and keeps using the copy it holds elsewhere. Which statement reads which was settled by measuring: in three places (0x0053FC32, 0x0053FED8, 0x0053FF2E) the original keeps using the copy it already holds in esi behind the call, where the source reads the global, and the compiler arrives at the same instructions.
    • In the eating-grass state the code that resets the state is written once, behind a flag. The flag starts TRUE, the arm for a frame above 0 clears it when the unit has not become idle, and if (reset) holds the reset. The compiler removes the flag by threading the jumps, and that leaves the original's layout: the arm for a frame of 0 or below jumps forward (0x0053FB3F) to the reset code at 0x0053FB5D, which sits behind the other arm's idle test. With the reset after the if/else, or written out in both arms, the compiler keeps the copy behind the first arm (97.8%).
  • UpdateMinerIronFetcher: apart from the dropped unitID copy, the draft's statements are all still there; what changed is how they are arranged, and what the compiler can see. On our dev branch (not public), reccmp raw / normalised with the file on the /GL list: the draft 68.1 / 67.9; every access through the slot 75.6 / 73.5; four right branch orders and a wrong one (US_DEATH_01 with the running frame first, undone again) 87.0 / 81.2; this PR 100.0 / 100.0.

    • It is on the /GL list, and every access reads the unit through DAT_CurrentUnitSlotID::instance: with no copy of the slot the compiler arrives at the original's loads (68.1% to 75.6%).
    • Four branch orders: the call that works is the first arm behind setWorkplaceBuildingEntryAsTarget in state 1 and behind canBuildingStoreTheAmount and buildingIsAccessible in the unloading state, and the odd slots come first where the waiting animation picks its frame table. US_DEATH_01 keeps the draft's order, the last frame first, which is the original's (jg at 0x00566677).
    • The working path is the fall-through, twice. Behind setDestinationForUnit in state 2 the original falls through into the path where the call worked and has the US_DISAPPEAR exit out of line (je at 0x005661CF); behind setWorkplaceBuildingEntryAsTarget in the unloading state likewise (je at 0x005664D1, the store of state 2 behind it at 0x005664D9). The draft had both as early exits with the rest behind them; now the call that works is the if body and the exit the else.
    • Two more callees have to be visible (next section): in quick_diff 0.9561 to 0.9839 for the first, and 0.9903 to 1.0000, the last step, for the second.
    • 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. The else parts of US_DEATH_01 and of the alive-check arm end in the same instructions; the original has them once, in the later state, and US_DEATH_01 jumps forward to them (0x005666A1 to 0x00566708). With blocks that return, the compiler kept the earlier copy.
    • quick_diff, with its /GL callees in the unit, reads 1.0000: 463 instructions against 463.

What the slot tells about the callees

The update functions keep the current unit's slot (DAT_CurrentUnitSlotID, 0x00EE0FC8) in a register and read it again behind a call that may change it. Where the original does not read it again, its compiler knew that the call leaves the slot alone, and ours can only know that when it sees the callee's body, and the bodies of everything that callee calls.

In UpdateMinerIronFetcher the original keeps the slot in ebx and its offset in esi:

  • across isWorkplaceBuildingOnAdjacentTile (the call at 0x005660E7, push ebx for the next call at 0x005660F2);
  • across UpdateWorker_ConsiderTakingABreakUnk (the call at 0x005661A1, the fields read through esi from 0x005661B1, push ebx at 0x005661C2);
  • and it does read the slot again behind setDestinationForUnit (0x005661D1), which may change it.

isWorkplaceBuildingOnAdjacentTile is a leaf. UpdateWorker_ConsiderTakingABreakUnk calls one function, ConsiderHavingABreakNowUnk, also a leaf. With both on the /GL list our compiler reaches the same conclusion as the original's.

Most workers' update functions call the break check (in #204, UpdateMiner and UpdateQuarryMason do not, and UpdateFletcher calls ConsiderHavingABreakNowUnk directly), so the lines for the break check and for ConsiderHavingABreakNowUnk matter beyond this function. In a build of our 29 unit drafts (not in this PR) with these lines, six of them rise by 0.7 to 3.0 points and one, whose branch orders had been searched under the old assumption, falls.

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

Kept as they are

  • UpdateOx, three shapes with a one-line why in the code: the reset flag (above); in the reset state the two stores that send the ox back are written on both paths (written once behind the if: quick_diff 0.8682, 437 instructions against 443); and the delivery tests are nested so that each failed test has its own reset of the load (as one && condition: 0.9591, 437 instructions).

  • UpdateOx, slot or local: which statements read the unit through the slot and which through unitID is match-driven too. That is in the commit text, not in comments.

  • Three (char) casts in UpdateOx: the two frame-table reads into animationFrame and (char)disappearFadeAlphaCountdown > 32. The headers have these as byte, and the original reads them signed (see the proposal below).

  • UpdateMinerIronFetcher, the 9 CHECKs of its commit: six (char) casts (the four frame-table reads into animationFrame and the two compares of disappearFadeAlphaCountdown: byte in the headers, read signed by the original, as in UpdateOx); (short)DAT_CurrentUnitSlotID::instance, the slot, an int global, stored into the mine's workers[], an array of short; and two placeholder names (next section).

  • UpdateMinerIronFetcher, not flagged: the chain of the states has a one-line why in the code. The draft's ternary on the mine's resources[RT_IRON] is an if / else (style guide 23); the compiler still emits the original's sete. BOOLEnum results are tested by truthiness where the call is the whole condition; SetStateToFreetimeWalking(...) == 0 stands inside an && condition and stays as it is. != 0 and == 0 also stay on short fields, on DAT_UnitHasBecomeIdle and on the two calls that return int (buildingIsAccessible, canBuildingStoreTheAmount).

What some placeholders hold

  • Unit::field_0x3f2: a flag of the ox. The waiting state clears it; it is set to 1 when an ox with a full load (8 or more) stops eating grass.

  • Unit::field_0x2fe: set to 0 when the unit goes resting. Of this PR's three functions only ConsiderHavingABreakNowUnk uses it, and nothing more is known from it.

  • Frames_Shared_UnitClimbingUp.field_0x4b1c and .field_0x4bc4: the ox's frame tables for eating grass and for dying.

  • Frames_Shared_UnitClimbingUp.field_0x5c94 and .field_0x5ce4: the fetcher's two frame tables for waiting at the mine until iron is ready; which one runs goes by the slot's lowest bit. That state carries the name US_AIM_WEAPONUnk (5) from the ranged units.

Proposed, not changed here

  • char for byte fields read as signed: Unit::disappearFadeAlphaCountdown and the AnimationFrameData frame tables, as on Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416. As char the nine casts above could go (three in UpdateOx, six in UpdateMinerIronFetcher).
  • Six lines of main's cmake/compiler-flags-gl.txt name files that main does not have (four files, two of them listed twice): takeScreenshot.cpp, determineBuildingPlacementRotation.cpp, executeDefaultCastleAIV.cpp and findAppropriateGridLocationForBuilding.cpp. Noticed while measuring; this PR leaves them alone.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 2 commits November 19, 2026 01:02
Units::ConsiderHavingABreakNowUnk, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (84dce4b at
97.56%, "load scheduling"). The draft was one instruction out of order: the original loads the unit's owner before
the branch on destinationNeeded (the load at 0x00530C52 sits between the cmp at 0x00530C4A and the je at
0x00530C59), although only the path behind that branch uses it, and in the draft the compiler folded the read into
its use behind the branch.

What the match needed: the owner is read into an int local above the test, and the player's productivity is read
twice through it, in the `< 100` test and in the target formula, with no local for the productivity. A value with
two uses keeps its load where it is declared; one with a single use is loaded at that use. Measured with
quick_diff: the draft 0.9756; the owner as a short 0.9639; the local declared behind the test 0.8293; a PlayerData
pointer 0.7952; this form 1.0000, 41 instructions on both sides and every jump on the same instruction.

Also US_UNKNOWN_121 for the state the draft wrote as (UnitState)121, and main's name field_0x2fe.

reccmp, with both resolver sets on: 100.0 (normalized 100.0), and the caller UpdateWorker_ConsiderTakingABreakUnk
stays at 100.0. That is from one run of 151 functions: the 133 files of main's /GL list that exist on main, this
function, its caller and UpdateOx, and two files outside that set, a local draft of takeScreenshot.cpp (which main's
list names but main does not have) and ResourceManager/paths.cpp (which it needs to link). The run was made before
`const` was added to the owner local; the function's instruction stream is the same line for line with and without
it. The function is not on the /GL list and needs no line there.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Units::UpdateOx, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (dab4c1d at 67.64%, "int
index local"; c703e13 at 62.84% before it).

How it got to 100:
- compiler-flags-gl.txt gets UpdateOx.cpp. The five callees whose bodies it has to see are on main's list already:
  addResourceToStockpile, buildingIsAccessible, checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination
  and makeUnitStopWalkingByClearingPathProgressState.
- Most accesses to the unit go through DAT_CurrentUnitSlotID::instance, the rest through the copy of the slot taken
  at the top. The original loads the global again behind a call that may change it and keeps using the copy it holds
  elsewhere. Which statement reads which was settled by measuring, not by copying the original: in three places
  (0x0053FC32, 0x0053FED8, 0x0053FF2E) the original keeps using the copy it already holds in esi behind the call,
  where the source reads the global, and the compiler arrives at the same instructions. With that, the wait state's
  success as the then-arm, the reset state's two stores written on both paths and the delivery tests nested so each
  failure keeps its own reset, the draft went to 97.8.
- The eating-grass state, 97.8 to 100: the code that resets the state is written once, behind a flag. The flag
  starts TRUE; the arm for a frame above 0 clears it when the unit has not become idle; `if (reset)` holds the
  reset, and the rest of the state follows it. The compiler removes the flag by threading the jumps, and that
  leaves the original's layout: the arm for a frame of 0 or below jumps forward (0x0053FB3F) to the reset code at
  0x0053FB5D, which sits behind the other arm's idle test. With the reset after the if/else, or written out in both
  arms, the compiler keeps the copy behind the first arm and the second arm jumps back to it (the 97.8). A goto
  into the reset code compiles to the same code as the flag.

Measured with quick_diff, the five /GL callees in the unit: this form has 443 instructions on both sides and every
jump on the same instruction, and the two lines it still shows are `mov word ptr [..], 0xffff` against `-0x1`, the
same bytes. The reset state's two stores written once behind the if: 0.8682, 437 instructions. The delivery tests
as one `&&` condition: 0.9591, 437 instructions.

reccmp, with both resolver sets on: UpdateOx 100.0 (normalized 100.0), ConsiderHavingABreakNowUnk and its caller
UpdateWorker_ConsiderTakingABreakUnk 100.0. That is from one run of 151 functions: the 133 files of main's /GL list
that exist on main, the three files just named, and two files outside that set, a local draft of takeScreenshot.cpp
(which main's list names but main does not have) and ResourceManager/paths.cpp (which it needs to link). Leaving out
the five functions of those two files, 142 of 146 read 100.0. The four below are lightUpBuilding 99.3,
updateBuildingPlacementRotationPreview 46.2, playUnitSelectSpeech 94.1 and InitBlendFilterArraysUnk 97.9: those are
readings of this narrow file set, not of a full build (main's status file has the first, second and fourth at 100.0
and playUnitSelectSpeech at 82.4), and this branch does not touch them. The run was made before `const` was added to
six locals and before the two comments on the nesting; the function's instruction stream is the same line for line
before and after.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Units::UpdateMinerIronFetcher, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his commits on
the file: b3d9191 at 44.16%, a2fddde at 49.78%, and the style commit 974c32f), through the forms on our
development branches, where its history is. On top of ConsiderHavingABreakNowUnk (ef8c838), which it needs.

What the match needed, against the draft (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 (68.1 -> 75.6).

- Four branches the way round the original has them: the call that works is the first arm behind
  setWorkplaceBuildingEntryAsTarget in state 1 and behind canBuildingStoreTheAmount and buildingIsAccessible in
  the unloading state, and the odd slots come first where the waiting animation picks its frame table. (The
  search that found them had also put the running frame first in US_DEATH_01, which read 87.0; that one was
  wrong and is not in this commit: the draft's order there, the last frame first, is the original's, jg at
  0x00566677.)

- The compiler has to see two more callees. The original keeps the slot in ebx and its offset in esi across
  isWorkplaceBuildingOnAdjacentTile (0x005660E7 to 0x005660F2) and across UpdateWorker_ConsiderTakingABreakUnk
  (0x005661A1 to 0x005661C2), and reads the slot again behind setDestinationForUnit (0x005661D1). So its compiler
  knew that the first two leave DAT_CurrentUnitSlotID alone. Ours knows it only when it sees their bodies, and for
  the break check also the body of ConsiderHavingABreakNowUnk, the one function it calls.

- The working path as the fall-through, twice: behind setDestinationForUnit in state 2 (je at 0x005661CF to the
  US_DISAPPEAR exit) and behind setWorkplaceBuildingEntryAsTarget in the unloading state (je at 0x005664D1, the
  store of state 2 behind it at 0x005664D9). Both were early exits with the rest behind them.

- 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. The else parts of US_DEATH_01 and of the alive-check arm end in the same instructions; falling
  out of a chain, the compiler keeps the later copy, as the original does (US_DEATH_01 jumps forward from
  0x005666A1 to 0x00566708). With blocks that return it kept the earlier one (-> 100.0).

The text follows main's unit functions: BOOLEnum results by truthiness where the call is the whole condition
(SetStateToFreetimeWalking's is compared with 0 inside an && condition and stays so), the names main has for the
states and for DNE_UNKNOWN_2 and RT_IRON, const on the five locals that are assigned once, the steps by one as
++ and --, the draft's ternary as an if / else (style guide 23; the compiler still emits the sete), and a
one-line why at the chain.

compiler-flags-gl.txt gets UpdateMinerIronFetcher.cpp and six of the callees it has to see:
ComputeGoodsProduced, IncrementAndOptionalUpdateAVValueRelated, SetStateToFreetimeWalking,
UpdateWorker_ConsiderTakingABreakUnk, isWorkplaceBuildingOnAdjacentTile and ConsiderHavingABreakNowUnk.
hasUnitReachedDestination and checkIfCitizenUnitIsAliveBasedOnState, the other two, are on the list already.
quick_diff with those eight in the unit reads 1.0000, 463 instructions against 463; leaving any one of them out
lowers it (0.8319 to 0.9903), and with none 0.4461.

Measured with reccmp, both resolver sets on:
- a /GL build of this file with its callee closure: UpdateMinerIronFetcher 100.0% raw and normalised, no diff
  entry; the six callees read 100.0% on the list as off it;
- a build of every file of main's /GL list that exists on main (3458fea), parseHlp.cpp, UpdateOx.cpp and
  this commit's seven files: 152 functions, UpdateMinerIronFetcher 100.0% with no diff entry; the same build
  without this file and with the six callees off the /GL list, as on 7af01b5: 151 functions, and each of
  them 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>

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