Make Units set operations linear instead of quadratic - #246
Open
JustinAWei wants to merge 1 commit into
Open
JustinAWei wants to merge 1 commit into
JustinAWei wants to merge 1 commit into
Conversation
Units.__or__, __add__, __and__ and __sub__ checked each unit's tag against a generator over the other collection, re-scanning it for every unit: O(n*m). Collect the tags into a set once instead. Same units, same order. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
JustinAWei
marked this pull request as ready for review
October 1, 2026 18:10
raspersc2
added a commit
to august-k/python-sc2
that referenced
this pull request
Oct 2, 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.
Units.__or__,__add__,__and__and__sub__check each unit's tag against a generator over the other collection:That rescans the whole collection for every unit, so the cost is O(n·m). This PR collects the tags into a set once, so each check is O(1). The result is the same: same units, same order (self first, then the new units from other).
Why it matters
ares-sc2 merges its remembered (snapshot) units into the live unit lists every step:
Against a creep-heavy Zerg,
all_enemy_unitsreaches several thousand entries (every creep tumor seen). In a profile of our bot against such an opponent, these merges were the largest single cost in ares' per-step update, about 2 ms per call. Together with other per-step work, that pushed the average step over the AI Arena 40 ms limit.Benchmark
A plain CPython micro-benchmark of the
+logic, run in this repo's uv env:Tests
test_unitsgains assertions on all four operators that pin down deduplication and order.uv run pytest test/test_pickled_data.py test/test_expiring_dict.py: 19 passed.ruff checkandruff format --checkare clean.🤖 Generated with Claude Code