sugarcube-2
sugarcube-2 copied to clipboard
Diff ignores arrays
Hi,
When calling Diff.diff()
to generate deltas between states, arrays properties are always cloned in the result, even if there is no change in the array. This method seems to support diffing between arrays, so this is probably an unexpected behavior.
Arrays are objects, but Object.prototype.toString.call()
returns [object Array]
for them, not [object Object]
, so this check fails and the arrays content is always cloned. Changing the condition from if (origPType !== '[object Object]')
to if (origPType !== '[object Object]' && origPType !== '[object Array]')
makes it possible to diff between array properties and significantly reduces the deltas size on my tests (up to x10 on some games).
~~Also, I have a question: why deltas are not used for saves? This could reduce the localStorage
space usage, and maybe remove the need for lzstring (or at least speed it up as the data to compress would be smaller).~~
Also, I have a question: why deltas are not used for saves? This could reduce the
localStorage
space usage, and maybe remove the need for lzstring (or at least speed it up as the data to compress would be smaller).
Ignore that part please: deltas are used for saves, but they are created after the call to stateMarshal()
instead of during that call.
I know that it used to handle arrays. I don't recall at the moment if array handling was purposefully removed for some reason or it was accidentally dropped during a revision. At a guess, it's probably the latter.
I'll look into it when I get my workstation up and running again.
FYI, it probably stopped working when you added code to filter out unknown non-generic objects (r1617 from the old mercurial repository). The commit message does not mention arrays explicitly, so probably a mistake indeed.