Skip to content

Commit

Permalink
Let FieldDef lazy-calculate and cache hash code (#892)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
2 people authored and brycezhongqing committed Nov 13, 2023
1 parent db93223 commit ed33fc7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions data/src/main/java/com/linkedin/data/template/FieldDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class FieldDef<T>
private final DataSchema _dataSchema;
private final Class<?> _dataClass;
private final RecordDataSchema.Field _field;
private Integer _hashCode;

public FieldDef(String name, Class<T> type)
{
Expand Down Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=29.46.8
version=29.46.9
group=com.linkedin.pegasus
org.gradle.configureondemand=true
org.gradle.parallel=true
Expand Down

0 comments on commit ed33fc7

Please sign in to comment.