-
Notifications
You must be signed in to change notification settings - Fork 38
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
perf: reduce hashmap allocations #1178
Changes from all commits
896df53
6a879b1
5cd181f
bddf7f2
e9c251c
0bddca9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,16 @@ | |
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import dev.openfeature.sdk.internal.ExcludeFromGeneratedCoverageReport; | ||
import lombok.ToString; | ||
import lombok.experimental.Delegate; | ||
|
||
/** | ||
* The EvaluationContext is a container for arbitrary contextual data | ||
* that can be used as a basis for dynamic evaluation. | ||
* The ImmutableContext is an EvaluationContext implementation which is threadsafe, and whose attributes can | ||
* The ImmutableContext is an EvaluationContext implementation which is | ||
* threadsafe, and whose attributes can | ||
* not be modified after instantiation. | ||
*/ | ||
@ToString | ||
|
@@ -21,7 +23,8 @@ | |
private final ImmutableStructure structure; | ||
|
||
/** | ||
* Create an immutable context with an empty targeting_key and attributes provided. | ||
* Create an immutable context with an empty targeting_key and attributes | ||
* provided. | ||
*/ | ||
public ImmutableContext() { | ||
this(new HashMap<>()); | ||
|
@@ -42,7 +45,7 @@ | |
* @param attributes evaluation context attributes | ||
*/ | ||
public ImmutableContext(Map<String, Value> attributes) { | ||
this("", attributes); | ||
this(null, attributes); | ||
} | ||
|
||
/** | ||
|
@@ -53,9 +56,7 @@ | |
*/ | ||
public ImmutableContext(String targetingKey, Map<String, Value> attributes) { | ||
if (targetingKey != null && !targetingKey.trim().isEmpty()) { | ||
final Map<String, Value> actualAttribs = new HashMap<>(attributes); | ||
actualAttribs.put(TARGETING_KEY, new Value(targetingKey)); | ||
this.structure = new ImmutableStructure(actualAttribs); | ||
this.structure = new ImmutableStructure(targetingKey, attributes); | ||
} else { | ||
this.structure = new ImmutableStructure(attributes); | ||
} | ||
|
@@ -71,31 +72,33 @@ | |
} | ||
|
||
/** | ||
* Merges this EvaluationContext object with the passed EvaluationContext, overriding in case of conflict. | ||
* Merges this EvaluationContext object with the passed EvaluationContext, | ||
* overriding in case of conflict. | ||
* | ||
* @param overridingContext overriding context | ||
* @return new, resulting merged context | ||
*/ | ||
@Override | ||
public EvaluationContext merge(EvaluationContext overridingContext) { | ||
if (overridingContext == null || overridingContext.isEmpty()) { | ||
return new ImmutableContext(this.asMap()); | ||
return new ImmutableContext(this.asUnmodifiableMap()); | ||
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. If we're already in an This goes in the direction @ssharaev suggested in https://github.com/open-feature/java-sdk/pull/1178/files#r1811813866 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. Because then it's not a new instance, and I think always returning a new object from this method is less surprising than sometimes returning the same one. |
||
} | ||
if (this.isEmpty()) { | ||
return new ImmutableContext(overridingContext.asMap()); | ||
return new ImmutableContext(overridingContext.asUnmodifiableMap()); | ||
} | ||
|
||
return new ImmutableContext( | ||
this.merge(ImmutableStructure::new, this.asMap(), overridingContext.asMap())); | ||
Map<String, Value> attributes = this.asMap(); | ||
EvaluationContext.mergeMaps(ImmutableStructure::new, attributes, | ||
overridingContext.asUnmodifiableMap()); | ||
return new ImmutableContext(attributes); | ||
} | ||
|
||
@SuppressWarnings("all") | ||
private static class DelegateExclusions { | ||
@ExcludeFromGeneratedCoverageReport | ||
public <T extends Structure> Map<String, Value> merge(Function<Map<String, Value>, Structure> newStructure, | ||
public <T extends Structure> Map<String, Value> merge(Function<Map<String, Value>, Structure> newStructure, | ||
Map<String, Value> base, | ||
Map<String, Value> overriding) { | ||
|
||
return null; | ||
} | ||
} | ||
|
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.
Is it worth to check if this is an unmodifiable nap, before wrapping it?
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.
According to SO,
Collections.unmodifiableMap
already does this.