Pathfinding Service Slows Down With Every Call

I already did, it wasn’t a memory leak at all.

1 Like

Hey, I couldn’t repro it.

First of all, the following wouldn’t work (ComputeAsync doesn’t return anything, it changes the state of the variable Path):

Secondly, here’s my scripts:

local PathfindingService = game:GetService("PathfindingService")

local paths = {}

local path = PathfindingService:CreatePath({
	AgentRadius = 3,
	AgentHeight = 6,
	AgentCanJump = false
})

function MakePath(Start, End)
	local s = os.clock()
	path:ComputeAsync(Start, End)
	local time = os.clock() - s
	
	table.insert(paths, path:GetWaypoints())
	
	return time
end

for i = 1,100 do
	print(MakePath(workspace.Start.Position, workspace.End.Position))
end

I tested it on two template levels but didn’t find anything suspicious. Here’s the output for Castle (you would need to place two Parts named “Start” and “End”):

  16:49:04.838  1.1574963000003  -  Server - Script:22
  16:49:04.919  0.079941699999836  -  Server - Script:22
  16:49:05.091  0.17215149999993  -  Server - Script:22
  16:49:05.235  0.14271570000074  -  Server - Script:22
  16:49:05.319  0.082724900000358  -  Server - Script:22
  16:49:05.434  0.11421020000034  -  Server - Script:22
  16:49:05.538  0.10342310000033  -  Server - Script:22
  16:49:05.633  0.094279400000232  -  Server - Script:22
  16:49:05.728  0.094616700000188  -  Server - Script:22
  16:49:05.827  0.097652700000253  -  Server - Script:22
  16:49:05.928  0.10022070000014  -  Server - Script:22
  16:49:06.044  0.11599639999986  -  Server - Script:22
  16:49:06.125  0.080301300000428  -  Server - Script:22
  16:49:06.227  0.10055290000037  -  Server - Script:22
  16:49:06.326  0.098765299999286  -  Server - Script:22
  16:49:06.426  0.099504099999649  -  Server - Script:22
  16:49:06.541  0.11422399999992  -  Server - Script:22
  16:49:06.643  0.10075869999946  -  Server - Script:22
  16:49:06.743  0.099913500000184  -  Server - Script:22
  16:49:06.826  0.082501799999591  -  Server - Script:22
  16:49:06.941  0.11376159999963  -  Server - Script:22
  16:49:07.041  0.099503400000685  -  Server - Script:22
  16:49:07.126  0.083963299999596  -  Server - Script:22
  16:49:07.241  0.11453919999985  -  Server - Script:22
  16:49:07.342  0.10023459999957  -  Server - Script:22
  16:49:07.443  0.099751400000059  -  Server - Script:22
  16:49:07.541  0.097359800000049  -  Server - Script:22
  16:49:07.641  0.099377000000459  -  Server - Script:22
  16:49:07.742  0.10010590000002  -  Server - Script:22
  16:49:07.841  0.098511000000144  -  Server - Script:22
  16:49:07.942  0.10057799999959  -  Server - Script:22
  16:49:08.041  0.097827999999936  -  Server - Script:22
  16:49:08.144  0.10186379999959  -  Server - Script:22
  16:49:08.224  0.079511500000081  -  Server - Script:22
  16:49:08.340  0.11586690000058  -  Server - Script:22
  16:49:08.426  0.08499589999974  -  Server - Script:22
  16:49:08.542  0.11505239999951  -  Server - Script:22
  16:49:08.642  0.099150999999438  -  Server - Script:22
  16:49:08.741  0.098264600000221  -  Server - Script:22
  16:49:08.841  0.099341400000412  -  Server - Script:22
  16:49:08.940  0.098513099999764  -  Server - Script:22
  16:49:09.042  0.10157650000019  -  Server - Script:22
  16:49:09.141  0.097457400000167  -  Server - Script:22
  16:49:09.242  0.10095410000031  -  Server - Script:22
  16:49:09.340  0.097071600000163  -  Server - Script:22
  16:49:09.440  0.099456500000088  -  Server - Script:22
  16:49:09.542  0.10058310000022  -  Server - Script:22

and so on.

2 Likes

Thanks for trying to help, yeah sorry I was pseudo coding my situation so it’s a little off, my bad.

Your code is close to what I have on my end, except you’re calling MakePath with the same positions every time, so I’m sure it’s just returning the same or similar paths every time. I’d imagine there’s some internal optimization there, but on my end I’m calling it with different start and end points every time.

1 Like

The first number (over 1 ms) is probably due to navmesh creation.

Like you, I keep references to the used path waypoints (in the table), so that it can’t reuse the same memory even theoretically.

Unless you have objections, let me consider the case closed.