mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #8036] [Feature]: Build ruleset live-state schemas once and cache the live-less sheet evaluation per combat command #7533
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#7533
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Promansis on GitHub (Oct 22, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/8036
Originally assigned to: @Promansis on GitHub.
What problem does this solve?
A ruleset fight re-evaluates the same character sheets hundreds of times per combat command.
scripts/regressions/ruleset-combat-director.regression.tsis the slowest Node regression (123 s solo, 131–300 s inside the 4-worker CI pool, one pool timeout at 300 s in five observed runs), and a CPU profile of it shows that the time is in the engine, not the test:evaluateRulesetSheetand helpers inpackages/shared/src/features/rulesets/sheet-math.tspackages/shared/src/features/rulesets/live-state.ts(listRulesetLivePools,resolveLive, wound work)ZodString/ZodTypeinsidereadStoredLiveStateAsIsTwo causes:
readStoredLiveStateAsIsbuildsz.string().max(...)twice on every call (keepValid(source.text, z.string().max(MAX_LIVE_TEXT_LENGTH), …)and the same forstates). It runs for every combatant on every read of a stored live state.resolveLive→listRulesetLivePools→evaluateRulesetSheet(definition, build)(no live values) is called from the option enumeration and picker loops. Counted with an instrumented build: the regression makes 9.7 million live-less evaluations for only 13,085 distinct(definition, build)pairs. One evaluation costs about 4 µs; the volume is the cost.The same code runs in the live server on the combat-director route, skill-check resolution, and the inventory and market services, so each ruleset combat turn pays this too.
Proposed solution
Two commits in one PR, the first mergeable on its own.
Commit 1 (near zero risk): build the two schemas once. In
live-state.ts, next toliveNumber:and in
readStoredLiveStateAsIsusekeepValid(source.text, liveTextValue, MAX_LIVE_TEXTS)andkeepValid(source.states, liveStateValue, MAX_LIVE_STATES). Same schema, same validation, four lines. Measured: the regression drops from 123 s to 102 s solo.Commit 2 (medium risk, so scoped and proven): cache the live-less sheet evaluation for the duration of one director command.
sheet-math.ts, keep the existing function body asevaluateRulesetSheetUncachedand exportevaluateRulesetSheetas a wrapper that, only while a scope is open, memoizes thelive === undefinedcase in aWeakMap<RulesetDefinition, WeakMap<RulesetSheetBuild, EvaluatedRulesetSheet>>. Calls with live values are never cached. ExportwithRulesetSheetEvaluationCache<T>(run: () => T): Tthat opens a fresh map, runs, and always clears it infinally(nested calls reuse the open scope).commandRulesetCombatDirector(packages/server/src/services/game/ruleset-combat-director.service.ts) around its body. Builds are not edited during a command (fight creation, sheet import and the creature-sheet hold run outside it), which is what makes an identity-keyed cache sound there. Outside a scope the function behaves exactly as today.packages/shared/src/features/ruleset-combat/hold.ts(health clamp, near line 525) the code clonesproposed, evaluates the clone throughresolveRulesetValueRef(definition, sheet, { field: field.id }), then writessheet.fields[field.id] = …and evaluates it again. Readcurrentfromproposed(identical before the write) so the clone is only evaluated after its edit. With a globally enabled cache this is the only failure in the 269ruleset-combat-*andgame-ruleset-*regressions (game-ruleset-combat-creature-sheetsreported "is now 200" instead of "is now 67").hold.tslines ~206, 260, 323, 472;sheet-builder.ts~1402, 1408, 2078–2089;sheet-math.ts~151;live-state.ts~1043. All are clone-then-edit builders and all passed the probe, but the rule "evaluate a build only after its last write" should be stated in a comment on the cache.scripts/regressions/ruleset-sheet-evaluation-cache.regression.ts: (a) insidewithRulesetSheetEvaluationCache, evaluating the same(definition, build)twice returns the same object and a third call with live values does not; (b) outside the scope two calls return distinct objects; (c) thehold.tshealth clamp, run inside the scope on a creature whose health is outside its tier, still reports the clamped maximum; (d) a nested scope does not clear the outer one. Keep the file deterministic and under a second.Object.freezerecursively) so any aliasing write throws. The probe run of this found no frozen-object writes.Measured with both commits applied: the regression runs in 60 s solo (from 123 s), with 9.7 million evaluations collapsing to 13,085 misses. Outputs are byte-identical; the only way the cache can change behaviour is the clone-then-edit pattern above, which the scope, the fix and the regression close.
Alternatives considered
listRulesetLivePoolsand the option loops: correct and explicit, but touches dozens of call sites inencounter.ts,options.tsandresolve.tsfor the same result.Additional context
stagingat4d67fe75b, Node 24.15. The regression's own lane effect: shard 1 ofcomplete-node-regressionsstops being the longest shard and the 300 s pool timeout (seen in run 39664968247, job 119474763718) cannot recur.evaluated.*finds none inpackages/), and the client already memoizes sheet evaluation per render (GameRulesetSheet.tsx), so no client change is needed.Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked: