--!strict local RunService = game:GetService("RunService") local Stats = game:GetService("Stats") local Workspace = game:GetService("Workspace") local Lighting = game:GetService("Lighting") local ReplicatedStorage = game:GetService("ReplicatedStorage") export type TroveTask = RBXScriptConnection | Instance | () -> () | thread export type EngineConfig = { TargetFramerate: number, SamplingWindow: number, AdaptiveThrottling: boolean, AggressiveLOD: boolean, } local DEFAULT_CONFIG: EngineConfig = { TargetFramerate = 60, SamplingWindow = 0.5, AdaptiveThrottling = true, AggressiveLOD = true, } local Trove = {} Trove.__index = Trove function Trove.new() return setmetatable({ _tasks = {} :: { TroveTask } }, Trove) end function Trove:Add(task: T & TroveTask): T table.insert(self._tasks, task) return task end function Trove:Clean() for _, task in ipairs(self._tasks) do if typeof(task) == "RBXScriptConnection" then task:Disconnect() elseif typeof(task) == "Instance" then task:Destroy() elseif typeof(task) == "function" then task() elseif typeof(task) == "thread" then task.cancel(task) end end table.clear(self._tasks) end local ObjectPool = {} ObjectPool.__index = ObjectPool function ObjectPool.new(template: Instance, prewarmSize: number) local self = setmetatable({}, ObjectPool) self._template = template self._pool = {} :: { Instance } self._container = Instance.new("Folder") self._container.Name = "_Pool_" .. template.Name self._container.Parent = ReplicatedStorage for _ = 1, prewarmSize do local obj = template:Clone() obj.Parent = self._container table.insert(self._pool, obj) end return self end function ObjectPool:Acquire(): Instance local obj: Instance if #self._pool > 0 then obj = table.remove(self._pool) :: Instance else obj = self._template:Clone() end return obj end function ObjectPool:Release(obj: Instance) obj.Parent = self._container table.insert(self._pool, obj) end function ObjectPool:Destroy() self._container:Destroy() table.clear(self._pool) end local OptimizationEngine = {} OptimizationEngine.__index = OptimizationEngine function OptimizationEngine.new(config: EngineConfig?) local self = setmetatable({}, OptimizationEngine) self.Config = config or DEFAULT_CONFIG self.Trove = Trove.new() self.RemoteRegistry = setmetatable({}, { __mode = "v" }) :: { [Instance]: number } self.PartCache = setmetatable({}, { __mode = "k" }) :: { [BasePart]: boolean } self.CurrentFramerate = 60 self.IsThrottled = false self.IsActive = false return self end function OptimizationEngine:ApplyEngineOverrides() pcall(function() local settingsObj = settings() local physics = settingsObj:GetService("Physics") if physics and Enum.EnviromentalPhysicsThrottle then physics.PhysicsEnvironmentalThrottle = Enum.EnviromentalPhysicsThrottle.DefaultAuto end local network = settingsObj:GetService("Network") if network then network.IncomingReplicationLag = 0 end end) pcall(function() if Enum.InterpolationThrottlingMode then Workspace.InterpolationThrottling = Enum.InterpolationThrottlingMode.Enabled end if Enum.AnimatorRetargetingMode then Workspace.Retargeting = Enum.AnimatorRetargetingMode.Disabled end end) end function OptimizationEngine:InitRemoteObserver() local roots = { ReplicatedStorage, Workspace } local function register(instance: Instance) if instance:IsA("RemoteEvent") or instance:IsA("RemoteFunction") then self.RemoteRegistry[instance] = os.clock() elseif self.Config.AggressiveLOD and instance:IsA("BasePart") then self.PartCache[instance] = true end end task.defer(function() local yieldThreshold = 250 local count = 0 for _, root in ipairs(roots) do for _, child in ipairs(root:GetDescendants()) do register(child) count += 1 if count % yieldThreshold == 0 then task.wait() end end end end) for _, root in ipairs(roots) do self.Trove:Add(root.DescendantAdded:Connect(register)) self.Trove:Add(root.DescendantRemoving:Connect(function(child) if self.RemoteRegistry[child] then self.RemoteRegistry[child] = nil end if self.PartCache[child :: BasePart] then self.PartCache[child :: BasePart] = nil end end)) end end function OptimizationEngine:InitAdaptiveMonitor() local lastCheck = os.clock() local frameCount = 0 local targetFrametime = 1 / self.Config.TargetFramerate self.Trove:Add(RunService.Heartbeat:Connect(function(dt: number) frameCount += 1 local now = os.clock() if now - lastCheck >= self.Config.SamplingWindow then self.CurrentFramerate = frameCount / (now - lastCheck) frameCount = 0 lastCheck = now if self.Config.AdaptiveThrottling then self:AdjustFidelity(dt > (targetFrametime * 1.25)) end end end)) end function OptimizationEngine:AdjustFidelity(shouldThrottle: boolean) if shouldThrottle == self.IsThrottled then return end self.IsThrottled = shouldThrottle if shouldThrottle then Lighting.GlobalShadows = false for part, _ in pairs(self.PartCache) do if part.Parent and part.CastShadow then part.CastShadow = false end end else Lighting.GlobalShadows = true end end function OptimizationEngine:GetMemoryReport(): { TotalMB: number, LuauHeapKB: number, ActiveRemotes: number } return { TotalMB = Stats:GetTotalMemoryUsageMb(), LuauHeapKB = gcinfo(), ActiveRemotes = self:GetTrackedRemoteCount(), } end function OptimizationEngine:GetTrackedRemoteCount(): number local count = 0 for remote, _ in pairs(self.RemoteRegistry) do if remote.Parent ~= nil then count += 1 end end return count end function OptimizationEngine:Start() if self.IsActive then return end self.IsActive = true self:ApplyEngineOverrides() self:InitRemoteObserver() self:InitAdaptiveMonitor() end function OptimizationEngine:Stop() self.IsActive = false self.Trove:Clean() table.clear(self.RemoteRegistry) table.clear(self.PartCache) end local System = OptimizationEngine.new({ TargetFramerate = 60, SamplingWindow = 1.0, AdaptiveThrottling = true, AggressiveLOD = true, }) System:Start() return { System = System, ObjectPool = ObjectPool, Trove = Trove, }