-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caw/improve array comparison performance #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ function myersDiff(actual, expected, checkCommaDisparity = false) { | |
const actualLength = actual.length; | ||
const expectedLength = expected.length; | ||
const max = actualLength + expectedLength; | ||
// TODO(BridgeAR): Cap the input in case the values go beyond the limit of 2^31 - 1. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The TODO about capping input lengths is valid but unimplemented. Failing to cap could cause Int32Array overflow when actualLength + expectedLength exceeds ~2^31, leading to incorrect diffs or crashes. Must implement input length validation before proceeding. SUGGESTION: + const MAX_SAFE_LENGTH = 0x7FFFFFF0; // ~2^31-16
+ if (actualLength > MAX_SAFE_LENGTH || expectedLength > MAX_SAFE_LENGTH) {
+ throw new Error('Input arrays too large for Myers diff');
+ }
const actualLength = actual.length;
const expectedLength = expected.length;
const max = actualLength + expectedLength;
- const v = new Int32Array(2 * max + 1);
+ if (max > MAX_SAFE_LENGTH) {
+ throw new Error('Combined lengths exceed safe limit');
+ }
+ const v = new Int32Array(2 * max + 1); |
||
const v = new Int32Array(2 * max + 1); | ||
const trace = []; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ const { | |
BooleanPrototypeValueOf, | ||
DatePrototypeGetTime, | ||
Error, | ||
NumberIsNaN, | ||
NumberPrototypeValueOf, | ||
ObjectGetOwnPropertySymbols: getOwnSymbols, | ||
ObjectGetPrototypeOf, | ||
|
@@ -185,7 +184,9 @@ function innerDeepEqual(val1, val2, mode, memos) { | |
// Check more closely if val1 and val2 are equal. | ||
if (mode !== kLoose) { | ||
if (typeof val1 === 'number') { | ||
return NumberIsNaN(val1) && NumberIsNaN(val2); | ||
// Check for NaN | ||
// eslint-disable-next-line no-self-compare | ||
return val1 !== val1 && val2 !== val2; | ||
} | ||
if (typeof val2 !== 'object' || | ||
typeof val1 !== 'object' || | ||
|
@@ -196,11 +197,10 @@ function innerDeepEqual(val1, val2, mode, memos) { | |
} | ||
} else { | ||
if (val1 === null || typeof val1 !== 'object') { | ||
if (val2 === null || typeof val2 !== 'object') { | ||
// eslint-disable-next-line eqeqeq | ||
return val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2)); | ||
} | ||
return false; | ||
return (val2 === null || typeof val2 !== 'object') && | ||
// Check for NaN | ||
// eslint-disable-next-line eqeqeq, no-self-compare | ||
(val1 == val2 || (val1 !== val1 && val2 !== val2)); | ||
} | ||
if (val2 === null || typeof val2 !== 'object') { | ||
return false; | ||
|
@@ -384,9 +384,7 @@ function keyCheck(val1, val2, mode, memos, iterationType, keys2) { | |
} | ||
} else if (keys2.length !== ObjectKeys(val1).length) { | ||
return false; | ||
} | ||
|
||
if (mode === kStrict) { | ||
} else if (mode === kStrict) { | ||
const symbolKeysA = getOwnSymbols(val1); | ||
if (symbolKeysA.length !== 0) { | ||
let count = 0; | ||
|
@@ -502,7 +500,9 @@ function findLooseMatchingPrimitives(prim) { | |
// a regular number and not NaN. | ||
// Fall through | ||
case 'number': | ||
if (NumberIsNaN(prim)) { | ||
// Check for NaN | ||
// eslint-disable-next-line no-self-compare | ||
if (prim !== prim) { | ||
return false; | ||
} | ||
} | ||
|
@@ -758,9 +758,9 @@ function sparseArrayEquiv(a, b, mode, memos, i) { | |
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
for (; i < keysA.length; i++) { | ||
const key = keysA[i]; | ||
if (!hasOwn(b, key) || !innerDeepEqual(a[key], b[key], mode, memos)) { | ||
for (; i < keysB.length; i++) { | ||
const key = keysB[i]; | ||
if ((a[key] === undefined && !hasOwn(a, key)) || !innerDeepEqual(a[key], b[key], mode, memos)) { | ||
return false; | ||
Comment on lines
+761
to
764
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looping over only SUGGESTION: - for (; i < keysB.length; i++) {
+ for (const key of [...keysA, ...keysB].filter((key, index, self) => self.indexOf(key) === index))) { |
||
} | ||
} | ||
|
@@ -782,17 +782,14 @@ function objEquiv(a, b, mode, keys2, memos, iterationType) { | |
return partialArrayEquiv(a, b, mode, memos); | ||
} | ||
for (let i = 0; i < a.length; i++) { | ||
if (!innerDeepEqual(a[i], b[i], mode, memos)) { | ||
return false; | ||
} | ||
const isSparseA = a[i] === undefined && !hasOwn(a, i); | ||
const isSparseB = b[i] === undefined && !hasOwn(b, i); | ||
if (isSparseA !== isSparseB) { | ||
if (b[i] === undefined) { | ||
if (!hasOwn(b, i)) | ||
return sparseArrayEquiv(a, b, mode, memos, i); | ||
if (a[i] !== undefined || !hasOwn(a, i)) | ||
return false; | ||
} else if (a[i] === undefined || !innerDeepEqual(a[i], b[i], mode, memos)) { | ||
return false; | ||
} | ||
if (isSparseA) { | ||
return sparseArrayEquiv(a, b, mode, memos, i); | ||
} | ||
} | ||
} else if (iterationType === kIsSet) { | ||
if (!setEquiv(a, b, mode, memos)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting actual[0] to undefined here affects all test cases, including non-sparse ones. This changes the behavior of existing tests like 'deepEqual_Array' which should test dense arrays without undefined elements. Move this setup into the sparse test cases only.
SUGGESTION:
- actual[0] = undefined;