rojo/plugin/src/ChangeBatcher/encodeService.lua
Joe a823db0297 Refactor encoding logic in ChangeBatcher and improve matching depth handling
- Simplified the `isReadable` condition in `encodeService` for better readability.
- Updated the `ServiceChunk` structure to conditionally include properties and references only if they exist, enhancing data efficiency.
- Introduced a `MAX_SCORING_DEPTH` constant in the matching module to limit recursion depth, improving performance and preventing potential stack overflow issues.
- Refactored the `computePairCost` and `matchChildren` functions to incorporate the new depth parameter, ensuring accurate cost calculations during matching.

These changes enhance the clarity and efficiency of the encoding and matching processes, contributing to overall performance improvements.
2026-02-18 08:11:00 -06:00

79 lines
2.1 KiB
Lua

local SerializationService = game:GetService("SerializationService")
local Packages = script.Parent.Parent.Parent.Packages
local Log = require(Packages.Log)
local RbxDom = require(Packages.RbxDom)
local encodeProperty = require(script.Parent.encodeProperty)
local UNENCODABLE_DATA_TYPES = require(script.Parent.propertyFilter)
local SKIP_PROPERTIES = {
Parent = true,
Name = true,
Archivable = true,
}
return function(service: Instance)
local properties = {}
local refs = {}
local classDescriptor = RbxDom.findClassDescriptor(service.ClassName)
if classDescriptor then
for propertyName, propertyMeta in pairs(classDescriptor.properties) do
if SKIP_PROPERTIES[propertyName] then
continue
end
local isReadable = propertyMeta.scriptability == "ReadWrite" or propertyMeta.scriptability == "Read"
local doesSerialize = propertyMeta.serialization ~= "DoesNotSerialize"
if isReadable and doesSerialize then
local descriptor = RbxDom.findCanonicalPropertyDescriptor(service.ClassName, propertyName)
if descriptor then
if UNENCODABLE_DATA_TYPES[descriptor.dataType] then
continue
end
if descriptor.dataType == "Ref" then
local readOk, target = descriptor:read(service)
if readOk and target and target.Parent == service then
refs[propertyName] = {
name = target.Name,
className = target.ClassName,
}
end
continue
end
local encodeOk, encoded = encodeProperty(service, propertyName, descriptor)
if encodeOk and encoded ~= nil then
properties[propertyName] = encoded
end
end
end
end
end
local children = service:GetChildren()
local data = buffer.create(0)
if #children > 0 then
local serializeOk, result = pcall(SerializationService.SerializeInstancesAsync, SerializationService, children)
if serializeOk then
data = result
else
Log.warn("Failed to serialize children of {}: {}", service.ClassName, tostring(result))
end
end
local chunk = {
className = service.ClassName,
data = data,
}
if next(properties) then
chunk.properties = properties
end
if next(refs) then
chunk.refs = refs
end
return chunk
end