Repository navigation
One more Map::Units function at 100%: UpdateFireBallista - #452
Open
kotijekriv wants to merge 1 commit into
Open
kotijekriv wants to merge 1 commit into
kotijekriv wants to merge 1 commit into
Conversation
Units::UpdateFireBallista, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (b66f4b3 at 51.30%, after c9a76ac at 47.90%), through versions on our dev branch from 83.3% to 92.6%. The forms that matter, beyond the siege engine shapes it shares with UpdateBallista: - the waiting state has one `int i` for its four loops. The original reads the crew search's index after the loop: createTribe gets units[i].owner with i where the search ended (`imul ebx, ebx, 1168` on the search's counter at 0x00578339), not the ballista's owner. With the shared index the link-time code generation also gives the crew's stores the original's registers (0x6d and 5 in ebx and edi); with an index per loop it swaps them; - the two returning engineers are set out in a loop of two passes, which the compiler unrolls, as in UpdateBallista, and in the loop that checks them each reason to stop is an `if (...) { break; }` of its own. As one `||` condition the zero for the stores behind the loop is set behind it; the original keeps it in edx from in front of the loop (0x00577FC1); - the search for another player's crew returns straight from the end of its loop: `for (crewOwner = 1;; ++crewOwner) { if (crewOwner >= 9) return; if (crewCount[crewOwner] == 2) break; }`; - the moving state stores animationSheetFrameOffset at the start of both arms of `if (tracker == 0) { ... } else { if (tracker > 70) ... }`, which the compiler hoists to between the compare and its jump (0x00578BCB), and reads the unit through the slot global, with no function-wide copy of the unit ID (state 9 keeps its own, which the original holds in edi across its two calls); - the distances to an engineer are taken with the engineer's coordinate first, in the crew search the y distance through two locals. Each of these has a one-line comment in the code. The capture path keeps two calls as the original makes them, each with a fixme: createTribe gets the owner of units[i] (i is maxUnitCount there), and addUnitToTribe gets the tribe first and the unit second (0x00578359 to 0x00578360; 0x00522590 indexes the units with its first argument). Main's style-lint flags every ternary on one line, so the six of the return check and the crew search are if / else; the code is the same. Numbers and pointer arithmetic that have names in main's headers are written with those names, as in UpdateKnight (sourcehold#444). compiler-flags-gl.txt gets UpdateFireBallista.cpp and UnitsState/deselectUnit.cpp, which is at 100% on main and was on no /GL list. The captured ballista is deselected, and the compiler has to see deselectUnit's body: behind the call at 0x005782F2 the original pushes edx (the slot, loaded at 0x005782E6) again at 0x00578300 and calls clearOrDeselectUnitFromSelection at 0x00578302 without loading ecx again. quick_diff with the /GL callees in the unit reads 0.9874 with it and 0.8796 without it. hasUnitReachedDestination, the other callee it has to see, is on main's list already (0.5758 without it). 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. UpdateFireBallista 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 was referenced Nov 21, 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.
One more function at 100%, with no header changes.
Units::UpdateFireBallistais from gynt's units branchreimpl/map-units-updatefunctions(his draft #204), and the commit credits him as co-author. It is the fire ballista next toUpdateBallistaand shares most of its siege engine code.Units::UpdateFireBallista/GLMeasured.
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie./GLlines and with 0x004314E0 (theMapEditorLandscaping_GeneralButtonshandler) 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 undertools/).UpdateFireBallistareads 100.0 raw and normalised there too, with no diff entry, anddeselectUnitstays 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)./GLlist that exist on main,UpdateFireBallista.cppandUnitsState/deselectUnit.cpp: 145 functions, average 99.57.UpdateFireBallistareads 100.0% raw and normalised, with no diff entries (of this cut), anddeselectUnit100.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.openshc-pr-fixfinds nothing to change, andopenshc-pr-checkgives 0 ERROR and 36 CHECK (explained below).What got it to 100%
Every number here is with the file on the
/GLlist (below). On our dev branch (not public) it went from 83.3% to 91.9% with the siege engine shapes ofUpdateCatapult(the ones listed on #451) and the states as one chain. Then it took the same three changes that broughtUpdateBallistato 100% (the two returning engineers set out in a loop of two passes that the compiler unrolls, the distance to an engineer with the engineer's coordinate first, the engineer's slot read once in the loop that removes the crew), which gave 92.6%. Five things were the fire ballista's own.createTribegetsunits[i].owner, withiwhere the crew search ended: the original multiplies the search's counter (imul ebx, ebx, 1168; movsx ecx, [ebx + owner]at 0x00578339), not the slot. So the state was written C-style, with oneint ifor its loops. That is also what the last two lines needed: with one index for the state's four loops, the link-time code generation puts 0x6d and 5 for the crew's stores intoebxandedi, as the original does, and with an index per loop it swaps them (99.4% against 100.0%). quick_diff cannot see that difference, also not with/Ziand the precompiled header; only the real build shows it. The two comments in the code say why.if (...) { break; }of its own (the uid differs, the engineer has not arrived, the heights differ by more than 16). The original keeps the zero for the stores behind that loop inedxfrom in front of the loop (xor edx, edxat 0x00577FC1, again at the loop's head (0x00577FD1) and where the loop is left early (0x0057809E)), and stores the fade byte as an immediate. With the three reasons in one||condition, the zero is set behind the loop (quick_diff 0.9704 and 943 instructions, against 0.9874 and 947).for (crewOwner = 1;; ++crewOwner) { if (crewOwner >= 9) return; if (crewCount[crewOwner] == 2) break; }. With the test in the loop's header (crewOwner < 9) andif (crewOwner >= 9) return;behind it, the compiler testscrewOwnera second time (quick_diff 0.9863 and 949 instructions, against 0.9874 and 947).animationSheetFrameOffsetat the start of both arms ofif (tracker == 0) { … } else { if (tracker > 70) … }. The compiler hoists the store out of the two arms to between the compare and its jump, where the original has it (cmp word [tracker], bpat 0x00578BCB, then the store andje). As one statement in front of an if / else-if chain, the store stays above the compare. The compiler hoists only from the two arms of oneif, not across anelse ifchain.unitID): the state ofUS_MOVE_TO_DESTINATIONandUS_IDLEUnkreads the unit throughDAT_CurrentUnitSlotID::instance; with the local, the tracker is read once into a register where the original reads it from memory twice. State 9 keeps itscurrent, which the original holds inediacross its two calls (0x00578639 to 0x00578672).quick_diff, with its
/GLcallees in the unit, reads 0.9874: 947 instructions against 951. The rest are four alignmentjmps that quick_diff does not lay out, the spellings of one mask and of0xffff, and the tile-cost table's address in one operand.Lines added to
cmake/compiler-flags-gl.txtUpdateFireBallista.cppandUnitsState/deselectUnit.cpp.deselectUnitis at 100% on main and was on no/GLlist. The captured ballista is deselected, and the compiler has to seedeselectUnit's body: behinddeselectUnitat 0x005782F2 the original pushesedx(the slot, loaded at 0x005782E6) again at 0x00578300, and callsclearOrDeselectUnitFromSelectionat 0x00578302 without loadingecxagain. quick_diff reads 0.9874 with it and 0.8796 without it.hasUnitReachedDestination, the other callee it has to see, is on main's list already (0.5758 without it).Kept as they are
int i;,int dx;andint dy;without a value (fivedeclared without a valueCHECKs) and five loops without their counter declared in the header (five CHECKs): the state's four loops share the index above, and the search for another player's crew counts withcrewOwner, which is used behind it;dxanddyare 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 return check and the crew search are if / else now; the instruction list is the same line for line.createTribegetsunits[i].ownerwithi=maxUnitCount, where the crew search ended (0x00578339).addUnitToTribegets the tribe first and the unit second (0x00578359 to 0x00578360), the other way round from its parameters(unitID, tribeID); 0x00522590 indexes the units with its first one. Both look like bugs of the original, and the code keeps them for the match, each with afixme:.units[slot].ownerread again althoughownerholds it (three times), because the original reads it again too (movsxat 0x00577DA5, 0x00577F98 and 0x005781C6).(int)widthOrHeight / 2:widthOrHeightis auint, and the original divides signed (cdq; sub; sar).(char)casts: two frame-table reads intoanimationFrameand one test ofdisappearFadeAlphaCountdown. The headers have these asbyte, and the original reads them signed.field_0x58 = 0, 3 times (theSG 5CHECKs): the checker takes the name for aBOOLEnumof another struct; inUnit.hppit is ashort, and the original stores a word.What some placeholders hold
Unit::field_0x3bc: a countdown before the fire ballista looks for a target again, as inUpdateBallista: set to -60 or -80 when there is no target, to 0 by the two moving states, and counted up by one per update while negative.Unit::field_0x58: ashortthat three of its states set to 0 (waiting for a crew, aiming and loading).PathFindingState::field_0x9c: read after each call ofpathFindingRelatedin the state that moves towards the enemy. While it is 2 or less, the next call tries a wider radius (10, 20, 60, 120).Frames_Shared_UnitClimbingUp:.field_0x35dcis loading,.field_0x3654firing.Proposed, not changed here
charfor byte fields read as signed: the AnimationFrameData frame tables andUnit::disappearFadeAlphaCountdown, as on Five more functions at 100%: InGameMenu MainButtons, Roundtable handler, BuySell, UpdateGhost and UpdateHopsFarmer #416 and Three more Map::Units functions at 100%: UpdateOx, ConsiderHavingABreakNowUnk and UpdateMinerIronFetcher #421. Ascharthe three casts above could go.Other PRs
UpdateFireBallista.cpptoo: an add/add conflict on that file, and the status line at 0x00577CC0 differs (100.0% here against his 51.3%). Once this lands, his branch can take main's version of it.cmake/compiler-flags-gl.txt. The conflict is line-level only: whichever lands second needs a trivial merge that keeps the lines of both. Reimpl/TileMapState 100% batch 2 #396 already conflicts with main on its own. One more Map::Units function at 100%: UpdateMangonel #445, One more Map::Units function at 100%: UpdateBallista #451 and Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 add the samedeselectUnit.cppline; Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 merges with no conflict, so with it in, the list names the file twice, which does no harm. Map::Units::UpdateLion at 100% #425, Reimplement 47 UnitsState functions at 100% and restyle 28 more #378, Reimplement 20 BuildingsState functions at 100% #377, Reimplement 88 Map::TileMapState functions at 100% #366, Reimplement the remaining Map::Buildings Update* functions #297 and Reimplement 16 rendering, UI and GameSynchronyState functions (not yet 100%) #313 merge cleanly there, and cmake: optional wrapper units built from function-visibility.txt #399 doesn't touch the list./GLlist:checkTargetBuildingPossibilityOrState,clearOrDeselectUnitFromSelection,deselectUnit,moveToFreeTileNearbyandsetUnitFacingDirectionForTargetXandY. We have not measured this function with Reimplement 47 UnitsState functions at 100% and restyle 28 more #378 in the tree. The calls at risk are the first two: the original reads the slot global again behind each (0x00577EE1, 0x00578307), and if their bodies are visible and leave the slot alone, the build may stop reloading it.🤖 Generated with Claude Code