Any optimization to make this code run faster?

I have a triangle terrain editor, and I need some optimization to the function that updates the triangles after moving a node

for i,Tri in pairs(node.Parent.Parent:GetChildren()) do --search in ALL the tris
		if Tri:IsA("WedgePart") then --if that's a wedge
			local dis = (node.Position-Tri.Position).Magnitude

			if dis < Max then --if its less distanced from the node by a value continues
				local Name = node.Name
				local Nums = stringNumToTable(Tri.Name) --convert its name into a list of numbers ex.(1,2,3,4 means that tri is attached to nodes 1,2,3 or 2,3,4)

				if table.find(Nums,node.Name) then --if the triangle name contains node name...
					table.insert(Tris,Tri) --insert into a table
				end
			end

		end
		if i % 100000 == 0 then --when i is divisible by x (100000) waits
			task.wait(0.01)
		end
	end

when the terrain gets big it start lagging as hell, so I’m asking if someone knows how to optimize this function.
any help is appreciated! (if you just do the code or only explain its still good)

If each node has a unique identifier, you could reduce a small amount of overhead by swapping table.find with Nums[node.Name]

Edit: also, you might want to limit how many nodes are being generated at a time, and how many can be generated in total. The server is a computer, it can only hold, show, and do so much at once lmao

It’s not really a good idea to have terrain generate outward to cover a larger circular area without proper management. It’s much easier on the server to create it in ordered lines working toward a given direction.

The bottleneck in your code seems to be stringNumToTable(Tri.Name). You’re making hundreds of thousands of tables from strings which use a lot of memory and all have to be garbage collected. You shouldn’t be using the name to store data.

well, just realized I should just get the colliding parts (and not the entire map) of that node can save lot of memory (maybe I can do a temporary node hitbox). I’ll see if this works (if this actually works then I’m pretty dumb)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.