Unverified Commit f1f72459 by Jason Song Committed by GitHub

Merge pull request #1072 from wuwen5/properties#preform

优化Properties#stringPropertyNames处理,jdk9以下版本此方法cpu消耗相对过高.
parents c4946900 96efd8ff
...@@ -8,6 +8,7 @@ import java.util.Map; ...@@ -8,6 +8,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -101,7 +102,20 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis ...@@ -101,7 +102,20 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
return Collections.emptySet(); return Collections.emptySet();
} }
return properties.stringPropertyNames(); return stringPropertyNames(properties);
}
private Set<String> stringPropertyNames(Properties properties) {
//jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历.
Map<String, String> h = new HashMap<>();
for (Map.Entry<Object, Object> e : properties.entrySet()) {
Object k = e.getKey();
Object v = e.getValue();
if (k instanceof String && v instanceof String) {
h.put((String) k, (String) v);
}
}
return h.keySet();
} }
@Override @Override
......
...@@ -11,6 +11,8 @@ import java.io.File; ...@@ -11,6 +11,8 @@ import java.io.File;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.Collections;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.After; import org.junit.After;
...@@ -645,6 +647,39 @@ public class DefaultConfigTest { ...@@ -645,6 +647,39 @@ public class DefaultConfigTest {
assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType()); assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType());
} }
@Test
public void testGetPropertyNames() {
String someKeyPrefix = "someKey";
String someValuePrefix = "someValue";
//set up config repo
someProperties = new Properties();
for (int i = 0; i < 10; i++) {
someProperties.setProperty(someKeyPrefix + i, someValuePrefix + i);
}
when(configRepository.getConfig()).thenReturn(someProperties);
DefaultConfig defaultConfig =
new DefaultConfig(someNamespace, configRepository);
Set<String> propertyNames = defaultConfig.getPropertyNames();
assertEquals(10, propertyNames.size());
assertEquals(someProperties.stringPropertyNames(), propertyNames);
}
@Test
public void testGetPropertyNamesWithNullProp() {
when(configRepository.getConfig()).thenReturn(null);
DefaultConfig defaultConfig =
new DefaultConfig(someNamespace, configRepository);
Set<String> propertyNames = defaultConfig.getPropertyNames();
assertEquals(Collections.emptySet(), propertyNames);
}
private void checkDatePropertyWithFormat(Config config, Date expected, String propertyName, String format, Date private void checkDatePropertyWithFormat(Config config, Date expected, String propertyName, String format, Date
defaultValue) { defaultValue) {
assertEquals(expected, config.getDateProperty(propertyName, format, defaultValue)); assertEquals(expected, config.getDateProperty(propertyName, format, defaultValue));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment