-
Notifications
You must be signed in to change notification settings - Fork 258
Memoize GetLabel for ~1.5% sim speedup at CPU-bound endgame #7163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
narfman0
wants to merge
2
commits into
FAForever:develop
Choose a base branch
from
narfman0:perf/navutils-getlabel-memoize
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−1
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - (#7163) Memoize `NavUtils.GetLabel` to avoid repeated navmesh quad-tree descents. Measured at ~1.5% of the sim tick at CPU-bound endgame (~2.5k units, 4v4), with a 98.6% cache hit rate. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -966,7 +966,7 @@ end | |
| ---@see GetTerrainLabel | ||
| ---@param layer NavLayers | ||
| ---@param position Vector | ||
| ---@return number? | ||
| ---@return number? | ||
| ---@return ('NotGenerated' | 'InvalidLayer' | 'OutsideMap' | 'SystemError' | 'Unpathable')? | ||
| function GetLabel(layer, position) | ||
| -- check if generated | ||
|
|
@@ -997,6 +997,73 @@ function GetLabel(layer, position) | |
| return leaf.Label, nil | ||
| end | ||
|
|
||
| -- Memoized wrapper over GetLabel. | ||
| -- | ||
| -- The FAF navmesh is generated once from static terrain (surface height + water depth; | ||
| -- no units or buildings). So (layer, position) -> label is constant for the whole | ||
| -- session. GetLabel re-descends the quad-tree on every call: M28AI alone issues | ||
| -- ~558 calls/tick at ~1k units, growing with unit count, costing ~1.5% of the | ||
| -- sim tick at CPU-bound endgame (~2.5k units, 4v4). Caching the result per | ||
| -- integer ogrid cell eliminates the repeated descents. | ||
| -- | ||
| -- Key: floor(x), floor(z). The quad-tree bottoms out at an integer-aligned | ||
| -- compressionThreshold grid, so a floored cell falls inside exactly one leaf and | ||
| -- has exactly one permanent label. Only PERMANENT labels are cached — 'NotGenerated' | ||
| -- and 'SystemError' are transient and must not stick. | ||
| -- | ||
| -- Memory: bounded by queried map area × number of layers; ~10^5 entries (~few MB) | ||
| -- for a full 4v4 Seton's game. Deterministic: terrain labels are static, so the cache | ||
| -- cannot cause desync. | ||
| do | ||
| local _GetLabel = GetLabel | ||
| local _floor = math.floor | ||
| -- [layer][kx][kz] = label (number) | false (permanent nil: Unpathable/OutsideMap) | ||
| local _cache = {} | ||
|
|
||
| GetLabel = function(layer, position) | ||
| local px, pz = position[1], position[3] | ||
| -- Guard malformed positions that the base function short-circuits before touching. | ||
| if px == nil or pz == nil then | ||
| return _GetLabel(layer, position) | ||
| end | ||
|
|
||
| local lc = _cache[layer] | ||
| if not lc then | ||
| lc = {} | ||
| _cache[layer] = lc | ||
| end | ||
|
|
||
| local kx = _floor(px) | ||
| local row = lc[kx] | ||
| if not row then | ||
| row = {} | ||
| lc[kx] = row | ||
| end | ||
|
|
||
| local kz = _floor(pz) | ||
| local entry = row[kz] | ||
| if entry ~= nil then | ||
| -- cache hit: false encodes a permanent nil result | ||
| if entry == false then | ||
| return nil, 'CachedNil' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return value |
||
| end | ||
| return entry | ||
| end | ||
|
|
||
| -- cache miss: call base and populate | ||
| local label, msg = _GetLabel(layer, position) | ||
| if label ~= nil then | ||
| row[kz] = label | ||
| elseif msg == 'OutsideMap' or msg == 'Unpathable' or msg == 'InvalidLayer' then | ||
| -- permanent: safe to cache as false | ||
| row[kz] = false | ||
| end | ||
| -- 'NotGenerated' / 'SystemError': transient, do not cache | ||
|
|
||
| return label, msg | ||
| end | ||
| end | ||
|
|
||
| --- Returns a table with all labels in the current iMAP cell. The keys represent the labels. The values represent the ratio of area it occupies in the cell | ||
| ---@param layer NavLayers | ||
| ---@param gx number | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inserting is essentially arbitrary, which means that
kxis not an index (8 bytes) but part of a hash (at least 20 bytes each).