DebrisGobbler v1.1
DebrisGobbler is a open source, strongly-typed drop-in alternative to DebrisService and task.delay that offers several significant performance and usability improvements.
Roblox model | Github repository
Why DebrisGobbler?
- DebrisGobbler is much faster than DebrisService (2x faster) and task.delay (3x faster) on insertion.
- DebrisGobbler is magnitudes faster than DebrisService (up to 1000x faster) and task.delay on destruction.
- DebrisGobbler is persistent, meaning unlike task.delay if your script is destroyed it will not leave debris behind.
- DebrisGobbler can release destroyed instances before their expiry
- DebrisGobbler can handle unlimited instances (vs. DebrisService’s 1000) and will not destroy items before their expiration time.
Alongside these performance improvements, this module retains all of the advantages of DebrisService, such as not creating a new thread or coroutine for each new item and not holding onto destroyed items with none of the disadvantages.
Performance
Insertion
DebrisGobbler is ~3x faster than task.delay and ~2x faster than DebrisService
Comparison code
--[[
This file is for use by Benchmarker (https://boatbomber.itch.io/benchmarker)
|WARNING| THIS RUNS IN YOUR REAL ENVIRONMENT. |WARNING|
--]]
local DebrisGobbler = require(game.ReplicatedStorage.DebrisGobbler.DebrisGobbler)
DebrisGobbler:Init()
local DebrisService = game:GetService('Debris')
local DestructionTime = 7
return {
ParameterGenerator = function()
local Parts = {}
for i = 1, 1000 do
table.insert(Parts, Instance.new("Part"))
end
return Parts
end,
Functions = {
["DebrisService:AddDebris"] = function(Profiler, Parts)
for _, Part in Parts do
DebrisService:AddItem(Part, DestructionTime)
end
end,
["DebrisGobbler:AddDebris"] = function(Profiler, Parts)
for _, Part in Parts do
DebrisGobbler:AddItem(Part, DestructionTime)
end
end,
["task.delay"] = function(Profiler, Parts)
for _, Part in Parts do
task.delay(DestructionTime, Part.Destroy, Part)
end
end,
},
}
Destruction
Unfortunately destruction is much harder to calculate performance benefits for, however we can make estimations based on what has been released on the inner workings of each system.
When it comes to destruction DebrisGobbler is magnitudes faster than DebrisService. While DebrisService iterates through every item during every other frame, resulting in up to 1000 iterations per frame or 30,000 iterations per second, this module only checks the nearest item to destruction per frame. DebrisService also runs on the old scheduler (30hz) which is inconsistent and adds limitations due to it being extremely slow (1000 max items), which means items can be destroyed early.
DebrisGobbler is faster than task.delay on destruction because of optimizations that merge multiple parts to be destroyed in the same frame into a single node, whereas task.delay faces performance penalties from crossing from C to Lua for each destruction alongside managing all the other task scheduling in the game.
Usage
DebrisGobbler should be placed as a module in ReplicatedStorage
DebrisGobbler:Init() must be called on a persistent script (one that will never be destroyed)
DebrisGobbler:Init()
Adds an item to be destroyed in Time. The default is 7 seconds.
DebrisGobbler:AddItem(Item: Instance, Time: number?)
Removes an item from the queue.
DebrisGobbler:RemoveItem(Item: Instance)
License
This project is fully open-source through the MIT license.