Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 45e664b

Browse files
committed
Eliminate excessive JsonPrimitive construction in an attempt to avoid performance issues with TargetRule matching
1 parent 2f7adbc commit 45e664b

File tree

5 files changed

+52
-49
lines changed

5 files changed

+52
-49
lines changed

src/main/java/com/launchdarkly/client/FeatureRep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private Float paramForUser(LDUser user) {
7878
String hash;
7979

8080
if (user.getKey() != null) {
81-
idHash = user.getKey();
81+
idHash = user.getKey().getAsString();
8282
}
8383
else {
8484
return null;

src/main/java/com/launchdarkly/client/IdentifyEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
class IdentifyEvent extends Event {
66

77
IdentifyEvent(LDUser user) {
8-
super("identify", user.getKey(), user);
8+
super("identify", user.getKey().getAsString(), user);
99
}
1010
}

src/main/java/com/launchdarkly/client/LDUser.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
* launch a feature to the top 10% of users on a site.
2525
*/
2626
public class LDUser {
27-
private String key;
28-
private String secondary;
29-
private String ip;
30-
private String email;
31-
private String name;
32-
private String avatar;
33-
private String firstName;
34-
private String lastName;
35-
private Boolean anonymous;
36-
37-
private LDCountryCode country;
27+
private JsonPrimitive key;
28+
private JsonPrimitive secondary;
29+
private JsonPrimitive ip;
30+
private JsonPrimitive email;
31+
private JsonPrimitive name;
32+
private JsonPrimitive avatar;
33+
private JsonPrimitive firstName;
34+
private JsonPrimitive lastName;
35+
private JsonPrimitive anonymous;
36+
37+
private JsonPrimitive country;
3838
private Map<String, JsonElement> custom;
3939
private static final Logger logger = LoggerFactory.getLogger(LDUser.class);
4040

@@ -44,16 +44,16 @@ public class LDUser {
4444
}
4545

4646
protected LDUser(Builder builder) {
47-
this.key = builder.key;
48-
this.ip = builder.ip;
49-
this.country = builder.country;
50-
this.secondary = builder.secondary;
51-
this.firstName = builder.firstName;
52-
this.lastName = builder.lastName;
53-
this.email = builder.email;
54-
this.name = builder.name;
55-
this.avatar = builder.avatar;
56-
this.anonymous = builder.anonymous;
47+
this.key = builder.key == null? null : new JsonPrimitive(builder.key);
48+
this.ip = builder.ip == null? null : new JsonPrimitive(builder.ip);
49+
this.country = builder.country == null? null : new JsonPrimitive(builder.country.getAlpha2());
50+
this.secondary = builder.secondary == null ? null : new JsonPrimitive(builder.secondary);
51+
this.firstName = builder.firstName == null ? null : new JsonPrimitive(builder.firstName);
52+
this.lastName = builder.lastName == null ? null : new JsonPrimitive(builder.lastName);
53+
this.email = builder.email == null ? null : new JsonPrimitive(builder.email);
54+
this.name = builder.name == null ? null : new JsonPrimitive(builder.name);
55+
this.avatar = builder.avatar == null ? null : new JsonPrimitive(builder.avatar);
56+
this.anonymous = builder.anonymous == null ? null : new JsonPrimitive(builder.anonymous);
5757
this.custom = new HashMap<String, JsonElement>(builder.custom);
5858
}
5959

@@ -62,31 +62,31 @@ protected LDUser(Builder builder) {
6262
* @param key a {@code String} that uniquely identifies a user
6363
*/
6464
public LDUser(String key) {
65-
this.key = key;
65+
this.key = new JsonPrimitive(key);
6666
this.custom = new HashMap<String, JsonElement>();
6767
}
6868

69-
String getKey() {
69+
JsonPrimitive getKey() {
7070
return key;
7171
}
7272

73-
String getIp() { return ip; }
73+
JsonPrimitive getIp() { return ip; }
7474

75-
LDCountryCode getCountry() { return country; }
75+
JsonPrimitive getCountry() { return country; }
7676

77-
String getSecondary() { return secondary; }
77+
JsonPrimitive getSecondary() { return secondary; }
7878

79-
String getName() { return name; }
79+
JsonPrimitive getName() { return name; }
8080

81-
String getFirstName() { return firstName; }
81+
JsonPrimitive getFirstName() { return firstName; }
8282

83-
String getLastName() { return lastName; }
83+
JsonPrimitive getLastName() { return lastName; }
8484

85-
String getEmail() { return email; }
85+
JsonPrimitive getEmail() { return email; }
8686

87-
String getAvatar() { return avatar; }
87+
JsonPrimitive getAvatar() { return avatar; }
8888

89-
Boolean getAnonymous() { return anonymous; }
89+
JsonPrimitive getAnonymous() { return anonymous; }
9090

9191
JsonElement getCustom(String key) {
9292
return custom.get(key);

src/main/java/com/launchdarkly/client/Variation.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,47 +137,47 @@ public boolean matchTarget(LDUser user) {
137137
Object uValue = null;
138138
if (attribute.equals("key")) {
139139
if (user.getKey() != null) {
140-
uValue = new JsonPrimitive(user.getKey());
140+
uValue = user.getKey();
141141
}
142142
}
143143
else if (attribute.equals("ip") && user.getIp() != null) {
144144
if (user.getIp() != null) {
145-
uValue = new JsonPrimitive(user.getIp());
145+
uValue = user.getIp();
146146
}
147147
}
148148
else if (attribute.equals("country")) {
149149
if (user.getCountry() != null) {
150-
uValue = new JsonPrimitive(user.getCountry().getAlpha2());
150+
uValue = user.getCountry();
151151
}
152152
}
153153
else if (attribute.equals("email")) {
154154
if (user.getEmail() != null) {
155-
uValue = new JsonPrimitive(user.getEmail());
155+
uValue = user.getEmail();
156156
}
157157
}
158158
else if (attribute.equals("firstName")) {
159159
if (user.getFirstName() != null ) {
160-
uValue = new JsonPrimitive(user.getFirstName());
160+
uValue = user.getFirstName();
161161
}
162162
}
163163
else if (attribute.equals("lastName")) {
164164
if (user.getLastName() != null) {
165-
uValue = new JsonPrimitive(user.getLastName());
165+
uValue = user.getLastName();
166166
}
167167
}
168168
else if (attribute.equals("avatar")) {
169169
if (user.getAvatar() != null) {
170-
uValue = new JsonPrimitive(user.getAvatar());
170+
uValue = user.getAvatar();
171171
}
172172
}
173173
else if (attribute.equals("name")) {
174174
if (user.getName() != null) {
175-
uValue = new JsonPrimitive(user.getName());
175+
uValue = user.getName();
176176
}
177177
}
178178
else if (attribute.equals("anonymous")) {
179179
if (user.getAnonymous() != null) {
180-
uValue = new JsonPrimitive(user.getAnonymous());
180+
uValue = user.getAnonymous();
181181
}
182182
}
183183
else { // Custom attribute

src/test/java/com/launchdarkly/client/LDUserTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
package com.launchdarkly.client;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonPrimitive;
45
import org.junit.Test;
56

67
public class LDUserTest {
78

9+
private JsonPrimitive us = new JsonPrimitive(LDCountryCode.US.getAlpha2());
10+
811
@Test
912
public void testValidCountryCodeSetsCountry() {
1013
LDUser user = new LDUser.Builder("key").country(LDCountryCode.US).build();
1114

12-
assert(user.getCountry().equals(LDCountryCode.US));
15+
assert(user.getCountry().equals(us));
1316
}
1417

1518

1619
@Test
1720
public void testValidCountryCodeStringSetsCountry() {
1821
LDUser user = new LDUser.Builder("key").country("US").build();
1922

20-
assert(user.getCountry().equals(LDCountryCode.US));
23+
assert(user.getCountry().equals(us));
2124
}
2225

2326
@Test
2427
public void testValidCountryCode3SetsCountry() {
2528
LDUser user = new LDUser.Builder("key").country("USA").build();
2629

27-
assert(user.getCountry().equals(LDCountryCode.US));
30+
assert(user.getCountry().equals(us));
2831
}
2932

3033
@Test
3134
public void testAmbiguousCountryNameSetsCountryWithExactMatch() {
3235
// "United States" is ambiguous: can also match "United States Minor Outlying Islands"
3336
LDUser user = new LDUser.Builder("key").country("United States").build();
34-
assert(user.getCountry().equals(LDCountryCode.US));
37+
assert(user.getCountry().equals(us));
3538
}
3639

3740
@Test
@@ -45,7 +48,7 @@ public void testAmbiguousCountryNameSetsCountryWithPartialMatch() {
4548
@Test
4649
public void testPartialUniqueMatchSetsCountry() {
4750
LDUser user = new LDUser.Builder("key").country("United States Minor").build();
48-
assert(user.getCountry().equals(LDCountryCode.UM));
51+
assert(user.getCountry().equals(new JsonPrimitive(LDCountryCode.UM.getAlpha2())));
4952
}
5053

5154
@Test
@@ -63,6 +66,6 @@ public void testLDUserJsonSerializationContainsCountryAsTwoDigitCode() {
6366

6467
LDUser deserialized = gson.fromJson(jsonStr, LDUser.class);
6568

66-
assert(deserialized.getCountry().equals(LDCountryCode.US));
69+
assert(deserialized.getCountry().equals(us));
6770
}
6871
}

0 commit comments

Comments
 (0)