Skip to content

uniqueItems: comparisonKey allocations slow down validation of all arrays (master and 2.x) #1302

Closed

Description

@stevehu

#1273 (master) and its backport #1301 (2.x) changed UniqueItemsValidator to compare items by comparisonKey. That fixed numeric equality ([1, 1.0] is now a duplicate), but every array now pays for it, not just arrays that mix integers and decimals:

  • every number gets a new BigDecimal from decimalValue().stripTrailingZeros();
  • every nested array or object is deep-copied into an ArrayList/LinkedHashMap key.

Rough timing on Java 8 against 2.x, with schema {"uniqueItems":true}, 20 warm-up runs, then the average of 20:

Instance Before (node equality) After (comparisonKey)
200k ints 1.8 ms 7.9 ms
20k objects, 10 string fields each 3.3 ms 6.7 ms

master uses the same design, so it should behave the same way.

Possible approaches:

  • Use a cheaper key for numbers: Long.valueOf(longValue()) for integral nodes that fit in a long, and the stripped BigDecimal only for the other numbers, mapped to Long when it's integral and fits. Integers and decimals with the same value still collide.
  • Or don't copy containers: wrap each node in a small key object whose equals/hashCode walk the node and compare numbers by value. That avoids allocating a copy of each nested array or object.

The fix needs to:

  • keep the numeric-equality rules (UniqueItemsNumericEqualityTest) and the JSON-Schema-Test-Suite passing (numbers stay distinct from booleans and strings);
  • land on both master and 2.x;
  • include a before/after measurement.

Activity

  1. stevehu commented on Oct 12, 2026

    @stevehu
    ContributorAuthor

    Two smaller items from the #1301 review were deferred here. Both apply to master and 2.x, so please fix them on both branches along with the speed work:

    1. A test that passes without the fix. UniqueItemsNumericEqualityTest.decimalsWithDifferentScaleAreDuplicates ([1.0, 1.00]) passes with or without numeric folding. The default mapper reads both values as the same DoubleNode(1.0), so they were already equal as nodes. To make it test scale-independence, read the instance with USE_BIG_DECIMAL_FOR_FLOATS so the two values stay as BigDecimals with different scales.
    2. Javadoc indentation. In the comparisonKey Javadoc in UniqueItemsValidator, the lines starting * of that and keep comparing as nodes and * Only numbers are folded together lost their leading indentation.
  2. ItsDeadlyProgrammer commented on Oct 12, 2026

    @ItsDeadlyProgrammer
    Contributor

    I'll take this, along with the two items from the #1301 review. I'll open a PR for master and one for 2.x, with before/after timings.

  3. added 2 commits that reference this issue on Oct 13, 2026
    87e8554
    e2750bf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions