You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by nf...@apache.org on 2026/09/25 12:01:06 UTC

(jackrabbit-oak) branch trunk updated: OAK-12411: Avoid allocation and cache hash code in Template.hashCode() (#3146)

This is an automated email from the ASF dual-hosted git repository.

nfsantos pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 3941fd3667 OAK-12411: Avoid allocation and cache hash code in Template.hashCode() (#3146)
3941fd3667 is described below

commit 3941fd3667252047ab181c6eecaa50e01d163374
Author: Nuno Santos <ns...@adobe.com>
AuthorDate: Fri Sep 25 14:01:00 2026 +0200

    OAK-12411: Avoid allocation and cache hash code in Template.hashCode() (#3146)
---
 .../apache/jackrabbit/oak/segment/Template.java    | 28 ++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/Template.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/Template.java
index 3f8c9e740e..a978e317bf 100644
--- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/Template.java
+++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/Template.java
@@ -91,6 +91,14 @@ public class Template {
     @Nullable
     private final String childName;
 
+    /**
+     * Cached hash code. Lazily computed by {@link #hashCode()} and never
+     * reset, relying on {@code Template} being immutable once constructed.
+     * {@code 0} means "not yet computed"; a benign race just recomputes the
+     * same value, as with {@link String#hashCode()}.
+     */
+    private int hash;
+
     Template(@NotNull SegmentReader reader,
              @Nullable PropertyState primaryType,
              @Nullable PropertyState mixinTypes,
@@ -154,6 +162,13 @@ public class Template {
         return mixinTypes;
     }
 
+    /**
+     * Returns the property templates of this template. The returned array is
+     * not a defensive copy and must be treated as read-only, as mutating it
+     * would invalidate the cached {@link #hashCode()}.
+     *
+     * @return property templates
+     */
     PropertyTemplate[] getPropertyTemplates() {
         return properties;
     }
@@ -307,8 +322,17 @@ public class Template {
 
     @Override
     public int hashCode() {
-        return Objects.hash(primaryType, mixinTypes,
-                Arrays.asList(properties), getTemplateType(), childName);
+        int result = hash;
+        if (result == 0) {
+            result = 1;
+            result = 31 * result + Objects.hashCode(primaryType);
+            result = 31 * result + Objects.hashCode(mixinTypes);
+            result = 31 * result + Arrays.hashCode(properties);
+            result = 31 * result + Short.hashCode(getTemplateType());
+            result = 31 * result + Objects.hashCode(childName);
+            hash = result;
+        }
+        return result;
     }
 
     @Override