Nuno Santos created OAK-12411:
---------------------------------
Summary: Avoid allocation and cache hash code in
Template.hashCode()
Key: OAK-12411
URL: https://issues.apache.org/jira/browse/OAK-12411
Project: Jackrabbit Oak
Issue Type: Improvement
Components: segment-tar
Reporter: Nuno Santos
{{Template.hashCode()}} currently recomputes its hash on every call via
{{Objects.hash(primaryType, mixinTypes, Arrays.asList(properties),
getTemplateType(), childName)}}, which allocates a varargs {{Object[]}} and a
{{List}} wrapper around {{properties}} on every invocation.
{{Template}} is immutable once constructed, so the hash code can be computed
once and cached, following the same lazy-caching pattern as
{{java.lang.String#hashCode()}}:
* A field initialized to {{0}} means "not yet computed".
* A benign race across threads just recomputes the same value; no
synchronization needed.
This removes the per-call allocation, which matters because
{{Template.equals()}}/{{hashCode()}} are used for template deduplication during
node writes — one of the hotter paths in {{DefaultSegmentWriter}}.
No behavioral change intended; the {{equals()}}/{{hashCode()}} contract is
preserved.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)