Hey, soo recently i made this module that i called Cluster, it’s global table like structure that allows me to create segments, pretty much simple idea, but there is a problem
local Cluster = {}
local Segments = {}
--/ Private functions
local function doSegmentExist(SegmentName: string): {}?
return Segments[SegmentName]
end
--/ Segments management
function Cluster:createSegment(Name: string)
if doSegmentExist(Name) then return end
Segments[Name] = {}
end
function Cluster:deleteSegment(Name: string)
local Segment = doSegmentExist(Name)
if not Segment then return end
table.clear(Segment)
Segments[Name] = nil
end
--/ Segments data management
function Cluster:addToSegment(SegmentName: string, Index: any, Value: any)
local Segment = doSegmentExist(SegmentName)
if not Segment then return end
Segment[Index] = Value
end
function Cluster:removeFromSegment(SegmentName: string, Index: any)
local Segment = doSegmentExist(SegmentName)
if not Segment then return end
Segment[Index] = nil
end
function Cluster:insertToSegment(SegmentName: string, Value: any)
local Segment = doSegmentExist(SegmentName)
if not Segment then return end
table.insert(Segment, Value)
end
function Cluster:retrieve(SegmentName, Index: any): any?
local Segment = doSegmentExist(SegmentName)
if not Segment then return end
return Segment[Index]
end
return Cluster
Module’s code ^
Let’s say we have code below
local mod = require(script.Parent)
mod:createSegment("A")
print("a")
task.wait(8)
print("b")
for i = 1, 100 do
mod:addToSegment("A", i, i * 2)
end
task.wait(1)
for i = 1, 100 do
mod:removeFromSegment("A", i)
end
print("c")
Now after deletion, Luau Heap shows that cluster’s A upvalue (which is according to Roblox reference of something outside the script) grew, which means it wasn’t removed, but the thing is that when i remove entire Segment it magically goes back to normal, i tried everything, but nothing have worked to fix this, any ideas?
Note: task.wait(8) is here to make Luau Heap metrics
Note: I tested it with more than 100 iterations, and number grew, for 1000 it was 16k bytes
Note: Last note, i’ve used task.wait(0) to give GC some time, and it didn’t fixed problem too
EDIT: I also forget to mention it only happens when there are two loops, if i remove something from segment when it’s in same loop, it will also solve the problem, which seems strange to me