Repository navigation
Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher - #421
Open
kotijekriv wants to merge 3 commits into
Open
kotijekriv wants to merge 3 commits into
kotijekriv wants to merge 3 commits into
Conversation
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>
This was referenced Nov 19, 2026
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 was referenced Nov 20, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 needsConsiderHavingABreakNowUnkfrom this PR.Units::ConsiderHavingABreakNowUnkUnits::UpdateOx/GLUnits::UpdateMinerIronFetcher/GLMeasured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie.UpdateOx,ConsiderHavingABreakNowUnkandUpdateMinerIronFetcherread 100.0% raw and normalised, with no diff entry, and so does each of the eight calleesUpdateMinerIronFetcherhas to see: the six that the third commit puts on the/GLlist and the two that were on it already.constwas 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 callerUpdateWorker_ConsiderTakingABreakUnk100.0%. That was one run of 151 functions: the 133 files of main's/GLlist that exist on main, the three files just named, and two files outside that set, a local draft oftakeScreenshot.cpp(which main's list names but main does not have) andResourceManager/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 arelightUpBuilding99.3%,updateBuildingPlacementRotationPreview46.2%,playUnitSelectSpeech94.1% andInitBlendFilterArraysUnk97.9%: readings of this narrow file set, not regressions (main's status file has three of them at 100.0% andplayUnitSelectSpeechat 82.4%), and this branch does not touch them. The full build above is the one to go by.UpdateMinerIronFetcher: the 133 files of main's/GLlist that exist on main,parseHlp.cpp(the first entry of our local build list),UpdateOx.cppand the third commit's seven files: 152 functions, average 99.59.UpdateMinerIronFetcherreads 100.0% raw and normalised, with no diff entries. The same build without that file and with its six callees built but off the/GLlist, 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/GLas without it,UpdateOxstays at 100.0%, and the functions below 100% are the four named above plusparseHlp99.7%, with the same numbers in both.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives 0 ERROR and 18 CHECK (9 on the first two commits, 9 on the third; explained below).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 thecmpand theje), 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 anint constlocal 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 ashort0.9639, the local declared behind the test 0.8293, aPlayerDatapointer 0.7952, this form 1.0000.UpdateOx: three things.
/GLlist (below).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.TRUE, the arm for a frame above 0 clears it when the unit has not become idle, andif (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
unitIDcopy, 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/GLlist: 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_01with the running frame first, undone again) 87.0 / 81.2; this PR 100.0 / 100.0./GLlist, and every access reads the unit throughDAT_CurrentUnitSlotID::instance: with no copy of the slot the compiler arrives at the original's loads (68.1% to 75.6%).setWorkplaceBuildingEntryAsTargetin state 1 and behindcanBuildingStoreTheAmountandbuildingIsAccessiblein the unloading state, and the odd slots come first where the waiting animation picks its frame table.US_DEATH_01keeps the draft's order, the last frame first, which is the original's (jgat 0x00566677).setDestinationForUnitin state 2 the original falls through into the path where the call worked and has theUS_DISAPPEARexit out of line (jeat 0x005661CF); behindsetWorkplaceBuildingEntryAsTargetin the unloading state likewise (jeat 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 theifbody and the exit theelse.US_DEATH_01and of the alive-check arm end in the same instructions; the original has them once, in the later state, andUS_DEATH_01jumps forward to them (0x005666A1 to 0x00566708). With blocks that return, the compiler kept the earlier copy./GLcallees 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
UpdateMinerIronFetcherthe original keeps the slot inebxand its offset inesi:isWorkplaceBuildingOnAdjacentTile(the call at 0x005660E7,push ebxfor the next call at 0x005660F2);UpdateWorker_ConsiderTakingABreakUnk(the call at 0x005661A1, the fields read throughesifrom 0x005661B1,push ebxat 0x005661C2);setDestinationForUnit(0x005661D1), which may change it.isWorkplaceBuildingOnAdjacentTileis a leaf.UpdateWorker_ConsiderTakingABreakUnkcalls one function,ConsiderHavingABreakNowUnk, also a leaf. With both on the/GLlist our compiler reaches the same conclusion as the original's.Most workers' update functions call the break check (in #204,
UpdateMinerandUpdateQuarryMasondo not, andUpdateFletchercallsConsiderHavingABreakNowUnkdirectly), so the lines for the break check and forConsiderHavingABreakNowUnkmatter 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.txtUpdateOx.cpp. Its five callees that have to be visible are on main's list already:addResourceToStockpile,buildingIsAccessible,checkIfCitizenUnitIsAliveBasedOnState,hasUnitReachedDestinationandmakeUnitStopWalkingByClearingPathProgressState. quick_diff reads 0.9955 with them visible (the rest is two0xffffagainst-0x1renderings of the same bytes) and 0.9016 with none of them.ConsiderHavingABreakNowUnkneeds no line for itself: it reads 100.0% on the list as off it. It is on the list forUpdateMinerIronFetcher, which has to see it through the break check.UpdateMinerIronFetcher.cppand six of the eight callees it has to see. All six are at 100% (five on main, the sixth in this PR) and were not on the list:ComputeGoodsProducedIncrementAndOptionalUpdateAVValueRelatedSetStateToFreetimeWalkingUpdateWorker_ConsiderTakingABreakUnkUnitsState::isWorkplaceBuildingOnAdjacentTileConsiderHavingABreakNowUnkThe other two are on main's list already:
hasUnitReachedDestination(0.9570 without it) andcheckIfCitizenUnitIsAliveBasedOnState(0.8319). With all eight quick_diff reads 1.0000, with none 0.4461. Three more of its callees are on main's list and make no difference to it either way:addResourceToStockpile,buildingIsAccessibleandmakeUnitStopWalkingByClearingPathProgressState(1.0000 without each).What the six lines do to other functions: nothing that our narrower run can see: with and without them, each of the 151 other functions reads the same. Apart from gynt's drafts in Reimpl/map units updatefunctions #204, Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416's
UpdateHopsFarmeris the one function in another open PR that calls these callees (four of them, the break check among them). In a build of the merge of Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416 with this branch it reads 99.7% / 90.9%, Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416's own number, with the same one hunk (the owner pushed througheaxwhere the original usesecx), both with these lines and without the four of the third commit's seven lines that Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416 does not add (the file itself and three callees). With these lines,UpdateMinerIronFetcher,UpdateOxandUpdateGhostread 100.0% in that merge.Reimplement 33 Synchrony::Commands functions at 100% #415, Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416, Reimpl/TileMapState 100% batch 2 #396 and Two more Map::Units functions at 100%: UpdateHunterDog and UpdateFireWatch #433 also add lines at the end of
cmake/compiler-flags-gl.txt, so whichever goes in second gets a small conflict there (Reimpl/TileMapState 100% batch 2 #396 already conflicts with main there): keep the lines of both. Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416 adds three of the same lines itself (ComputeGoodsProduced,IncrementAndOptionalUpdateAVValueRelated,SetStateToFreetimeWalking), so keeping both leaves those three in the list twice, and Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 does the same withisWorkplaceBuildingOnAdjacentTile.cpp. Main's list has such pairs already; a line listed twice does no harm.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
unitIDis match-driven too. That is in the commit text, not in comments.Three
(char)casts in UpdateOx: the two frame-table reads intoanimationFrameand(char)disappearFadeAlphaCountdown > 32. The headers have these asbyte, 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 intoanimationFrameand the two compares ofdisappearFadeAlphaCountdown:bytein the headers, read signed by the original, as in UpdateOx);(short)DAT_CurrentUnitSlotID::instance, the slot, anintglobal, stored into the mine'sworkers[], an array ofshort; 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'ssete.BOOLEnumresults are tested by truthiness where the call is the whole condition;SetStateToFreetimeWalking(...) == 0stands inside an&&condition and stays as it is.!= 0and== 0also stay onshortfields, onDAT_UnitHasBecomeIdleand on the two calls that returnint(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 onlyConsiderHavingABreakNowUnkuses it, and nothing more is known from it.Frames_Shared_UnitClimbingUp.field_0x4b1cand.field_0x4bc4: the ox's frame tables for eating grass and for dying.Frames_Shared_UnitClimbingUp.field_0x5c94and.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 nameUS_AIM_WEAPONUnk(5) from the ranged units.Proposed, not changed here
charfor byte fields read as signed:Unit::disappearFadeAlphaCountdownand the AnimationFrameData frame tables, as on Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416. Ascharthe nine casts above could go (three in UpdateOx, six in UpdateMinerIronFetcher).cmake/compiler-flags-gl.txtname files that main does not have (four files, two of them listed twice):takeScreenshot.cpp,determineBuildingPlacementRotation.cpp,executeDefaultCastleAIV.cppandfindAppropriateGridLocationForBuilding.cpp. Noticed while measuring; this PR leaves them alone.Other PRs
UpdateMinerIronFetcher.cppand one more hunk in the status file, at 0x00566010 (100.0% here against his 49.8%). Once this lands, his branch can take main's version of them.UpdateHunterDog,UpdateFireWatch), Reimplement 33 Synchrony::Commands functions at 100% #415, Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416 and Reimpl/TileMapState 100% batch 2 #396 conflict with this PR incmake/compiler-flags-gl.txtonly, as named above. Map::Units::UpdateLion at 100% #425 and Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 merge cleanly./GLlines in the tree,UpdateMinerIronFetcherstill reads 100.0% raw and normalised (measured on a local test tree, not on main with Reimplement 47 UnitsState functions at 100% and restyle 28 more #378).🤖 Generated with Claude Code