Skip to content

Commit 97d9c4c

Browse files
authored
Optimizations for TypeSafeDriverConfig (#1616)
* TypesafeDriverConfig: getProfile can avoid calling containsKey On a typical applicative workload, 0.53% of CPU is spend resolving configuration profiles. By getting the profile first and failing if it is null we can easily cut that in half. * TypesafeDriverConfig: optimize getDefaultProfile We could easily get a dedicated field for the default profile thus avoiding recurring maps lookups. * fixup! TypesafeDriverConfig: optimize getDefaultProfile
1 parent a40bbc2 commit 97d9c4c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/config/typesafe/TypesafeDriverConfig.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import edu.umd.cs.findbugs.annotations.NonNull;
3333
import java.net.URL;
3434
import java.util.Map;
35+
import java.util.Optional;
3536
import java.util.concurrent.ConcurrentHashMap;
3637
import net.jcip.annotations.ThreadSafe;
3738
import org.slf4j.Logger;
@@ -50,6 +51,8 @@ public class TypesafeDriverConfig implements DriverConfig {
5051

5152
private final Map<DriverOption, Object> defaultOverrides = new ConcurrentHashMap<>();
5253

54+
private final TypesafeDriverExecutionProfile.Base defaultProfile;
55+
5356
public TypesafeDriverConfig(Config config) {
5457
this.lastLoadedConfig = config;
5558
Map<String, Config> profileConfigs = extractProfiles(config);
@@ -62,6 +65,7 @@ public TypesafeDriverConfig(Config config) {
6265
new TypesafeDriverExecutionProfile.Base(entry.getKey(), entry.getValue()));
6366
}
6467
this.profiles = builder.build();
68+
this.defaultProfile = profiles.get(DriverExecutionProfile.DEFAULT_NAME);
6569
}
6670

6771
/** @return whether the configuration changed */
@@ -136,14 +140,19 @@ private Map<String, Config> extractProfiles(Config sourceConfig) {
136140
return result.build();
137141
}
138142

143+
@Override
144+
public DriverExecutionProfile getDefaultProfile() {
145+
return defaultProfile;
146+
}
147+
139148
@NonNull
140149
@Override
141150
public DriverExecutionProfile getProfile(@NonNull String profileName) {
142-
Preconditions.checkArgument(
143-
profiles.containsKey(profileName),
144-
"Unknown profile '%s'. Check your configuration.",
145-
profileName);
146-
return profiles.get(profileName);
151+
if (profileName.equals(DriverExecutionProfile.DEFAULT_NAME)) {
152+
return defaultProfile;
153+
}
154+
return Optional.ofNullable(profiles.get(profileName))
155+
.orElseThrow(() -> new IllegalArgumentException(String.format("Unknown profile '%s'. Check your configuration.", profileName)));
147156
}
148157

149158
@NonNull

core/src/test/java/com/datastax/oss/driver/internal/core/config/typesafe/TypesafeDriverConfigTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ public void should_enumerate_options() {
171171
entry("int1", 45));
172172
}
173173

174+
@Test
175+
public void should_update_default_profile_on_reload() {
176+
TypesafeDriverConfig config = parse("int1 = 42\n profiles { profile1 { int1 = 43 } }");
177+
assertThat(config.getDefaultProfile().getInt(MockOptions.INT1)).isEqualTo(42);
178+
config.reload(ConfigFactory.parseString("int1 = 44\n profiles { profile1 { int1 = 45 } }"));
179+
assertThat(config.getDefaultProfile().getInt(MockOptions.INT1)).isEqualTo(44);
180+
}
181+
174182
private TypesafeDriverConfig parse(String configString) {
175183
Config config = ConfigFactory.parseString(configString);
176184
return new TypesafeDriverConfig(config);

0 commit comments

Comments
 (0)