Federico Mariani created CAMEL-24684:
----------------------------------------
Summary: camel-groovy - groovyXml creates a new XmlParser per
unmarshal and writes one fragment at a time on marshal
Key: CAMEL-24684
URL: https://issues.apache.org/jira/browse/CAMEL-24684
Project: Camel
Issue Type: Improvement
Components: camel-groovy
Affects Versions: 4.22.0
Reporter: Federico Mariani
Unmarshal: {{GroovyXmlDataFormat.unmarshal}} does {{new XmlParser()}} on every
call. Groovy's constructor looks the {{SAXParserFactory}} up on the class path
and sets its features, and the JDK builds a throwaway parser for every
{{setFeature}}: for a 1 KB document that is 61% of the CPU and 64% of the bytes
(33.2 us and 66 KB per parse; a reused factory measured 18.3 us).
Marshal: {{printLines}} calls {{String.getBytes()}} and {{os.write()}} once per
fragment (platform charset) and runs the {{TypeConverter}} on every value;
2.5-3x jacksonXml and 4-5x the bytes, and 28-34x slower on an unbuffered stream
(252 us for 1 KB, 261 ms for 1 MB).
Proposed fix (implemented and benchmarked): one configured {{SAXParserFactory}}
per data format with exactly the features Groovy 5.1.2 sets
({{setNamespaceAware(true)}}, {{setValidating(false)}},
{{FEATURE_SECURE_PROCESSING}}, {{disallow-doctype-decl}}) and a {{SAXParser}}
per thread wrapped by a cheap {{new XmlParser(saxParser)}} per call; the
document rendered in memory and written once with the exchange charset; no
{{TypeConverter}} call for String/Number/Boolean values. A DOCTYPE/XXE document
is still rejected with the same {{SAXParseException}}; marshal output is
byte-identical to the previous output.
With the fix: 1 KB unmarshal 33.2 -> 10.8 us (3.1x), marshal 1.1-1.2x,
unbuffered stream 33x. 1 MB unmarshal unchanged (the parse dominates).
Related: CAMEL-22361 (original groovy-xml), design/security.adoc (XML parser
features).
----
_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)