Repository navigation
Reimplement 16 TileMapState functions that need volatile reads or a __forceinline resolver get() - #519
Merged
Conversation
…_forceinline resolver get() Split out of #396 at review. Five of them are compiled without global optimisation (#pragma optimize or an asm jump to a C label), which only matches when FunctionResolver's get() is __forceinline; the other eleven need a volatile read the original's code shape implies. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XmXCmZXUzxbDcYJj2mVg9V
…ped with jz $+N The original neighbour-flags macros jumped relative to the location counter (jz $+9 / jz $+12), as a macro used several times in one function cannot repeat a label. MSVC encodes such a jump in the near form 0F 84 rel32 and counts it as control flow leaving the __asm block, so it raises C4740 and turns global optimisation off for the enclosing function. That is the unoptimised frame of every original function that contains these macros. MACRO_ASM_JZ_NEAR now writes that jump, so setBitFlagBasedOnWallTowerGatehouseOrKeep, setTerrain, updateGFXLayers and updateMacroLayerRelated need no pragma, and updateGfxLayer's "__asm jmp wallAlpha __asm jmp wallAlpha" is a plain goto. All five compile to the same bytes as before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… in one unit with it The original reads the tile and y out-parameters of getTileForBrush once after the call and keeps them in registers across later stores and calls. A plain copy of them compiles that way only when the callee's body is in the same translation unit, as it was in the original; with the callee out of sight c2 propagates the copy, which is what the volatile reads worked around. function-visibility.txt now lists createPlateau, isValidCastleSiteLocation, setLand, useEraserBrush, useLevelBrush and useTerrainHeightBrush as callers of getTileForBrush, and their volatile reads are plain copies. Measured with OPENSHC_FUNCTION_VISIBILITY_UNITS on and both resolver sets enabled: isValidCastleSiteLocation 83.8% -> 100%, the other five unchanged (useTerrainHeightBrush stays at its 76.4% block-order wall), TileMapState average 99.28 -> 99.37. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This was referenced Dec 1, 2026
…orceinline resolver get() Each line now states its percentage together with the caveat it depends on: a volatile read or store in the source, or (for the five functions the original's jz $+N asm macros leave unoptimised) __forceinline on FunctionResolver's get(). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…racters Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Split out of #396 at review: the
Map::TileMapStatefunctions that need something this project has not needed before, so they can be looked at separately. Builds on #396, which has to be merged first; until then this PR is diffed againstreimpl/TileMapState-100-2.What is in it
16 functions in two groups.
Five compiled without global optimisation, which only match when
FunctionResolver'sget()is__forceinline:setBitFlagBasedOnWallTowerGatehouseOrKeep,setTerrain,updateGFXLayers,updateMacroLayerRelatedandupdateGfxLayer.NeighbourFlagsGlobalAsm.hpp, which is part of this PR.#pragma optimize. The original macros jump withjz $+9/jz $+12, relative to the location counter, since a macro used several times in one function cannot repeat a label. MSVC encodes such a jump as the near0F 84 rel32(a jump to a label is always the 2-byte short form, even withnear ptr) and counts it as control flow leaving the__asmblock. That raises C4740, a level-4 warning that is silent at/W3, and turns global optimisation off for the whole enclosing function.MACRO_MARK_CHANGED_NEIGHBOURS_GLOBAL(createPlateau,useLevelBrush,useTerrainHeightBrush) are optimised, and so are PathFindingState's macros, which jump to labels.MACRO_ASM_JZ_NEARwrites that jump (it used to_emitthe bytes), so the four pragmas are gone. All five functions compile to the same bytes as before.Six callers of
getTileForBrushbuilt in one unit with it:createPlateau,isValidCastleSiteLocation,setLand,useEraserBrush,useLevelBrushanduseTerrainHeightBrush, listed incmake/function-visibility.txt.tileandyout-parameters once after the call and keeps them in registers across later stores and calls. A plainint const brushTile = tile;compiles that way only whengetTileForBrush's body is in the same translation unit, as it was in the original (0x004FBD80 sits among its callers).setLandall give the same wrong function, which is what the earlier*(int volatile*)&tilereads worked around. Those reads are gone.OPENSHC_FUNCTION_VISIBILITY_UNITSon, like Reimplement UpdatePitchman and UpdateEngineer from gynt's units branch, 100% with OPENSHC_FUNCTION_VISIBILITY_UNITS on #514. With it off the six read lower than with the old volatiles (setLandfalls well below 100%).Five that still need a
volatile:raiseLand,processEntityDamageToBuildingCollateral,placeRock,processDamageToBuildingandrenderPreviewMapperWithBrush. Every volatile that a plain form matches was removed in #396's review round; the replies there give, per site, the best non-volatile score measured.getTileForBrush, but in a unit with it a plain brush copy still loses (quick_diff ratioraiseLand1.00 -> 0.65, collateral 0.90 -> 0.82), and dropping their other volatiles as well (raiseLand's in-memoryLogicLayer &= ~L_ROCKY, the collateral function'sHeightLayerre-read) only gets back to 0.81 and 0.84.updateGFXLayersandupdateGfxLayeralso readGMFirstPictureIndexthrough a volatile cast. At /Od every plain spelling tried (12) narrows that read tomov ax, word ptr, while the original loads the full dword first; the table itself is not volatile in the original, which narrows it elsewhere (0x4f8de6).determineBuildingPlacementRotationalso keeps a volatile but stays in #396, because main'supdateBuildingPlacementRotationPreviewcalls it under/GLand drops from 100% to 46.2% without it.Match
Measured with reccmp on this branch (
80f58eed5), struct and function resolvers on,OPENSHC_FUNCTION_VISIBILITY_UNITSon, with the 186 TileMapState sources pluscalculateHoveredTile.cppandxyAreValid.cppin the build list (188 functions, average 99.37%):createPlateau,getTileForBrush,isValidCastleSiteLocation,raiseLand,renderPreviewMapperWithBrush,setBitFlagBasedOnWallTowerGatehouseOrKeep,setLand,setTerrain,updateGFXLayers,updateMacroLayerRelated,useEraserBrush,useLevelBrushupdateGfxLayerprocessEntityDamageToBuildingCollateralplaceRockprocessDamageToBuildinguseTerrainHeightBrushisValidCastleSiteLocationwas 83.8% with its volatile reads; the unit takes it to 100%. Every other function in that list scores the same as on #396.src/precomp/FunctionResolver.h:get()is__forceinlineAll five
get()definitions of the function resolver change frominlineto__forceinline: the implemented, the game-function and the wrapperFunctionPtrUnifier,GameFunction, and the memberGameFunctionmacro.get()out of line. EveryMACRO_CALLthere becomescall Resolver::get+call eaxwhere the original has one directcall, which is two bytes longer per call and shifts every later jump displacement.InlCallTupleIsCandidate, 0x10b69bd0): with_Globopt_flagcleared, only callees carrying the__forceinlinebit are inline candidates. Plaininlineis not enough, and neither/Obnor#pragma inline_depthhelps. All five definitions are needed.get(). A build of all 2515 sources undersrc/OpenSHCagreed that nothing else changes; the only two reported "worse",UI::Menu::Constructor_MenuandhandleMenuItems, have unchanged bytes and differ only in how reccmp names acmp .., 0x11000000immediate.Gotos
processDamageToBuildingandprocessEntityDamageToBuildingCollateralno longer use agoto: adestroyNormallyflag, cleared in the special cases with the shared block after the switch, gives the same code.updateGfxLayerkeeps its gotos, because every goto-free form measured lost match. Its former__asm jmp wallAlpha __asm jmp wallAlphais now a plaingoto wallAlpha;with identical bytes, since the macros already turn global optimisation off.🤖 Generated with Claude Code
https://claude.ai/code/session_01XmXCmZXUzxbDcYJj2mVg9V