Skip to content

perf: optimize copy on write attributes #74

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 3 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.launchdarkly.sdk;

import java.util.HashMap;
import java.util.Map;

final class AttributeMap {
private final AttributeMap parent;
private final Map<String, LDValue> map;

AttributeMap() {
this(null);
}

AttributeMap(AttributeMap parent) {
this.parent = parent;
this.map = new HashMap<>();
}

LDValue get(String key) {
AttributeMap current = this;
while (current != null) {
LDValue value = current.map.get(key);
if (value != null) {
if (value.isNull()) {
break;
}
return value;
}
current = current.parent;
}
return null;
}

void put(String key, LDValue value) {
map.put(key, value);
}

@Override
public int hashCode() {
return flatten().hashCode();
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof AttributeMap)) {
return false;
}
AttributeMap o = (AttributeMap) other;
return flatten().equals(o.flatten());
}

Map<String, LDValue> flatten() {
if (parent == null) {
return map;
}
Map<String, LDValue> out = new HashMap<>();
flattenRecursive(out);
return out;
}

private void flattenRecursive(Map<String, LDValue> out) {
if (parent != null) {
parent.flattenRecursive(out);
}
for (Map.Entry<String, LDValue> entry : map.entrySet()) {
String key = entry.getKey();
LDValue value = entry.getValue();
if (value.isNull()) {
out.remove(key);
} else {
out.put(key, value);
}
}
}

void remove(String key) {
if (parent == null) {
map.remove(key);
return;
}
// we need to hide the value from the parents
map.put(key, LDValue.ofNull());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.launchdarkly.sdk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* A mutable object that uses the builder pattern to specify properties for {@link LDContext}.
Expand Down Expand Up @@ -34,7 +32,7 @@ public final class ContextBuilder {
private ContextKind kind;
private String key;
private String name;
private Map<String, LDValue> attributes;
private AttributeMap attributes;
private boolean anonymous;
private List<AttributeRef> privateAttributes;
private boolean copyOnWriteAttributes;
Expand Down Expand Up @@ -303,7 +301,7 @@ public boolean trySet(String attributeName, LDValue value) {
return false;
default:
if (copyOnWriteAttributes) {
attributes = new HashMap<>(attributes);
attributes = new AttributeMap(attributes);
copyOnWriteAttributes = false;
}
if (value == null || value.isNull()) {
Expand All @@ -312,7 +310,7 @@ public boolean trySet(String attributeName, LDValue value) {
}
} else {
if (attributes == null) {
attributes = new HashMap<>();
attributes = new AttributeMap();
}
attributes.put(attributeName, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class LDContext implements JsonSerializable {
final String key;
final String fullyQualifiedKey;
final String name;
final Map<String, LDValue> attributes;
final AttributeMap attributes;
final boolean anonymous;
final List<AttributeRef> privateAttributes;

Expand All @@ -68,7 +68,7 @@ private LDContext(
String key,
String fullyQualifiedKey,
String name,
Map<String, LDValue> attributes,
AttributeMap attributes,
boolean anonymous,
List<AttributeRef> privateAttributes
) {
Expand Down Expand Up @@ -100,7 +100,7 @@ static LDContext createSingle(
ContextKind kind,
String key,
String name,
Map<String, LDValue> attributes,
AttributeMap attributes,
boolean anonymous,
List<AttributeRef> privateAttributes,
boolean allowEmptyKey // allowEmptyKey is true only when deserializing old-style user JSON
Expand Down Expand Up @@ -300,22 +300,22 @@ public static LDContext fromUser(LDUser user) {
return failed(Errors.CONTEXT_NO_KEY);
}
}
Map<String, LDValue> attributes = null;
AttributeMap attributes = null;
for (UserAttribute a: UserAttribute.OPTIONAL_STRING_ATTRIBUTES) {
if (a == UserAttribute.NAME) {
continue;
}
LDValue value = user.getAttribute(a);
if (!value.isNull()) {
if (attributes == null) {
attributes = new HashMap<>();
attributes = new AttributeMap();
}
attributes.put(a.getName(), value);
}
}
if (user.custom != null && !user.custom.isEmpty()) {
if (attributes == null) {
attributes = new HashMap<>();
attributes = new AttributeMap();
}
for (Map.Entry<UserAttribute, LDValue> kv: user.custom.entrySet()) {
attributes.put(kv.getKey().getName(), kv.getValue());
Expand Down Expand Up @@ -671,7 +671,7 @@ public LDValue getValue(AttributeRef attributeRef) {
* @return an iterable of strings (may be empty, but will never be null)
*/
public Iterable<String> getCustomAttributeNames() {
return attributes == null ? Collections.<String>emptyList() : attributes.keySet();
return attributes == null ? Collections.<String>emptyList() : attributes.flatten().keySet();
}

/**
Expand Down Expand Up @@ -853,17 +853,9 @@ public boolean equals(Object other) {
if (!Objects.equals(key, o.key) || !Objects.equals(name, o.name) || anonymous != o.anonymous) {
return false;
}
if ((attributes == null ? 0 : attributes.size()) !=
(o.attributes == null ? 0 : o.attributes.size())) {
if (!Objects.equals(attributes, o.attributes)) {
return false;
}
if (attributes != null) {
for (Map.Entry<String, LDValue> kv: attributes.entrySet()) {
if (!Objects.equals(o.attributes.get(kv.getKey()), kv.getValue())) {
return false;
}
}
}
if (getPrivateAttributeCount() != o.getPrivateAttributeCount()) {
return false;
}
Expand All @@ -886,22 +878,16 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
// This implementation of hashCode() is inefficient due to the need to create a predictable ordering
// of attribute names. That's necessary just for the sake of aligning with the behavior of equals(),
// which is insensitive to ordering. However, using an LDContext as a map key is not an anticipated
// or recommended use case.
// This implementation of hashCode() is inefficient due to the need to flatten the attributes map.
// However, using an LDContext as a map key is not an anticipated or recommended use case.
int h = Objects.hash(error, kind, key, name, anonymous);
if (multiContexts != null) {
for (LDContext c: multiContexts) {
h = h * 17 + c.hashCode();
}
}
if (attributes != null) {
String[] names = attributes.keySet().toArray(new String[attributes.size()]);
Arrays.sort(names);
for (String name: names) {
h = (h * 17 + name.hashCode()) * 17 + attributes.get(name).hashCode();
}
h = h * 17 + attributes.hashCode();
}
if (privateAttributes != null) {
AttributeRef[] refs = privateAttributes.toArray(new AttributeRef[privateAttributes.size()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void writeSingleKind(JsonWriter out, LDContext c, boolean includeKind) t
out.name(ATTR_ANONYMOUS).value(c.isAnonymous());
}
if (c.attributes != null) {
for (Map.Entry<String, LDValue> kv: c.attributes.entrySet()) {
for (Map.Entry<String, LDValue> kv: c.attributes.flatten().entrySet()) {
out.name(kv.getKey());
LDValueTypeAdapter.INSTANCE.write(out, kv.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.launchdarkly.sdk.LDContextTest.kind1;
import static com.launchdarkly.sdk.LDContextTest.kind2;
Expand Down Expand Up @@ -75,6 +77,35 @@ public void copyOnWriteAttributes() {
assertThat(c1.getValue("b"), equalTo(LDValue.ofNull()));
assertThat(c2.getValue("a"), equalTo(LDValue.of(2)));
assertThat(c2.getValue("b"), equalTo(LDValue.of(3)));
Map<String, LDValue> c1Map = new HashMap<>();
c1Map.put("a", LDValue.of(1));
assertThat(c1.attributes.flatten(), equalTo(c1Map));
Map<String, LDValue> c2Map = new HashMap<>();
c2Map.put("a", LDValue.of(2));
c2Map.put("b", LDValue.of(3));
assertThat(c2.attributes.flatten(), equalTo(c2Map));
}


@Test
public void copyOnWriteAttributes2() {
LDContext c1 = LDContext.builder("key").set("a", 1).set("c", LDValue.ofNull()).set("fsdf", 12).build();
LDContext c2 = LDContext.builderFromContext(c1).set("a", 2).set("b", 3).set("fsdf", LDValue.ofNull()).build();

assertThat(c1.getValue("a"), equalTo(LDValue.of(1)));
assertThat(c1.getValue("b"), equalTo(LDValue.ofNull()));
assertThat(c1.getValue("fsdf"), equalTo(LDValue.of(12)));
assertThat(c2.getValue("a"), equalTo(LDValue.of(2)));
assertThat(c2.getValue("b"), equalTo(LDValue.of(3)));
assertThat(c2.getValue("fsdf"), equalTo(LDValue.ofNull()));
Map<String, LDValue> c1Map = new HashMap<>();
c1Map.put("a", LDValue.of(1));
c1Map.put("fsdf", LDValue.of(12));
assertThat(c1.attributes.flatten(), equalTo(c1Map));
Map<String, LDValue> c2Map = new HashMap<>();
c2Map.put("a", LDValue.of(2));
c2Map.put("b", LDValue.of(3));
assertThat(c2.attributes.flatten(), equalTo(c2Map));
}

@Test
Expand Down