Copilot commented on code in PR #15633:
URL: https://github.com/apache/dubbo/pull/15633#discussion_r2280321841
##########
dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/DefaultServiceInstance.java:
##########
@@ -305,36 +311,39 @@ public boolean equals(Object o) {
return false;
}
DefaultServiceInstance that = (DefaultServiceInstance) o;
- boolean equals = Objects.equals(getServiceName(),
that.getServiceName())
- && Objects.equals(getHost(), that.getHost())
- && Objects.equals(getPort(), that.getPort());
- for (Map.Entry<String, String> entry : this.getMetadata().entrySet()) {
- if
(entry.getKey().equals(EXPORTED_SERVICES_REVISION_PROPERTY_NAME)) {
- continue;
- }
- if (entry.getValue() == null) {
- equals = equals && (entry.getValue() ==
that.getMetadata().get(entry.getKey()));
- } else {
- equals = equals &&
entry.getValue().equals(that.getMetadata().get(entry.getKey()));
- }
+ if (!Objects.equals(getServiceName(), that.getServiceName())
+ || !Objects.equals(getHost(), that.getHost())
+ || !Objects.equals(getPort(), that.getPort())) {
+ return false;
}
- return equals;
+ // compare filtered metadata.
+ return getFilteredMetadata().equals(that.getFilteredMetadata());
}
@Override
public int hashCode() {
int result = Objects.hash(getServiceName(), getHost(), getPort());
- for (Map.Entry<String, String> entry : this.getMetadata().entrySet()) {
- if
(entry.getKey().equals(EXPORTED_SERVICES_REVISION_PROPERTY_NAME)) {
- continue;
- }
+
+ // Calculate filtered metadata to ensures the hashCode is consistent
with equals result.
+ for (Map.Entry<String, String> entry :
this.getFilteredMetadata().entrySet()) {
result = 31 * result
+ (entry.getValue() == null ? 0 :
entry.getValue().hashCode());
}
return result;
}
+ /**
+ * Get filtered metadata
+ * @return filtered metadata, a sorted map which excludes revision and
timestamp keys.
+ */
+ private SortedMap<String, String> getFilteredMetadata() {
+ TreeMap<String, String> filteredMetadata = new TreeMap<>(metadata);
+ filteredMetadata.remove(EXPORTED_SERVICES_REVISION_PROPERTY_NAME);
+ filteredMetadata.remove(TIMESTAMP_KEY);
+ return filteredMetadata;
Review Comment:
Creating a new TreeMap copy in getFilteredMetadata() for every
equals/hashCode call could impact performance. Consider caching the filtered
metadata or using a more efficient filtering approach, especially since this
method will be called frequently during collection operations.
```suggestion
SortedMap<String, String> cache = filteredMetadataCache;
if (cache == null) {
synchronized (this) {
cache = filteredMetadataCache;
if (cache == null) {
TreeMap<String, String> filteredMetadata = new
TreeMap<>(metadata);
filteredMetadata.remove(EXPORTED_SERVICES_REVISION_PROPERTY_NAME);
filteredMetadata.remove(TIMESTAMP_KEY);
cache =
Collections.unmodifiableSortedMap(filteredMetadata);
filteredMetadataCache = cache;
}
}
}
return cache;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]