-
Notifications
You must be signed in to change notification settings - Fork 52
fix: Define equality behavior for EvaluationContext implementations #1756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,6 +251,24 @@ public Map<String, Object> asObjectMap() { | |
| return map; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof EvaluationContext)) { | ||
| return false; | ||
| } | ||
|
|
||
| EvaluationContext that = (EvaluationContext) o; | ||
| return this.asMap().equals(that.asMap()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return asMap().hashCode(); | ||
| } | ||
|
Comment on lines
+255
to
+270
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic here is correct, but it has a significant performance implication. The I recommend caching the resolved map. The cache can be invalidated in Here's a potential refactoring:
Example: // Add this field
private Map<String, Value> cachedMap;
// New helper method
private Map<String, Value> getResolvedMap() {
if (cachedMap == null) {
// logic from asMap() to build the map
HashMap<String, Value> map = new HashMap<>();
// ... build map ...
cachedMap = map;
}
return cachedMap;
}
@Override
public Map<String, Value> asMap() {
// Return a copy to prevent mutation of the cached map
return new HashMap<>(getResolvedMap());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof EvaluationContext)) {
return false;
}
EvaluationContext that = (EvaluationContext) o;
return this.getResolvedMap().equals(that.asMap());
}
@Override
public int hashCode() {
return getResolvedMap().hashCode();
}
void putHookContext(EvaluationContext context) {
// ... existing logic
this.cachedMap = null; // Invalidate cache
this.keySet = null;
}This change would greatly improve the performance of |
||
|
|
||
| void putHookContext(EvaluationContext context) { | ||
| if (context == null || context.isEmpty()) { | ||
| return; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is correct, but
asMap()may be an expensive call if this object is used frequently in hash-based collections. SinceImmutableContextis immutable, you could cache the hash code after its first computation to improve performance. This is a common optimization for immutable objects.For example, you could introduce a
private volatile Integer cachedHashCode;field and use lazy initialization in a thread-safe way: