Skip to content

Commit c989f23

Browse files
committed
perf: Fix memory cleanup in Tie + Rogue Properties to try to address memory consumption
1 parent b023a77 commit c989f23

File tree

9 files changed

+63
-22
lines changed

9 files changed

+63
-22
lines changed

src/animations/src/Shared/AnimationTrackPlayer.lua

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ end
135135
@param fadeTime number
136136
]=]
137137
function AnimationTrackPlayer:SetWeightTargetIfNotSet(weight: number, fadeTime: number)
138-
self._maid._adjustWeight = self:_onEachTrack(function(_maid, track)
138+
self._maid._adjustWeight = self:_onEachTrack(function(track)
139139
if track.WeightTarget ~= weight then
140140
track:AdjustWeight(weight, fadeTime)
141141
end
@@ -159,7 +159,7 @@ function AnimationTrackPlayer:Play(fadeTime: number, weight: number, speed: numb
159159
end
160160

161161
self._maid._stop = nil
162-
self._maid._play = self:_onEachTrack(function(_maid, track)
162+
self._maid._play = self:_onEachTrack(function(track)
163163
track:Play(fadeTime, weight, speed)
164164
end)
165165
end
@@ -171,7 +171,7 @@ end
171171
]=]
172172
function AnimationTrackPlayer:Stop(fadeTime: number)
173173
self._maid._play = nil
174-
self._maid._stop = self:_onEachTrack(function(_maid, track)
174+
self._maid._stop = self:_onEachTrack(function(track)
175175
track:Stop(fadeTime)
176176
end)
177177
end
@@ -183,7 +183,7 @@ end
183183
@param fadeTime number
184184
]=]
185185
function AnimationTrackPlayer:AdjustWeight(weight: number, fadeTime: number)
186-
self._maid._adjustWeight = self:_onEachTrack(function(_maid, track)
186+
self._maid._adjustWeight = self:_onEachTrack(function(track)
187187
track:AdjustWeight(weight, fadeTime)
188188
end)
189189
end
@@ -195,7 +195,7 @@ end
195195
@param fadeTime number
196196
]=]
197197
function AnimationTrackPlayer:AdjustSpeed(speed: number, fadeTime: number)
198-
self._maid._adjustSpeed = self:_onEachTrack(function(_maid, track)
198+
self._maid._adjustSpeed = self:_onEachTrack(function(track)
199199
track:AdjustSpeed(speed, fadeTime)
200200
end)
201201
end
@@ -215,17 +215,11 @@ function AnimationTrackPlayer:IsPlaying(): boolean
215215
end
216216

217217
function AnimationTrackPlayer:_onEachTrack(callback)
218-
return self._currentTrack
219-
:ObserveBrio(function(track)
220-
return track ~= nil
221-
end)
222-
:Subscribe(function(brio)
223-
if brio:IsDead() then
224-
return
218+
return self._currentTrack:Observe()
219+
:Subscribe(function(track)
220+
if track ~= nil then
221+
callback(track)
225222
end
226-
227-
local track = brio:GetValue()
228-
callback(brio:ToMaid(), track)
229223
end)
230224
end
231225

src/brio/src/Shared/Brio.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function Brio.ToMaid<T...>(self: Brio<T...>): Maid.Maid
208208
local maid = Maid.new()
209209

210210
maid:GiveTask(self:GetDiedSignal():Connect(function()
211-
maid:DoCleaning()
211+
maid:Destroy()
212212
end))
213213

214214
return maid

src/performanceutils/src/Shared/PerformanceUtils.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ end
115115
function PerformanceUtils.countObject(label: string, object: any): ()
116116
PerformanceUtils.countCalls(label .. "_new", object, "new")
117117
PerformanceUtils.countCalls(label .. "_destroy", object, "Destroy")
118+
PerformanceUtils.setLabelFormat(label .. "_total", function()
119+
return tostring(PerformanceUtils.readCounter(label .. "_new") - PerformanceUtils.readCounter(label .. "_destroy"))
120+
end)
118121
end
119122

120123
function PerformanceUtils.trackObjectConstruction(object: any): () -> ()

src/rogue-properties/src/Shared/Cache/RoguePropertyCache.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
@class RoguePropertyCache
55
]=]
66

7+
local require = require(script.Parent.loader).load(script)
8+
9+
local WEAK_KV = { __mode = "kv" }
10+
711
local RoguePropertyCache = {}
812
RoguePropertyCache.ClassName = "RoguePropertyCache"
913
RoguePropertyCache.__index = RoguePropertyCache
1014

11-
function RoguePropertyCache.new()
15+
function RoguePropertyCache.new(debugName: string)
1216
local self = setmetatable({}, RoguePropertyCache)
1317

14-
self._cache = setmetatable({}, { __mode = "v" })
18+
self._debugName = debugName
19+
self._cache = setmetatable({}, WEAK_KV)
1520

1621
return self
1722
end

src/rogue-properties/src/Shared/Cache/RoguePropertyCacheService.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,27 @@ local ServiceBag = require("ServiceBag")
1414
local RoguePropertyCacheService = {}
1515
RoguePropertyCacheService.ServiceName = "RoguePropertyCacheService"
1616

17+
local WEAK_K_TABLE = { __mode = "k" }
18+
1719
function RoguePropertyCacheService:Init(serviceBag: ServiceBag.ServiceBag)
1820
assert(not self._serviceBag, "Already initialized")
1921
self._serviceBag = assert(serviceBag, "No serviceBag")
2022

21-
self._cache = setmetatable({}, { __mode = "k" })
23+
self._cache = setmetatable({}, WEAK_K_TABLE)
2224
end
2325

2426
function RoguePropertyCacheService:GetCache(roguePropertyDefinition)
2527
if not self._cache then
2628
assert(not RunService:IsRunning(), "Not in test mode")
2729
-- Test mode
28-
return RoguePropertyCache.new()
30+
return RoguePropertyCache.new(roguePropertyDefinition:GetName())
2931
end
3032

3133
if self._cache[roguePropertyDefinition] then
3234
return self._cache[roguePropertyDefinition]
3335
end
3436

35-
local cache = RoguePropertyCache.new()
37+
local cache = RoguePropertyCache.new(roguePropertyDefinition:GetName())
3638
self._cache[roguePropertyDefinition] = cache
3739
return cache
3840
end

src/tie/src/Shared/Members/Methods/TieMethodImplementation.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ function TieMethodImplementation.new(memberDefinition, parent: Instance, initial
2525

2626
self:SetImplementation(initialValue)
2727

28+
-- Since "actualSelf" can be quite large, we clean up our stuff aggressively for GC.
29+
self._maid:GiveTask(function()
30+
self._maid:DoCleaning()
31+
32+
for key, _ in pairs(self) do
33+
rawset(self, key, nil)
34+
end
35+
end)
36+
2837
return self
2938
end
3039

src/tie/src/Shared/Members/Properties/TiePropertyImplementation.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,23 @@ function TiePropertyImplementation.new(memberDefinition, folder: Folder, initial
3838

3939
self:SetImplementation(initialValue)
4040

41+
-- Since "actualSelf" can be quite large, we clean up our stuff aggressively for GC.
42+
self._maid:GiveTask(function()
43+
self._maid:DoCleaning()
44+
45+
for key, _ in pairs(self) do
46+
rawset(self, key, nil)
47+
end
48+
end)
49+
4150
return self
4251
end
4352

4453
function TiePropertyImplementation:SetImplementation(implementation)
45-
local maid = Maid.new()
4654
self._maid._current = nil
4755

56+
local maid = Maid.new()
57+
4858
-- override with default value on nil case
4959
if implementation == nil then
5060
implementation = self._memberDefinition:GetDefaultValue()

src/tie/src/Shared/Members/Signals/TieSignalImplementation.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ function TieSignalImplementation.new(memberDefinition, implParent, initialValue)
2626

2727
self:SetImplementation(initialValue)
2828

29+
-- Since "actualSelf" can be quite large, we clean up our stuff aggressively for GC.
30+
self._maid:GiveTask(function()
31+
self._maid:DoCleaning()
32+
33+
for key, _ in pairs(self) do
34+
rawset(self, key, nil)
35+
end
36+
end)
37+
2938
return self
3039
end
3140

src/tie/src/Shared/TieImplementation.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ function TieImplementation.new(
5050

5151
self._implParent.Parent = self._adornee
5252

53+
-- Since "actualSelf" can be quite large, we clean up our stuff aggressively for GC.
54+
self._maid:GiveTask(function()
55+
self._maid:DoCleaning()
56+
57+
for key, _ in pairs(self) do
58+
rawset(self, key, nil)
59+
end
60+
end)
61+
5362
return self
5463
end
5564

0 commit comments

Comments
 (0)