Repository navigation
perf: optimize _abilities_count_and_build_progress - #253
Open
SamOgon-one wants to merge 3 commits into
Open
SamOgon-one wants to merge 3 commits into
SamOgon-one wants to merge 3 commits into
Conversation
### Optimize `_abilities_count_and_build_progress` #### Description Optimizes `_abilities_count_and_build_progress` by eliminating intermediate Python object allocations and reducing attribute lookup overhead in the main loop over `all_own_units`. #### How it optimizes & speeds up: 1. **Bypasses `UnitOrder` instantiation:** Instead of iterating over `unit.orders` (which creates a new `UnitOrder` instance and converts target coordinates to `Point2` for every single order), we read directly from `unit._proto.orders`. 2. **Fast Enum resolution:** Uses `AbilityId._value2member_map_` cached in a local variable for direct $O(1)$ dict lookups of `order.ability_id`. 3. **Loop-invariant hoisting:** Hoists `is_terran = self.race == Race.Terran` and `structure_type_ids = self.game_data.structure_type_ids` out of the unit loop. 4. **Direct protobuf field access:** Reads `proto.build_progress` and `proto.unit_type` directly to avoid property overhead where applicable. #### Benchmark / Profiling: According to `py-spy` profiling across a 10-minute Protoss game: * **Before:** `_abilities_count_and_build_progress` accounted for **3.5%** of total CPU runtime. * **After:** Reduced to **2.6%** of total CPU runtime (~26% relative reduction for this method).
Cast ability_by_id to the correct type for better type safety.
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.
Optimize
_abilities_count_and_build_progressDescription
Optimizes
_abilities_count_and_build_progressby eliminating intermediate Python object allocations and reducing attribute lookup overhead in the main loop overall_own_units.How it optimizes & speeds up:
UnitOrderinstantiation: Instead of iterating overunit.orders(which creates a newUnitOrderinstance and converts target coordinates toPoint2for every single order), we read directly fromunit._proto.orders.AbilityId._value2member_map_cached in a local variable for directorder.ability_id.is_terran = self.race == Race.Terranandstructure_type_ids = self.game_data.structure_type_idsout of the unit loop.proto.build_progressandproto.unit_typedirectly to avoid property overhead where applicable.Benchmark / Profiling:
According to
py-spyprofiling across a 10-minute Protoss game:_abilities_count_and_build_progressaccounted for 3.5% of total CPU runtime.