Polish "Improve performance of CompositePropertySource#getPropertyNames"
See gh-27236
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,6 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
@@ -34,6 +33,9 @@ import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import org.springframework.util.AlternativeJdkIdGenerator;
|
||||
import org.springframework.util.IdGenerator;
|
||||
|
||||
/**
|
||||
* Benchmarks for {@link CompositePropertySource}.
|
||||
*
|
||||
@@ -53,14 +55,16 @@ public class CompositePropertySourceBenchmark {
|
||||
@State(Scope.Benchmark)
|
||||
public static class BenchmarkState {
|
||||
|
||||
static final Object VALUE = new Object();
|
||||
private static final IdGenerator ID_GENERATOR = new AlternativeJdkIdGenerator();
|
||||
|
||||
private static final Object VALUE = new Object();
|
||||
|
||||
CompositePropertySource composite;
|
||||
|
||||
@Param({"2", "5", "10"})
|
||||
@Param({ "2", "5", "10" })
|
||||
int numberOfPropertySource;
|
||||
|
||||
@Param({"10", "100", "1000"})
|
||||
@Param({ "10", "100", "1000" })
|
||||
int numberOfPropertyNamesPerSource;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
@@ -69,11 +73,13 @@ public class CompositePropertySourceBenchmark {
|
||||
for (int i = 0; i < this.numberOfPropertySource; i++) {
|
||||
Map<String, Object> map = new HashMap<>(this.numberOfPropertyNamesPerSource);
|
||||
for (int j = 0; j < this.numberOfPropertyNamesPerSource; j++) {
|
||||
map.put(RandomStringUtils.randomAlphanumeric(5, 50), VALUE);
|
||||
map.put(ID_GENERATOR.generateId().toString(), VALUE);
|
||||
}
|
||||
PropertySource<?> propertySource = new MapPropertySource("propertySource" + i, map);
|
||||
this.composite.addPropertySource(propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -90,9 +90,7 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> {
|
||||
total += names.length;
|
||||
}
|
||||
Set<String> allNames = new LinkedHashSet<>(total);
|
||||
for (String[] names : namesList) {
|
||||
allNames.addAll(Arrays.asList(names));
|
||||
}
|
||||
namesList.forEach(names -> allNames.addAll(Arrays.asList(names)));
|
||||
return StringUtils.toStringArray(allNames);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -46,4 +47,13 @@ class CompositePropertySourceTests {
|
||||
assertThat(((i1 < i2) && (i2 < i3))).as("Bad order: " + s).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPropertyNamesRemovesDuplicates() {
|
||||
CompositePropertySource composite = new CompositePropertySource("c");
|
||||
composite.addPropertySource(new MapPropertySource("p1", Map.of("p1.property", "value")));
|
||||
composite.addPropertySource(new MapPropertySource("p2",
|
||||
Map.of("p2.property1", "value", "p1.property", "value", "p2.property2", "value")));
|
||||
assertThat(composite.getPropertyNames()).containsOnly("p1.property", "p2.property1", "p2.property2");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user