mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-08-14 05:32:24 +00:00
### Summary When two or more sibling instances share the same `Name` and `ClassName`, Rojo's reconciler previously paired them with their server-side counterparts purely by child order (first-unvisited match in `GetChildren()` order). If the workspace child order ever diverged from the server's, the wrong instance got paired so each duplicate could inherit a sibling's properties. This is the root of the #1257 bug: the welded parts would oscillate between positions on each connect/disconnect because hydration kept mis-pairing them. (#1265 stopped the sync fallback from scrambling child order in the first place but this PR makes hydration robust even when order *does* diverge.) This PR makes `hydrate` break ties by comparing properties: when several existing children match on `Name`+`ClassName`, it scores each candidate by how many of the virtual instance's properties match the candidate's live values, and picks the best. Order remains the tiebreak when scores are equal, so behavior is unchanged for uniquely-named instances and for indistinguishable siblings. ### Changes - `trueEquals.lua`: extracted verbatim from `diff.lua` (the fuzzy value-equality helper) so it can be shared. No behavior change; `diff.lua` now requires it. - `countMatchingProperties.lua`: added `countMatchingProperties(instance, virtualInstance, instanceMap) -> number`. Skips `Ref` properties (the instanceMap isn't fully built mid-hydrate, so refs can't be decoded reliably, and they're a poor disambiguator anyway) and any property that can't be read or decoded. - `hydrate.lua`: See details below. ### Hydrate Changes This touches `hydrate`, which runs over the whole tree on every connect/resync, so I want state clearly that **the common path is faster than before, not slower** even for parents with thousands of children! The old algorithm was a nested scan: for each of `V` virtual children, scan existing children until the first unvisited `Name`+`ClassName` match. Two costs stand out: - A `pcall` (to guard DataModel permission errors) ran on every comparison (up to `V*E` `pcall`s per parent). - Even for in-order trees the re-scanning of the visited prefix made it `O(V^2)`. The new algorithm does a single bucketing pass, then `O(1)` lookups: 1. One `O(E)` pass groups existing children into nested `buckets[name][className]` tables. This runs exactly `E` `pcall`s total (one per child), down from the `V*E` worst case. 2. Each virtual child does an `O(1)` bucket lookup to find its candidates. 3. A per-bucket cursor skips already-paired children, so order-based matching is amortized `O(1)` per child instead of rescanning. | Scenario | Old | New | | ------------------------------------------------ | --------------------------------------- | --------------------------------------------- | | Unique-named children (typical, incl. thousands) | `O(V^2)`, plus up to `V*E` pcalls | `O(V + E)`, plus exactly `E` pcalls | | `C <= 32` candidates | `O(C^2)` | `O(P * C^2)` scoring | | `C > 32` candidates | `O(C^2)` | `O(C)` | Property scoring (`getProperty`/`decodeValue`/`trueEquals`) is the only new expense, and it's gated two ways: - It runs only when a `Name`+`ClassName` group has >=2 candidates (i.e. never for uniquely-named instances). - A cap, `MAX_CANDIDATES_TO_SCORE = 32`, means scoring only kicks in once a group has <=32 unvisited candidates. A folder of thousands of identically-named parts therefore does not trigger scoring; it falls back to the original order-based pairing. The worst-case added scoring work is bounded to roughly `32^2` property comparisons per group, independent of group size. So overall this is faster when you have unique names or many children. It is slower but more robust when you have small groups of duplicate names. Memory usage is increased as it creates the candidate buckets.
101 lines
3.1 KiB
Lua
101 lines
3.1 KiB
Lua
--[[
|
|
Fuzzy value-equality used to compare a decoded virtual property value against
|
|
the live value read from a real instance. Shared by `diff` (to decide whether
|
|
a property changed) and `hydrate` (to score candidate instances).
|
|
]]
|
|
|
|
local function fuzzyEq(a: number, b: number, epsilon: number): boolean
|
|
return math.abs(a - b) < epsilon
|
|
end
|
|
|
|
local function trueEquals(a, b): boolean
|
|
-- Exit early for simple equality values
|
|
if a == b then
|
|
return true
|
|
end
|
|
|
|
-- Treat nil and { Ref = "000...0" } as equal
|
|
if
|
|
(a == nil and type(b) == "table" and b.Ref == "00000000000000000000000000000000")
|
|
or (b == nil and type(a) == "table" and a.Ref == "00000000000000000000000000000000")
|
|
then
|
|
return true
|
|
end
|
|
|
|
local typeA, typeB = typeof(a), typeof(b)
|
|
|
|
-- For tables, try recursive deep equality
|
|
if typeA == "table" and typeB == "table" then
|
|
local checkedKeys = {}
|
|
for key, value in a do
|
|
checkedKeys[key] = true
|
|
if not trueEquals(value, b[key]) then
|
|
return false
|
|
end
|
|
end
|
|
for key, value in b do
|
|
if checkedKeys[key] then
|
|
continue
|
|
end
|
|
if not trueEquals(value, a[key]) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
|
|
-- For NaN, check if both values are not equal to themselves
|
|
elseif a ~= a and b ~= b then
|
|
return true
|
|
|
|
-- For numbers, compare with epsilon of 0.0001 to avoid floating point inequality
|
|
elseif typeA == "number" and typeB == "number" then
|
|
return fuzzyEq(a, b, 0.0001)
|
|
|
|
-- For EnumItem->number, compare the EnumItem's value
|
|
elseif typeA == "number" and typeB == "EnumItem" then
|
|
return a == b.Value
|
|
elseif typeA == "EnumItem" and typeB == "number" then
|
|
return a.Value == b
|
|
|
|
-- For Color3s, compare to RGB ints to avoid floating point inequality
|
|
elseif typeA == "Color3" and typeB == "Color3" then
|
|
local aR, aG, aB = math.floor(a.R * 255), math.floor(a.G * 255), math.floor(a.B * 255)
|
|
local bR, bG, bB = math.floor(b.R * 255), math.floor(b.G * 255), math.floor(b.B * 255)
|
|
return aR == bR and aG == bG and aB == bB
|
|
|
|
-- For CFrames, compare to components with epsilon of 0.0001 to avoid floating point inequality
|
|
elseif typeA == "CFrame" and typeB == "CFrame" then
|
|
local aComponents, bComponents = { a:GetComponents() }, { b:GetComponents() }
|
|
for i, aComponent in aComponents do
|
|
if not fuzzyEq(aComponent, bComponents[i], 0.0001) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
|
|
-- For Vector3s, compare to components with epsilon of 0.0001 to avoid floating point inequality
|
|
elseif typeA == "Vector3" and typeB == "Vector3" then
|
|
local aComponents, bComponents = { a.X, a.Y, a.Z }, { b.X, b.Y, b.Z }
|
|
for i, aComponent in aComponents do
|
|
if not fuzzyEq(aComponent, bComponents[i], 0.0001) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
|
|
-- For Vector2s, compare to components with epsilon of 0.0001 to avoid floating point inequality
|
|
elseif typeA == "Vector2" and typeB == "Vector2" then
|
|
local aComponents, bComponents = { a.X, a.Y }, { b.X, b.Y }
|
|
for i, aComponent in aComponents do
|
|
if not fuzzyEq(aComponent, bComponents[i], 0.0001) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
return trueEquals
|