From 64faf7e54d88deba0a74772feab5f7ad91707092 Mon Sep 17 00:00:00 2001 From: Patrick Zhai Date: Fri, 3 Nov 2023 13:37:30 -0700 Subject: [PATCH] Let FieldDef lazy-calculate and cache hash code (#892) Sometimes _dataSchema could be very complex and makes computing hash code for this class extremely slow. Since all data fields participated in hash code calculation is final, we should able to calculate it at construction time and avoid paying the cost repeatedly. * Update FieldDef lazily calculate and cache hash code --------- Co-authored-by: Patrick Zhai --- CHANGELOG.md | 6 +++++- .../java/com/linkedin/data/template/FieldDef.java | 11 +++++++++++ gradle.properties | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 448257624e..21a72fe58b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and what APIs have changed, if applicable. ## [Unreleased] +## [29.46.9] - 2023-11-02 +- Update FieldDef so that it will lazily cache the hashCode. + ## [29.46.8] - 2023-10-11 - add metrics about xds connection status and count @@ -5554,7 +5557,8 @@ patch operations can re-use these classes for generating patch messages. ## [0.14.1] -[Unreleased]: https://github.com/linkedin/rest.li/compare/v29.46.8...master +[Unreleased]: https://github.com/linkedin/rest.li/compare/v29.46.9...master +[29.46.9]: https://github.com/linkedin/rest.li/compare/v29.46.8...v29.46.9 [29.46.8]: https://github.com/linkedin/rest.li/compare/v29.46.7...v29.46.8 [29.46.7]: https://github.com/linkedin/rest.li/compare/v29.46.6...v29.46.7 [29.46.6]: https://github.com/linkedin/rest.li/compare/v29.46.5...v29.46.6 diff --git a/data/src/main/java/com/linkedin/data/template/FieldDef.java b/data/src/main/java/com/linkedin/data/template/FieldDef.java index 90b14c9e72..5b83795e34 100644 --- a/data/src/main/java/com/linkedin/data/template/FieldDef.java +++ b/data/src/main/java/com/linkedin/data/template/FieldDef.java @@ -37,6 +37,7 @@ public class FieldDef private final DataSchema _dataSchema; private final Class _dataClass; private final RecordDataSchema.Field _field; + private Integer _hashCode; public FieldDef(String name, Class type) { @@ -126,6 +127,16 @@ public boolean equals(Object object) @Override public int hashCode() + { + if (_hashCode == null) { + // If this method is called by multiple thread, there might be multiple concurrent write + // here, but since the hashCode should be the same it is tolerable + _hashCode = computeHashCode(); + } + return _hashCode; + } + + private int computeHashCode() { return 13*_name.hashCode() + 17*_type.hashCode() + 23*(_dataSchema == null? 1 :_dataSchema.hashCode()); } diff --git a/gradle.properties b/gradle.properties index 836a948536..33ae9d13ae 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version=29.46.8 +version=29.46.9 group=com.linkedin.pegasus org.gradle.configureondemand=true org.gradle.parallel=true