Skip to content
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

Improve performance of navigational functions #5861

Merged
merged 2 commits into from Jan 27, 2024

Conversation

Garanas
Copy link
Member

@Garanas Garanas commented Jan 27, 2024

Reduces the table trashing that occurs when retrieving nearby sections. The improvements applies to:

  • GetPositionsInRadius
  • DirectionsFrom
  • DirectionsFromWithThreatThreshold

@Garanas Garanas added the area: nav-mesh related to navigational mesh label Jan 27, 2024
local current = 1
local stack = { sectionOrigin }
Copy link
Member Author

@Garanas Garanas Jan 27, 2024

Choose a reason for hiding this comment

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

Instead of creating new tables for each function call we use a cache that is provided to us by the function parameters.

This is an internal function (note the 'local' keyword). Therefore we have full control over the cache.

-- if neighbor exceeds the distance then we pick the neighbor
local dx = ox - neighbor.Center[1]
local dz = oz - neighbor.Center[3]
if MathSqrt(dx * dx + dz * dz) < distance then
Copy link
Member Author

Choose a reason for hiding this comment

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

We can skip the square root by squaring the distance.

local msg = count --[[@as string]]
return nil, msg
end

-- only keep those at the edge
Copy link
Member Author

Choose a reason for hiding this comment

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

Add the possibility for the user to provide a cache to prevent table allocations.


---@type Vector[]
local points = { }
Copy link
Member Author

Choose a reason for hiding this comment

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

This should be cacheable via a parameter too, but we'll keep that for the next pull request 😃

@Garanas Garanas merged commit d2af437 into deploy/fafdevelop Jan 27, 2024
3 checks passed
@Garanas Garanas deleted the feature/nav-mesh/performance branch January 27, 2024 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: nav-mesh related to navigational mesh
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant