Skip to content

Skip serializing string nodes for const error arguments - #1393

Merged
stevehu merged 2 commits into
networknt:masterfrom
g4lb:perf-const-string-args
Nov 27, 2026
Merged

stevehu merged 2 commits into
networknt:masterfrom
g4lb:perf-const-string-args

Conversation

@g4lb

@g4lb g4lb commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Each failed const calls node.toString() for the error arguments, also for a string node. This change uses asString() for a StringNode. The arguments are the same.

Benchmark Before After Change
String const, no match 273 ns 89 ns −67%
items, 10 mixed values 1.86 µs 1.01 µs −46%
oneOf, 100 items, invalid 94.1 µs 60.4 µs −36%
oneOf, 100 items, valid 82.0 µs 52.9 µs −35%
Number or object const no change

The errors are the same on 1.1M inputs. Tests pass on JDK 17, 21 and 26.

@stevehu

stevehu commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Review of #1393

Reviewed at head d45fab2. The change looks correct to me: the const error arguments are unchanged. In Jackson 3, StringNode.asString() and asString(String) both return the raw value, so skipping toString() for a string node is safe. All 11,324 tests pass on JDK 25 here, with 45 skipped. ConstValidatorTest already checks the string case ("must be the constant value 'aa' but is 'bb'").

I compared the old and new conversions with a probe on Jackson 3.0.1, 3.1.8 and 3.2.4, over 20 node types: string, empty string, int, long, short, float, double, BigInteger, BigDecimal, boolean, null, binary, POJO, object, array and missing. They matched every time.

Minor

Numbers, booleans and null still pay for toString() (ConstValidator.java:46)

The shortcut only applies when the class is exactly StringNode. For a failed {"const": 1} against 2 (or true, or null), both nodes are still serialized with toString(), then asString(String) drops that result and returns the scalar text.

Suggested change:

private static String asString(JsonNode node) {
    return node.asStringOpt().orElseGet(node::toString);
}

This builds the default only when it is used. In the same probe it gave the same string as the old asString(node.toString()) for all 20 node types on all three Jackson versions. The class check and the StringNode import are then no longer needed.

@g4lb

g4lb commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thank you. I applied your suggestion.

Benchmark Before After Change
Number 279 ns 101 ns −64%
String 248 ns 77 ns −69%
Mixed values 1.67 µs 684 ns −59%

The output is the same on 1.17M random inputs.

@stevehu

stevehu commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Re-review of #1393

Reviewed at head 9ba69ab. Thanks, this looks good to me.

asStringOpt().orElseGet(node::toString) gives the same string as the old asString(node.toString()) for every node type I tried: string, empty string, the numeric types, boolean, null, binary, POJO, object, array and missing, on Jackson 3.0.1, 3.1.8 and 3.2.4. All 11,278 tests pass on JDK 25 here, with 45 skipped.

@stevehu
stevehu merged commit 2de9814 into networknt:master Nov 27, 2026
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