Skip to content

perf: add heap benchmark and reduce allocations #1156

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

Merged
merged 10 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions benchmark.txt

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.openfeature</groupId>
Expand All @@ -11,7 +11,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.11.2</junit.jupiter.version>
<!-- exclusion expression for e2e tests -->
<!-- exclusion expression for e2e tests -->
<testExclusions>**/e2e/*.java</testExclusions>
<module-name>${project.groupId}.${project.artifactId}</module-name>
</properties>
Expand Down Expand Up @@ -146,6 +146,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.37</version>
<scope>test</scope>
</dependency>

</dependencies>

<dependencyManagement>
Expand Down Expand Up @@ -473,7 +480,7 @@
<version>3.10.1</version>
<configuration>
<failOnWarnings>true</failOnWarnings>
<doclint>all,-missing</doclint> <!-- ignore missing javadoc, these are enforced with more customizability in the checkstyle plugin -->
<doclint>all,-missing</doclint> <!-- ignore missing javadoc, these are enforced with more customizability in the checkstyle plugin -->
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -507,6 +514,19 @@
</build>
</profile>

<profile>
<id>benchmark</id>
<build>
<plugins>
<plugin>
<groupId>pw.krejci</groupId>
<artifactId>jmh-maven-plugin</artifactId>
<version>0.2.2</version>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>e2e</id>
<properties>
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/dev/openfeature/sdk/AbstractStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
@SuppressWarnings({ "PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType" })
abstract class AbstractStructure implements Structure {

protected final Map<String, Value> attributes;
private Map<String, Value> attributes;

@Override
public boolean isEmpty() {
return attributes == null || attributes.size() == 0;
}

AbstractStructure() {
this.attributes = new HashMap<>();
// intentionally don't initialize the attributes - do this lazily
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opinion: non-major performance improvement vs. less code later, less readable and possibly less bug prone in fututre?
Some references, from first search results:
1
2
3
4

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted for: #1156 (comment)

}

AbstractStructure(Map<String, Value> attributes) {
Expand All @@ -32,4 +37,11 @@ public Map<String, Object> asObjectMap() {
(accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())),
HashMap::putAll);
}

protected Map<String, Value> getAttributes() {
if (attributes == null) {
attributes = new HashMap<>();
}
return attributes;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf: lazily initialize.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes it non thread safe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point. I've reverted this because it's not a even a substantial savings. The bench result barely changes.

}
59 changes: 27 additions & 32 deletions src/main/java/dev/openfeature/sdk/HookSupport.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package dev.openfeature.sdk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,11 +17,7 @@ class HookSupport {

public EvaluationContext beforeHooks(FlagValueType flagValueType, HookContext hookCtx, List<Hook> hooks,
Map<String, Object> hints) {
Stream<EvaluationContext> result = callBeforeHooks(flagValueType, hookCtx, hooks, hints);
return hookCtx.getCtx().merge(
result.reduce(hookCtx.getCtx(), (EvaluationContext accumulated, EvaluationContext current) -> {
return accumulated.merge(current);
}));
return callBeforeHooks(flagValueType, hookCtx, hooks, hints);
}

public void afterHooks(FlagValueType flagValueType, HookContext hookContext, FlagEvaluationDetails details,
Expand All @@ -46,10 +40,11 @@ private <T> void executeHooks(
String hookMethod,
Consumer<Hook<T>> hookCode) {
if (hooks != null) {
hooks
.stream()
.filter(hook -> hook.supportsFlagValueType(flagValueType))
.forEach(hook -> executeChecked(hook, hookCode, hookMethod));
for (Hook hook : hooks) {
if (hook.supportsFlagValueType(flagValueType)) {
executeChecked(hook, hookCode, hookMethod);
}
}
Comment on lines +43 to +47
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf: streams 😢

}
}

Expand All @@ -68,29 +63,29 @@ private <T> void executeHooksUnchecked(
FlagValueType flagValueType, List<Hook> hooks,
Consumer<Hook<T>> hookCode) {
if (hooks != null) {
hooks
.stream()
.filter(hook -> hook.supportsFlagValueType(flagValueType))
.forEach(hookCode::accept);
for (Hook hook : hooks) {
if (hook.supportsFlagValueType(flagValueType)) {
hookCode.accept(hook);
}
}
Comment on lines +66 to +70
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf: streams 😢

}
}

private Stream<EvaluationContext> callBeforeHooks(FlagValueType flagValueType, HookContext hookCtx,
private EvaluationContext callBeforeHooks(FlagValueType flagValueType, HookContext hookCtx,
List<Hook> hooks, Map<String, Object> hints) {
// These traverse backwards from normal.
List<Hook> reversedHooks = IntStream
.range(0, hooks.size())
.map(i -> hooks.size() - 1 - i)
.mapToObj(hooks::get)
.collect(Collectors.toList());

return reversedHooks
.stream()
.filter(hook -> hook.supportsFlagValueType(flagValueType))
.map(hook -> hook.before(hookCtx, hints))
.filter(Objects::nonNull)
.filter(Optional::isPresent)
.map(Optional::get)
.map(EvaluationContext.class::cast);
List<Hook> reversedHooks = new ArrayList<>(hooks);
Collections.reverse(reversedHooks);
EvaluationContext context = hookCtx.getCtx();
for (Hook hook : reversedHooks) {
if (hook.supportsFlagValueType(flagValueType)) {
Optional<EvaluationContext> optional = Optional.ofNullable(hook.before(hookCtx, hints))
.orElse(Optional.empty());
if (optional.isPresent()) {
context = context.merge(optional.get());
}
}
}
return context;
Comment on lines +77 to +89
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf: streams 😢

}
}
5 changes: 4 additions & 1 deletion src/main/java/dev/openfeature/sdk/ImmutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ public String getTargetingKey() {
*/
@Override
public EvaluationContext merge(EvaluationContext overridingContext) {
if (overridingContext == null) {
if (overridingContext == null || overridingContext.isEmpty()) {
return new ImmutableContext(this.asMap());
}
if (this.isEmpty()) {
return new ImmutableContext(overridingContext.asMap());
}

return new ImmutableContext(
this.merge(ImmutableStructure::new, this.asMap(), overridingContext.asMap()));
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/dev/openfeature/sdk/ImmutableStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -35,25 +36,18 @@ public ImmutableStructure() {
* @param attributes attributes.
*/
public ImmutableStructure(Map<String, Value> attributes) {
super(new HashMap<>(attributes.entrySet()
.stream()
.collect(HashMap::new,
(accumulated, entry) -> accumulated.put(entry.getKey(),
Optional.ofNullable(entry.getValue())
.map(Value::clone)
.orElse(null)),
HashMap::putAll)));
super(copyAttributes(attributes));
}

@Override
public Set<String> keySet() {
return new HashSet<>(this.attributes.keySet());
return new HashSet<>(this.getAttributes().keySet());
}

// getters
@Override
public Value getValue(String key) {
Value value = this.attributes.get(key);
Value value = getAttributes().get(key);
return value != null ? value.clone() : null;
}

Expand All @@ -64,14 +58,16 @@ public Value getValue(String key) {
*/
@Override
public Map<String, Value> asMap() {
return attributes
.entrySet()
.stream()
.collect(HashMap::new,
(accumulated, entry) -> accumulated.put(entry.getKey(),
Optional.ofNullable(entry.getValue())
.map(Value::clone)
.orElse(null)),
HashMap::putAll);
return copyAttributes(getAttributes());
}

private static Map<String, Value> copyAttributes(Map<String, Value> in) {
Map<String, Value> copy = new HashMap<>();
for (Entry<String, Value> entry : in.entrySet()) {
copy.put(entry.getKey(),
Optional.ofNullable(entry.getValue()).map((Value val) -> val.clone()).orElse(null));
}
return copy;
}
Comment on lines +64 to +71
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf: common, non-stream method to copy.


}
9 changes: 6 additions & 3 deletions src/main/java/dev/openfeature/sdk/MutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public MutableContext(String targetingKey, Map<String, Value> attributes) {
this.structure = new MutableStructure(attributes);
if (targetingKey != null && !targetingKey.trim().isEmpty()) {
this.structure.attributes.put(TARGETING_KEY, new Value(targetingKey));
this.structure.getAttributes().put(TARGETING_KEY, new Value(targetingKey));
}
}

Expand Down Expand Up @@ -114,8 +114,11 @@
*/
@Override
public EvaluationContext merge(EvaluationContext overridingContext) {
if (overridingContext == null) {
return new MutableContext(this.asMap());
if (overridingContext == null || overridingContext.isEmpty()) {
return this;
}
if (this.isEmpty()) {
return overridingContext;

Check warning on line 121 in src/main/java/dev/openfeature/sdk/MutableContext.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/dev/openfeature/sdk/MutableContext.java#L121

Added line #L121 was not covered by tests
Comment on lines +117 to +121
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf: quit early with less HashMap instantiations

}

Map<String, Value> merged = this.merge(
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/dev/openfeature/sdk/MutableStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,53 @@ public MutableStructure(Map<String, Value> attributes) {

@Override
public Set<String> keySet() {
return this.attributes.keySet();
return getAttributes().keySet();
}

// getters
@Override
public Value getValue(String key) {
return this.attributes.get(key);
return getAttributes().get(key);
}

// adders
public MutableStructure add(String key, Value value) {
attributes.put(key, value);
getAttributes().put(key, value);
return this;
}

public MutableStructure add(String key, Boolean value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

public MutableStructure add(String key, String value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

public MutableStructure add(String key, Integer value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

public MutableStructure add(String key, Double value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

public MutableStructure add(String key, Instant value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

public MutableStructure add(String key, Structure value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

public MutableStructure add(String key, List<Value> value) {
attributes.put(key, new Value(value));
getAttributes().put(key, new Value(value));
return this;
}

Expand All @@ -87,6 +87,6 @@ public MutableStructure add(String key, List<Value> value) {
*/
@Override
public Map<String, Value> asMap() {
return new HashMap<>(this.attributes);
return new HashMap<>(getAttributes());
}
}
15 changes: 14 additions & 1 deletion src/main/java/dev/openfeature/sdk/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
public interface Structure {

/**
* Boolean indicating if this structure is empty.
* @return boolean for emptiness
*/
boolean isEmpty();

/**
* Get all keys.
*
Expand Down Expand Up @@ -113,7 +119,14 @@
default <T extends Structure> Map<String, Value> merge(Function<Map<String, Value>, Structure> newStructure,
Map<String, Value> base,
Map<String, Value> overriding) {


if (base.isEmpty()) {
return overriding;

Check warning on line 124 in src/main/java/dev/openfeature/sdk/Structure.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/dev/openfeature/sdk/Structure.java#L124

Added line #L124 was not covered by tests
}
if (overriding.isEmpty()) {
return base;

Check warning on line 127 in src/main/java/dev/openfeature/sdk/Structure.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/dev/openfeature/sdk/Structure.java#L127

Added line #L127 was not covered by tests
}

final Map<String, Value> merged = new HashMap<>(base);
for (Entry<String, Value> overridingEntry : overriding.entrySet()) {
String key = overridingEntry.getKey();
Expand Down
Loading
Loading