Federico Mariani created CAMEL-24683:
----------------------------------------
Summary: camel-groovy - groovyJson prettyPrint=true re-lexes the
generated document, 30x slower than Jackson
Key: CAMEL-24683
URL: https://issues.apache.org/jira/browse/CAMEL-24683
Project: Camel
Issue Type: Improvement
Components: camel-groovy
Affects Versions: 4.22.0
Reporter: Federico Mariani
{{GroovyJSonlDataFormat.serialize}} renders the map with {{JsonOutput.toJson}}
and then, with the default {{prettyPrint=true}}, calls
{{JsonOutput.prettyPrint}} on the String, which lexes the whole document again
with a regex-based tokenizer.
Measured (JDK 21, through the DataFormat API): 1 KB document 60.8 us vs 1.9 us
for Jackson (32x), 352 KB allocated; 1 MB document 55.6 ms (30x) and 243 MB
allocated. 75-84% of the CPU is in {{JsonOutput.prettyPrint}}.
{{prettyPrint=false}} is 13.5 us (7x Jackson). It also writes with
{{String.getBytes()}}, the platform charset, instead of the exchange charset.
Proposed fix (implemented and benchmarked): a single-pass pretty writer that
walks Map / Iterable / arrays with the 4-space layout of
{{JsonOutput.prettyPrint}}, formats strings, integral numbers, finite doubles
and booleans directly with the escaping of {{JsonOutput}} (control characters,
quotes, backslashes and every character above '~' as lower case unicode
escapes), and delegates dates, big numbers, enums and POJOs to
{{JsonOutput.toJson}}. Output is byte-identical to
{{JsonOutput.prettyPrint(JsonOutput.toJson(doc))}} on 14 documents (empty
containers, nesting, unicode, escapes, nulls, doubles, BigDecimal,
Date/UUID/enum, arrays, top-level scalars, a POJO). Bytes are written with the
exchange charset (UTF-8 default).
With the fix: 1 KB 2.7 us (22x faster, on a par with Jackson's pretty printer),
1 MB 3.1 ms (18x).
Related: CAMEL-23001 (data format added), CAMEL-24669 (compact path fixed).
----
_Found during a benchmark review of camel-groovy and the other scripting
languages (camel-performance-tests branch camel-groovy-perf,
tests/camel-jmh/groovy-results, JMH 1.37, JDK 21, one M1 Pro laptop). A
candidate fix with tests exists on the local apache/camel branch
camel-perf-all-fixes; a PR follows._
_Claude Code on behalf of Croway_
--
This message was sent by Atlassian Jira
(v8.20.10#820010)