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

jiangtian pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/tsfile.git


The following commit(s) were added to refs/heads/develop by this push:
     new f63ccb2f Cache hash code of StringArrayDeviceID (#453)
f63ccb2f is described below

commit f63ccb2f8cc8bccd23629234fd545cc88d695eef
Author: Haonan <[email protected]>
AuthorDate: Tue Apr 22 14:28:41 2025 +0800

    Cache hash code of StringArrayDeviceID (#453)
---
 .../tsfile/file/metadata/StringArrayDeviceID.java    | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git 
a/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/StringArrayDeviceID.java
 
b/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/StringArrayDeviceID.java
index e012cdfb..7589c0ea 100644
--- 
a/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/StringArrayDeviceID.java
+++ 
b/java/tsfile/src/main/java/org/apache/tsfile/file/metadata/StringArrayDeviceID.java
@@ -76,6 +76,15 @@ public class StringArrayDeviceID implements IDeviceID {
   // or we can just use a tuple like Relational DB.
   private final String[] segments;
 
+  /** Cache the hash code */
+  private int hash; // Default to 0
+
+  /**
+   * Cache if the hash has been calculated as actually being zero, enabling us 
to avoid
+   * recalculating this.
+   */
+  private boolean hashIsZero; // Default to false;
+
   public StringArrayDeviceID(String... deviceIdSegments) {
     this.segments = formalize(deviceIdSegments);
   }
@@ -306,7 +315,16 @@ public class StringArrayDeviceID implements IDeviceID {
 
   @Override
   public int hashCode() {
-    return Arrays.hashCode(segments);
+    int h = hash;
+    if (h == 0 && !hashIsZero) {
+      h = Arrays.hashCode(segments);
+      if (h == 0) {
+        hashIsZero = true;
+      } else {
+        hash = h;
+      }
+    }
+    return h;
   }
 
   @Override

Reply via email to