Part Pooling - Increase performance with many parts

Part Pooling Version: Stable


(This topic was separated from the suggestion of a Community Sage)

I am releasing two modules that are mostly complete and generally only for more advanced scripters. (The other modules are on the bottom of this post)

Part Pooling

The first, more complete module is called Part Pooling. This is a technique used more commonly in quantity-intensive games, such as bullet hells, example:


Games that use pooling can use many objects because they do not suffer from performance loss created from instancing or destroying. Part Pooling works the same by creating a bunch of parts built from a ‘template’ part and putting them in ReplicatedStorage (or a location of your choosing) until you need them.

Pooling Usage Example Code
local Pooling = require(PoolingSystem)

---Let's create a template part so the pooling script can duplicate it
local TemplatePart = Instance.new("Part")

---We tell the pooling script to create 50 copies of the template part
local Objects = Pooling:Create(50, TemplatePart)

---Get the part from our pooling list
local Part = Pooling:GetObject(Objects)
Part.CFrame = CFrame.new(0, 5, 0)
Part.Parent = workspace

wait(5)

---"Destroy" means putting back the part into our pooling list.
Pooling:Destroy(Objects, Part)

wait(5)

---This will actually destroy all of our pooled objects. Only use if you don't need it anymore.
Pooling:Clear(Objects)

Parts aren’t actually removed in a pooling system, they are put back in the “queue” until the next time they need to be used. This ensures that performance will not suffer since we are reusing the parts.

https://gfycat.com/NearDistortedFinch

You can get the Pooling System here. Alternatively, you can download the place file here containing both my pooling system and IK w/ creature. Scripts are in workspace.
PoolingAndIK.rbxl (27.5 KB)


My other modules:
Raycast Hitbox Module
Inverse Kinematics and Procedural Animations

89 Likes

Does the part pool automatically create the part if there isn’t any left? I want to use it for my real-time triangle terrain but don’t have a good way of selecting how many to create in the pool (it depends on a lot of factors).

2 Likes

Your PartPool is nice, but PartCache is a better alternative.
Your pool has constant reparenting which causes extra physics steps, whereas this alternative module simply CFrames the object far away when not in use. It’s far faster.

Also, it has a parameter for initial pool size, like @grilme99 asked for, and can dynamically expand pool size when required.

This isn’t to say yours is bad, I’m just here to make sure people know they could be using something even faster if necessary.

20 Likes

Thanks for the heads up. I realized PartCache was a thing afterwards. Don’t want to directly copy his findings but this code can easily be done with the same techniques if anybody wants to use the same way PartCache does it.

@grilme99

It unfortunately does not create more parts automatically. If you need more parts in the pool, you can simply make new parts and Destroy them into the new pool.

local Part = MyOldTrianglePart

for i = 1, 100 do ---Make 100 more parts
     Pooling:Destroy(Objects, Part:Clone())
end
Pooling:Destroy(Objects, Part) --- making sure we don't leave the straggler
4 Likes