This is an automated email from the ASF dual-hosted git repository.
jiangtian pushed a commit to branch mem_usage_refinement
in repository https://gitbox.apache.org/repos/asf/tsfile.git
The following commit(s) were added to refs/heads/mem_usage_refinement by this
push:
new b88e3a30 Cache hash code of StringArrayDeviceID (#453)
b88e3a30 is described below
commit b88e3a30bd005bbd08e94d65658d847f8d562343
Author: Haonan <[email protected]>
AuthorDate: Tue Apr 22 14:28:41 2025 +0800
Cache hash code of StringArrayDeviceID (#453)
(cherry picked from commit f63ccb2f8cc8bccd23629234fd545cc88d695eef)
---
.../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 685df51f..6f093add 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);
}
@@ -309,7 +318,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