Repository navigation
Performance improvements - #103
Conversation
|
I pushed a further performance improvement. This changes the signature of |
|
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. |
|
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 |
| 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[] { |
There was a problem hiding this comment.
yeah this changes a public API; separate and call it intersection2. don't export it if possible
There was a problem hiding this comment.
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?
| "clean": "tsc -b . test --clean" | ||
| }, | ||
| "dependencies": { | ||
| "fast-deep-equal": "^3.1.3" |
There was a problem hiding this comment.
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
| 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() | ||
| } | ||
|
|
There was a problem hiding this comment.
e.g. move all this to a subsequent PR
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.
4b2a4af to
e88ed9d
Compare
|
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 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. |
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 |
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]>
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:
The final code runs roughly 3x faster for the data we're manipulating.