Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/snippets/performance.7163.md
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.
69 changes: 68 additions & 1 deletion lua/sim/NavUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

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 kx is not an index (8 bytes) but part of a hash (at least 20 bytes each).

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'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value CachedNil does not explain to the caller why there is no label. The point of the last return argument is to help the caller to understand this. For example: you tried to query outside the map or the area is not pathable.

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
Expand Down