Skip to content

Performance improvements - #103

Merged
chbrown merged 3 commits into
chbrown:masterfrom
joshkel:perf-improvements
May 28, 2025
Merged

chbrown merged 3 commits into
chbrown:masterfrom
joshkel:perf-improvements

Conversation

@joshkel

@joshkel joshkel commented Mar 18, 2025

Copy link
Copy Markdown
Contributor

I found that rfc6902 could perform quite slowly when comparing a data structure containing 700-element arrays. This PR makes a few changes to help improve this:

  • Use a number-keyed map instead of a JS object to avoid overhead of stringifying array indexes then looking up numeric string object keys.
  • Since we only need a single lowest cost item, find that, instead of doing a full array sort.
  • Use a linked list of operations to avoid having to repeatedly copy and concatenate operations arrays.

The final code runs roughly 3x faster for the data we're manipulating.

@joshkel

joshkel commented Mar 18, 2025

Copy link
Copy Markdown
Contributor Author

I pushed a further performance improvement. This changes the signature of intersection. Since intersection is neither documented nor exported from index.ts, I hope this is okay, but I can change it if desired.

@joshkel

joshkel commented May 24, 2025

Copy link
Copy Markdown
Contributor Author

Yet another performance improvement. Instead of doing a full diff, with pointer manipulation, just to check if two items are equal, this lets it exit out at the first inequality (assuming the code isn't using a custom diff implementation). This makes fast-deep-equal roughly 35 times faster for my test cases. This is, obviously, a more invasive change: it adds a dependency and breaks the UMD build. Since this is a more invasive change, please let me know how you'd like me to proceed - I can pull that change into a separate PR, or introduce something like Rollup to fix the UMD build, or something else.

@chbrown

chbrown commented May 24, 2025

Copy link
Copy Markdown
Owner

thanks most of this looks good, mainly would like to keep things simple by splitting out the non-controversial stuff into an initial PR then the other optimizations into a separate PR

please followup if it's not clear which goes where

Comment thread diff.ts Outdated
if (hasOwnProperty.call(object, key) && object[key] !== undefined) {
counter[key] = (counter[key] || 0) + 1
}
export function intersection(a: {[index: string]: any}, b: {[index: string]: any}): string[] {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this changes a public API; separate and call it intersection2. don't export it if possible

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks.

What would you think about marking intersection (and, probably, subtract) as @deprecated, since rfc6902 will no longer need intersection itself, and since they're not exported from index.ts and don't seem directly related to JSON Patch functionality?

Comment thread diff.ts Resolved
Comment thread diff.ts Resolved
Comment thread dist/rfc6902.js Resolved
Comment thread package.json Outdated
"clean": "tsc -b . test --clean"
},
"dependencies": {
"fast-deep-equal": "^3.1.3"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will almost certainly merit a major version bump; and tbd — I still need to look into whether it's worth adding a dep — put this in a subsequent deeper-cut PR

Comment thread diff.ts Resolved
Comment thread diff.ts
Comment on lines 116 to +162
interface DynamicAlternative {
operations: ArrayOperation[]
operation: ArrayOperation
/**
cost indicates the total cost of getting to this position.
*/
cost: number
base?: DynamicAlternative
}

function appendArrayOperation(base: DynamicAlternative, operation: ArrayOperation): DynamicAlternative {
function appendArrayOperation(base: DynamicAlternative | undefined, operation: ArrayOperation): DynamicAlternative {
return {
// the new operation must be pushed on the end
operations: base.operations.concat(operation),
cost: base.cost + 1,
operation,
cost: !base ? 1 : base.cost + 1,
base,
}
}

function findLowestCost(items: DynamicAlternative[]) {
let bestCost = Infinity;
let best: DynamicAlternative | undefined;
for (const item of items) {
if (item.cost < bestCost) {
bestCost = item.cost
best = item
}
}
return best!
}

function getOperationsArray(item: DynamicAlternative | undefined): ArrayOperation[] {
const operations: ArrayOperation[] = []
while (item) {
operations.push(item.operation)
item = item.base
}
return operations.reverse()
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. move all this to a subsequent PR

joshkel added 3 commits May 26, 2025 15:27
Use a number-keyed map instead of a JS object to avoid overhead of stringifying and hashing numbers.

Update tsconfig.json to prevent TypeScript warning about Map.
Since `intersection` is part of the public interface, this adds a more targeted `intersection2` function.
@joshkel
joshkel force-pushed the perf-improvements branch from 4b2a4af to e88ed9d Compare May 26, 2025 19:48
@joshkel

joshkel commented May 26, 2025

Copy link
Copy Markdown
Contributor Author

I've reworked the PR to contain just the first set of changes, as requested. Thank you for the feedback.

Personally, I'd also include the findLowestCost change in this initial PR - replacing an O(n log n) sort with an O(n) search, when all you need a single item, seems uncontroversial and cognitively cheap. But I'm happy to go with whatever you decide.

The full set of changes are in https://github.com/joshkel/rfc6902/tree/all-perf-improvements. I can create additional PRs as desired from there once this PR is done.

@chbrown chbrown left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! lgtm

Comment thread diff.ts Resolved
Comment thread diff.ts Resolved
@chbrown
chbrown merged commit ad05aac into chbrown:master May 28, 2025
@chbrown

chbrown commented May 28, 2025

Copy link
Copy Markdown
Owner

But I'm happy to go with whatever you decide.

Yeah this is what I wanted — thanks! Smaller chunks make it easier to take a look as free time allows ;) send along the next batch at your leisure

chbrown added a commit that referenced this pull request Nov 21, 2026
With the default diff, diffArrays now checks whether two elements are
equal with isEqual, which gives the same answer as !diffAny(...).length
but stops at the first difference and builds no operations or pointers.
A custom diff is still called, since it may consider elements equal that
isEqual wouldn't (#15). equalAtomic now takes an equality callback, so
the Date/Map/Set rules are shared by diffAny and isEqual.

Based on the early-exit idea from #103 and indepedently matches
implementation in #117 (our Claudes think alike ;)).

Coded by Claude, who lacks a real email address.

Co-Authored-By: Vico <[email protected]>
Co-Authored-By: Josh Kelley <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants