CAMEL-23686: Avoid creating empty headers map on read access#23784
Conversation
getHeader(), removeHeader(), and removeHeaders() were forcing creation of a CaseInsensitiveMap (allocating 4 arrays) just to return null when no headers had been set. Now short-circuits when headers is null and isPopulateHeadersSupported() is false (DefaultMessage). JMS-style messages with lazy header population are preserved. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
🧪 CI tested the following changed modules:
|
davsclaus
left a comment
There was a problem hiding this comment.
Clean optimization — eliminates unnecessary CaseInsensitiveMap allocation (~200 bytes) on read access when headers haven't been set. The isPopulateHeadersSupported() guard correctly preserves the JMS-style lazy population path. The DRY refactor of getHeader overloads delegating to the base method is a nice simplification (-37 net lines). CI is green.
This review does not replace specialized AI review tools (CodeRabbit, Sourcery) or static analysis (SonarCloud).
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
CAMEL-23686
Summary
getHeader(),removeHeader(), andremoveHeaders()no longer force creation of aCaseInsensitiveMapwhen headers haven't been setnullreturn whenheaders == nullandisPopulateHeadersSupported()isfalseisPopulateHeadersSupported() == true) are preservedProblem
Every
getHeader(name)call on a message with no headers forced creation of aCaseInsensitiveMap(allocating 4 internal arrays, ~200 bytes) just to returnnull. This happened frequently in splitter/multicast sub-exchanges that never use headers.Before:
After:
Benchmark results
Baseline route (timer → split(1000) → setBody, no header access):
DefaultMessageinstances but only 4CaseInsensitiveMap— split children don't allocate maps at allgetHeader()callPipeline route (with
setHeaderon every exchange):Test plan
DefaultMessageHeaderTest(38 tests) — pass, including lazy-populated headers testDefaultMessageTest— pass