sugarcube-2 icon indicating copy to clipboard operation
sugarcube-2 copied to clipboard

Diff ignores arrays

Open Kassy2048 opened this issue 2 years ago • 3 comments

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).~~

Kassy2048 avatar May 29 '22 00:05 Kassy2048

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.

Kassy2048 avatar May 29 '22 00:05 Kassy2048

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.

tmedwards avatar Jun 04 '22 23:06 tmedwards

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.

Kassy2048 avatar Jun 06 '22 13:06 Kassy2048