Instance.old (recycle instances)

Instance.old is basically what it sounds like. It’s like Instance.new, except instead of destroying instances, it recycles them.

It was designed to mimic Instance.new, except with a few more features, and potentially better performance for certain use cases. Here’s an example script:

Instance = require(script.Instance_custom)

Parts = {}

for i=1,5 do
	Parts[i] = Instance.old("Part",workspace)
	Parts[i].Color = Color3.new(1,0,0)
end

Parts[1]:Destroy()
Parts[2]:Destroy()
Parts[3]:Destroy()

local RecycledPart = Instance.old("Part")
RecycledPart.Color = Color3.new(0,1,0)
RecycledPart.Parent = workspace

local AnotherRecycledPart = Instance.old("Part",{
	Parent = workspace,
	Color = Color3.new(0,0,1)
})

As you can see, it also offers something similar to what LoadLibrary(“RbxUtility”).Create used to do. This is also pretty optimized because it sets the Parent property last.

When you call “Destroy” on Instances created with Instance.old, the module disconnects any event connections associated with the instance, then adds it to a recycling bin folder. When Instance.old is called to get a new Instance, the module looks through the recycling bin to find the requested class type. If it can’t find that class type, it creates a new one. In theory, this should improve performance because Roblox won’t have to create entire new Instances all the time. It’ll be re-using old ones.

Things to keep in mind:

  • Objects made with Instance.old have events disconnected when destroyed, but other properties are not guaranteed to be set to the default when first called upon! Make sure you are explicit with any properties you want.
  • Objects created with Instance.old are basically never deleted, so if they’re all being used at once it’ll keep making new ones and keep all of them, even after they’re potentially not being used.
  • This hasn’t actually been benchmarked yet.

Clickable Links/Buttons:

Untitled2

Untitled ← this is particularly cool imo

Untitled

I made this for fun. I have yet to do benchmarks because I’m lazy. I might post a benchmark later. If you want to do a benchmark yourself, I’d appreciate it if you’d post the results here!
Drop constructive criticisms below if you have any.

6 Likes

How much performance are we actually saving? I like the idea but I feel like it’s saving a super small and insignificant amount of performance.

Edit: While I didn’t use OP’s creation, I did create my own cache for generating random instances.

I found that generating ~200 instances without a cache took about 0.05 seconds, whereas a cache only took ~0.01 seconds.

I suggest not using a community resource cache though, as they can sometimes be hard to read (as you didn’t write them) or they’re unoptimized. Creating your own cache system takes no longer than 5 minutes and it can be optimized extremely well.

If you choose not to cache your reusable instances (ex. bullet trails) you’re blatantly deciding that you want the Roblox engine to perform worse. Using a cache means more resources and performance is saved for other costly functions.

3 Likes