This is an automated email from the ASF dual-hosted git repository. haonan pushed a commit to branch StringArrayDeviceID_cache_hash in repository https://gitbox.apache.org/repos/asf/tsfile.git
commit a33dbdfa54e27d4e8dd691d24c8cae90a3985e00 Author: HTHou <[email protected]> AuthorDate: Tue Apr 8 10:59:52 2025 +0800 Cache hash code of StringArrayDeviceID --- .../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
