Skip to content

Reimplement UpdateBrewer from gynt's units branch (100% with OPENSHC_FUNCTION_VISIBILITY_UNITS on) - #562

Open
kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-brewer-100
Open

kotijekriv wants to merge 2 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-A-brewer-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

The last of the twelve units on gynt's units branch reimpl/map-units-updatefunctions (his draft #204) that call BuildingsState::getBuildingResourceAmountByUid, which main defines inline since #536; main has the other eleven (six from #536, two from #561, and UpdateEngineer, UpdateInnkeeper and UpdateMiller from #514, #393 and #533). The branch is on main 33cdd1f. UpdateBrewer reads 100.0% raw and normalised with main's function-visibility option on, and 93.0% / 85.5% in the default build (see the table). It gets an entry in cmake/function-visibility.txt. The unit commit cites gynt's commits on the file and credits him as co-author; the text work began on other development branches of ours, written with Claude Fable 5.2, and the unit commit keeps that co-author line.

Function Address Before Default build Option on
Units::UpdateBrewer 0x005565D0 gynt's draft 62.38% (284b976) 93.0% / 85.5% 100.0%

"Before" is gynt's own status number on the file (main's status file has 0.0%, Pending). The default build's numbers are raw / normalised. The status line carries the default build's raw number with the remark 100.0 with OPENSHC_FUNCTION_VISIBILITY_UNITS on, as main's UpdatePitchman, UpdateEngineer and UpdateDairyFarmer have (and #561's UpdateBaker).

The sweeping state

In state 1 (sweeping the brewery) the unit stores animationSpeed = 2, picks the frame from one of four tables by substate, and plays the broom sound when the frame is 2 and the cycle has just moved on. The original stores the 2 and compares with 2 as two immediates (0x0055667B, 0x005566E1), with the cycle number in ecx for the table reads. With the sound test once after the four arms, as in the draft, the 2 is kept in ecx for the compare (mov ecx, 2 ... cmp eax, ecx), the cycle number moves to edx, the sound call's two pushes change places, and further on the owner's push for the next stockpile changes register: 98.5% / 82.8% with the option on, 0.9846 in quick_diff. With the test written at the end of each arm, the four copies fold back into one and the 2 stays an immediate: 100.0%, 0.9988 (the rest is spelling). A comment above the arms says so.

Tried first and not it: the field types (both int; a short or unsigned store or compare shares the 2 all the same), the other uses of 2 in the function (changing any of them leaves the register), the 2 through a local, and the frame test as a one-case switch (the 2 goes, but the frame is read again from memory).

The entries

  • With the option on, every caller and callee joined by entries ends up in one group, each group is built as one wrapper unit that #includes its members (without /GL), and the members leave the /GL list. The entry names the nine callees UpdateBrewer calls, and setupUnitSharingCurrentTilePosition and setupUnitSharingTileIDs, which it reaches through commitUnitLocation (commitUnitLocation -> setupUnitSharingCurrentTilePosition -> setupUnitSharingTileIDs). Every name of the entry is already a member of the group that holds hasUnitReachedDestination (as are Reimplement UpdateQuarryWorker and UpdateBaker from gynt's units branch (100%, one with OPENSHC_FUNCTION_VISIBILITY_UNITS on) #561's UpdateQuarryWorker and UpdateBaker): the unit joins it, and no other function sees a body it did not see before or loses one. cmake/function-visibility-units.py makes 25 wrapper units holding 175 source files on this branch (main e3e4265: 25 units, 174 files; on 87e40b6: 172 on the branch, 171 on main): the unit is the only new member.
  • quick_diff, the unit with the callees of its entry in front of it. The rest to 1 is spelling in quick_diff's two streams (a negative byte immediate prints as 0xff on the original's side).
    • UpdateBrewer: 0.6297 alone, 0.9988 with the 11 listed. Left out, getBuildingResourceAmountByUid 0.9690; setupUnitSharingTileIDs 0.9963; IncrementAndOptionalUpdateAVValueRelated 0.8912; ConsiderHavingABreakNowUnk 0.9772; SetStateToFreetimeWalking 0.9534; ComputeGoodsProduced 0.9661; checkIfCitizenUnitIsAliveBasedOnState 0.9594; hasUnitReachedDestination 0.9289; setupUnitSharingCurrentTilePosition 0.9963; UpdateWorker_ConsiderTakingABreakUnk 0.9932; commitUnitLocation 0.9963. Not named, leaving it out changes nothing: makeUnitStopWalkingByClearingPathProgressState.

Measured

Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed: the data resolvers with tools/reimplementation-control/enable_reimplemented_data.py, the function resolver of every .cpp in the build with tools/reimplementation-helper/batch/enable_local_functions.py), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The small builds below are of main 87e40b6 and of this branch on it, and the last one of this branch on c5de30d; the branch was then rebased onto e3e4265 and 33cdd1f. Between 87e40b6 and c5de30d main took #561 (UpdateQuarryWorker, UpdateBaker and Unit::field_0x410 as char), gynt's #551 (three GameStateStructures functions) and his reccmp/float-literals.csv, which pairs the original's float constants and CRT helpers; UpdateBrewer has no float constant and calls no CRT helper. e3e4265 adds #548: one Credits .cpp and its status line, nothing in cmake/, so the counts above hold; 3b9e506 and 33cdd1f are gynt's header syncs for #559 (PlayerData::foodTypeToBeEatenNext typed as the new FoodTypeEnumInt, which no .cpp names), and no .cpp changes.

Against gynt's draft

The text was compared with gynt's as folded text (spelling, qualifiers and his local copy of the slot folded): every call with its arguments, every test and every store. UpdateBrewer has 60 calls and 68 tests where his text has 57 and 64: the sound call and its test stand in each of the four substate arms (+3 each, above), and his ternary is an if (+1). Otherwise the calls and tests are the same. A BOOLEnum call's != 0 / == FALSE written as a truth test and tests turned round where arms are swapped are not listed. What differs:

  • Every access reads the unit through DAT_CurrentUnitSlotID::instance; his text keeps the slot in a local read at the top.
  • State 1: the sound test at the end of each substate arm (above), and the next substate (105 after 104, else 102) as an else if / else where his text has a ternary (the same object code).
  • Any other state (back to state 1, the walk stopped) is the else of the death-state test (checkIfCitizenUnitIsAliveBasedOnState, which returns TRUE for the death states; its branch plays the death frames); his text has it after that test, whose branch returns, and then the object code differs (the unit with the entry's callees in front). A comment there says so.
  • The arms the other way round, by state: 2, taking hops (no hops left at the stockpile first: the next stockpile, the way home or the removal; the take after it); 4, walking to the brewery (a destination set first, the exit to disappearing after it); 8, carrying ale (space in the stockpile first; the other stockpile reachable first; at the end, the next hops stockpile reachable first). Each was measured with his order, with an early return where his has one: the object code differs (in the last one only with the entry's callees in front). A comment above each of these tests says so.

Kept as they are

Names on main this file shows (follow-ups, not changed here)

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 2 commits December 5, 2026 01:42
…N_VISIBILITY_UNITS on

Units::UpdateBrewer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (his draft PR 204; his
commits on the file: 3062b29 at 55.52%, 284b976 at 62.38%, then 974c32f's clang-format), through the forms
on our development branches. With OPENSHC_FUNCTION_VISIBILITY_UNITS on, its entry in cmake/function-visibility.txt
and getBuildingResourceAmountByUid defined inline in its own file (on main since PR 536) it reads 100.0 / 100.0
(reccmp raw / normalised). Compared with his draft as folded text (spelling, qualifiers and the local copy of the
slot folded): the text has 60 calls and 68 tests where the draft has 57 and 64, because the sound call and its test
stand in each of the four substate arms (+3 each) and the ternary is an if (+1); otherwise the calls have the same
arguments, and the tests and the stores are the same but for the list below (a BOOLEnum call's `!= 0` / `== FALSE`
written as a truth test, and tests turned round where the arms are swapped, are not listed).

- Every access reads the unit through DAT_CurrentUnitSlotID::instance; the draft keeps the slot in a local read at
  the top.
- State 1, sweeping: the sound test (animationFrame == 2 and animationCycleNumberHasJustIncremented, then
  playSFXAtLocation with FX_BROOM) stands at the end of each of the four substate arms, where the draft has it once
  after them. The four copies fold back into one, but with the test once after the arms the 2 of
  `animationSpeed = 2` is kept in ecx for the compare (`mov ecx, 2` ... `cmp eax, ecx`); the original stores and
  compares two immediates (0x0055667B, 0x005566E1). quick_diff with the entry's callees in front reads 0.9846 with
  the test after the arms and 0.9988 with it in each arm (the rest is spelling).
- State 1: the next substate (105 after 104, else 102) is an else if / else where the draft has a ternary; the
  object code is the same.
- Any other state (back to state 1, the walk stopped) is the else of the death-state test
  (checkIfCitizenUnitIsAliveBasedOnState, which returns TRUE for the death states), where the draft puts it after
  that test, whose branch returns; with the draft's form the object code differs (the unit with the entry's callees
  in front).
- Arms the other way round than in the draft, by state: 2, taking hops (no hops left at the stockpile first: the
  next stockpile, the way home or the removal; the take after it); 4, walking to the brewery (a destination set
  first, the exit to disappearing after it); 8, carrying ale (space in the stockpile first; the other stockpile
  reachable first; at the end, the next hops stockpile reachable first). Each was measured: with the draft's order
  the object code differs (in the last one only with the entry's callees in front), and a comment above each test
  says so.

Text pass, the same object code before and after (the unit alone and with its entry's callees in front, compiled
without /GL, as for PR 561's units): states by UnitState's names (there is no UnitState_Brewer; the resting state 1
as US_IDLEUnk), DNE_UNKNOWN_2, hex constants as decimal, the file-scope using lines gone, the (char) casts on the
fade countdown and the frame tables dropped (char in the headers), engineerManningSiegeStateRef_checkType set to -2,
++ / -- for the steps of one, the BOOLEnum calls tested by truthiness, const on the locals, back to his blocks that
return, one per state, the ternary as an else if / else, comments on what states 109, 110, 111, 121 and the other
death states do, and whys above the sound tests, the swapped tests and the death-state test.

Measured with real builds (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp raw / normalised):
small builds on main 87e40b6, before this branch was rebased (490 files, 503 functions: every /GL file, the unit,
every .cpp that names getBuildingResourceAmountByUid, findQuarryPileThatIsCloseAndHasMostStone, the callees of these
that read 100.0 down their call trees, IO/ResourceManager/paths.cpp for the link, and every file of a
function-visibility group that touches one of these), with OPENSHC_FUNCTION_VISIBILITY_UNITS on and off, 0 failures.
On: 100.0 / 100.0. Off: 93.0 / 85.5. On c5de30d (PR 561's UpdateQuarryWorker and UpdateBaker on main), the same
list with those two, option on: UpdateBrewer, UpdateQuarryWorker and UpdateBaker 100.0 / 100.0. The branch is now on
e3e4265, which adds PR 548 (one Credits .cpp and its status line). The status line carries the default build's
raw number. Our earlier text, with the sound test once after the arms, read 98.5 / 82.8 on and 91.4 / 84.2 off in
small builds of the same list.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateBrewer needs the bodies of the callees below in the unit it is compiled with: the nine it calls, and
setupUnitSharingCurrentTilePosition and setupUnitSharingTileIDs, which it reaches through commitUnitLocation
(commitUnitLocation -> setupUnitSharingCurrentTilePosition -> setupUnitSharingTileIDs). The entry names them at the
unit's address, the callees in address order. Every name is already a member of the group that holds
hasUnitReachedDestination, so the unit joins that group and no other function sees a body it did not see before or
loses one. The generator makes 25 wrapper units holding 175 source files (main e3e4265: 25 units, 174 files; on
87e40b6: 172 on the branch, 171 on main): the unit is the only new member.

Measured with quick_diff, the unit compiled with the callee files in front of it (the getter inline) against the
unit alone. The rest to 1 is spelling in quick_diff's two streams (a negative byte immediate prints as 0xff on the
original's side).
- UpdateBrewer: 0.6297 alone, 0.9988 with the 11 listed. Left out, getBuildingResourceAmountByUid 0.9690;
  setupUnitSharingTileIDs 0.9963; IncrementAndOptionalUpdateAVValueRelated 0.8912; ConsiderHavingABreakNowUnk
  0.9772; SetStateToFreetimeWalking 0.9534; ComputeGoodsProduced 0.9661; checkIfCitizenUnitIsAliveBasedOnState
  0.9594; hasUnitReachedDestination 0.9289; setupUnitSharingCurrentTilePosition 0.9963;
  UpdateWorker_ConsiderTakingABreakUnk 0.9932; commitUnitLocation 0.9963. Not named, leaving it out changes nothing:
  makeUnitStopWalkingByClearingPathProgressState.

Real builds: the small builds of the unit commit. With the option on the unit reads 100.0 / 100.0. Against main
87e40b6's build of the same list nothing else reads differently; on c5de30d the unit, UpdateQuarryWorker and
UpdateBaker read 100.0 / 100.0.

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