Avoid copying the match table for every queued then block (O(N^2) fireRules) - #12
Open
PDepaula wants to merge 1 commit into
Open
Avoid copying the match table for every queued then block (O(N^2) fireRules)#12PDepaula wants to merge 1 commit into
PDepaula wants to merge 1 commit into
Conversation
fireRules copied nodeToMatches[node] into a local for each entry of thenQueue. Table assignment is a deep copy, so a pass that runs N then blocks of the same rule copied that rule's N-entry match table N times: O(N^2) per pass. Index the table in place instead. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019VGNJYtoUzydpJzfWTW15h
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.
Problem
In
fireRules, the loop that executesthenblocks doesTableassignment is a deep copy, so each queuedthencopies the whole match table of its rule. When one pass queues Nthenblocks of the same rule (one match per entity, the usual "many enemies / particles" shape), the rule's N-entry table is copied N times: O(N²) perfireRules, with each copied entry carrying a fullMatchT.Fix
Index
nodeToMatches[node]in place (two lines). The snapshot semantics are unchanged:nodeToMatchesis still filled once before anythenruns, so athenthat mutates matches does not affect the same pass (the "non-deterministic behavior" test still passes).Evidence
Benchmark: one rule with
(id, Pos),(id, Speed),(Global, DeltaTime);thenupdatesPos. Time perfireRuleswith N entities,-d:release --gc:orc, Nim 2.2.10:benchmark source
Found while profiling a Vampire Survivors–style game on pararules (https://github.com/PDepaula/parasurvivors) where 300 enemies each have a per-entity movement rule:
fireRuleswas ~83% of the frame innimprof, all in this loop. That project currently ships this change viapatchFile; with it upstream the workaround goes away.nimble testpasses.🤖 Generated with Claude Code