Repository navigation
[2.x] Compare uniqueItems items without copying them - #1306
Conversation
comparisonKey built a BigDecimal for every number and copied every nested array and object, so all arrays paid for the numeric-equality fix, not only arrays that mix integers and decimals. Wrap each item in a key that walks the node and compares numbers by value instead: whole numbers that fit in a long as longs, other doubles as doubles, and only other BigIntegers and BigDecimals as BigDecimals. Whole doubles at or above 2^63 are compared by their exact value, the same as smaller whole doubles, rather than by their shortest decimal form. Make decimalsWithDifferentScaleAreDuplicates read BigDecimals so it no longer passes without numeric folding, fix the comparisonKey Javadoc indentation by rewriting it, and add tests for the new comparison paths. Fixes networknt#1302
|
Same review as #1305, see #1305 (posted just now). The code here is the same apart from the Jackson imports. On
|
… doubles A BIG number that isn't exactly a double's value now hashes its own exact value, so integers that round to the same double no longer share a bucket. Hashing from the BigDecimal also keeps the branch identical to master, where Jackson 3 throws from doubleValue() outside the double range. Whole doubles of 2^53 and above go back to comparing by decimalValue(), their shortest decimal form, as before. Changing that belongs with const and enum in networknt#1293.
|
Same fix as #1305, see my reply there: c2cd3f9. Only the Jackson imports differ. On |
|
Same review as #1305, see #1305 (posted just now). c2cd3f9 fixes items 1–3 here too. On Java 8, the suites pass and 20,000 consecutive integers from 2^70 take 33 ms. The -2^63 problem described there also happens here on Java 8, and the same |
|
Follow-up for items 4 and 6 is now #1307. |
stevehu
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. I re-reviewed ba114aa (#1305) and bc140e7 (#1306); the two are identical apart from the Jackson imports.
Verification
- The -2^63 fix works.
[-2^63, its shortest form]is a duplicate and[-2^63, 9223372036854775616]is unique, matching base andconst. All 15 -2^63 edge cases match base on Java 8, 17, 21 and 25. - Revert check: with the previous commit's validator,
minusTwoToSixtyThreeAsADoubleIsComparedByItsShortestDecimalFormfails on JDK 25 and Java 8. - On
master(JDK 17, 25) and2.x(Java 8, 21):- The JSON Schema Test Suite (7261), the UniqueItems tests (50 + 17), and the const/enum tests pass.
- The 253k-input and 2.39M-input fuzzes show 0 disagreements.
- The DoS probe stays at 4–20 ms for 20k items.
Optional, not blocking
- The test Javadoc says the shortest form is
-9223372036854776000"on JDK 19+". Corretto 8 and 17 give the same form, so the JDK qualifier can go. - One more assertion would pin the pair on the boundary that moved:
assertTrue(valid(array().add(Long.MIN_VALUE).add(-0x1p63))). value > -0x1p63relies ondecimalValue()of -2^63 being belowLong.MIN_VALUE. That holds on every JDK I tested. Deciding that single value from its decimal form would remove the assumption, if you ever touch this again.
Fixes #1302 on
2.x. Same change as #1305.UniqueItemsValidatornow compares items through a small key that walks each node in place, instead of building acomparisonKeycopy. Numbers are still compared by value, so[1, 1.0]is still a duplicate, but arrays without mixed number types no longer pay for it.How numbers are compared:
decimalValue(), as before. For a double that is its shortest decimal form, so1e30equals10^30and a double read from0.1equals aBigDecimal0.1.long(ints, longs, and whole doubles,BigIntegers andBigDecimals) are compared aslongs.doubles.BigIntegers andBigDecimals outside that range are compared asBigDecimals.Double.hashCodeof its value. ABigIntegerorBigDecimaloutside the long range takes that same hash only when it is exactly a double's value; otherwise it hashes its own exact value. So equal numbers of different types land in the same bucket, while integers that only round to the same double, such as 2^70 and 2^70 + 1, don't. The hash never callsdoubleValue()on a big node, which Jackson 3 would throw from for values like 10^400.There is no behaviour change from the current code. Fuzzing 400,000 number pairs (large whole doubles, powers of ten, big integers near them, trailing-zero decimals, and each value in another type) against the current
comparisonKeygives the same verdict for every pair, both at the top level and nested.Items deferred from the #1301 review:
decimalsWithDifferentScaleAreDuplicatesnow reads the instance withUSE_BIG_DECIMAL_FOR_FLOATS, so1.0and1.00stayBigDecimals with different scales. On2.xit passes even without numeric folding, because Jackson 2 strips trailing zeros when it reads withUSE_BIG_DECIMAL_FOR_FLOATS, andDecimalNodeequality ignores scale. It is kept so both branches have the same test.comparisonKeyJavadoc is rewritten for the new key, with the indentation fixed.New tests in
UniqueItemsNumericEqualityTest: double vsBigDecimal,0vs-0.0, numbers outside the long range, large whole doubles ([1e30, 10^30]is a duplicate, the long 2^60 and the double 2^60 are not), 1,000 consecutive integers from 2^70 (all unique, then one repeated),[10^400]nested and at the top level, and[1e400, 1e401]/[1e400, 1e400]withUSE_BIG_DECIMAL_FOR_FLOATS, and object property order. On the first version of this PR, the large-double test failed on both branches and the 10^400 test threw onmaster. The 2^70 test checks correctness only, so it doesn't depend on timing.Timing with schema
{"uniqueItems":true}: 50 warm-up runs, then the best of 5 rounds of 20 runs, and the median of 3 JVM runs. Times in ms.master, JDK 25:comparisonKey)i.5)[i, i+1.25, {"a":i}])2.x, JDK 17 (I don't have a Java 8 JDK here):comparisonKey)i.5)Against the current code it is 1.5–3x faster on
masterand 1.4–4.2x on2.x. It's still a bit slower than plain node equality, mostly from one key object per item and classifying each number. The objects numbers were the noisiest between runs. None of these instances goes through the big-number hash.Consecutive integers from 2^70, which share a double in runs of 2^18. The first two columns are from your review; the last is one run with no warm-up:
master) / 4.6 s (2.x)master) / 19 ms (2.x)master) / 108 ms (2.x)mvn verifypasses, including the JSON Schema Test Suite: 8625 tests on JDK 17.