-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid map resizing in PrivateViewConfig
Properly size the LinkedHashMaps in the createState method to avoid rehashing / resizing. Additionally, if instrumentation is not enabled, just use the singleton empty map. To facilitate this change, add an Iterables utility class with a "size" helper to determine the size of the passed in Iterable; this is necessary because the Config::keys method returns Iterable, but in practice is typically a Collection. Finally, add a marker annotation, Internal, to indicate classes such as Maps and Iterables that are not meant to be used by users of Archaius and may not have stable APIs.
- Loading branch information
Showing
5 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
archaius2-core/src/main/java/com/netflix/archaius/Internal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.netflix.archaius; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PACKAGE; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
|
||
/** | ||
* Annotation to convey classes and other elements not meant to be part of the public API, but are public by necessity; | ||
* such elements should not be depended on by consumers as there is no guarantee of API stability. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(value = {CONSTRUCTOR, METHOD, TYPE, FIELD, PACKAGE}) | ||
public @interface Internal { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
archaius2-core/src/main/java/com/netflix/archaius/util/Iterables.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.netflix.archaius.util; | ||
|
||
import com.netflix.archaius.Internal; | ||
|
||
import java.util.Collection; | ||
|
||
@Internal | ||
public final class Iterables { | ||
private Iterables() {} | ||
|
||
/** | ||
* Returns the number of elements in {@code iterable}. | ||
*/ | ||
public static int size(Iterable<?> iterable) { | ||
if (iterable instanceof Collection<?>) { | ||
return ((Collection<?>) iterable).size(); | ||
} | ||
int size = 0; | ||
for (Object ignored : iterable) { | ||
size++; | ||
} | ||
return size; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
archaius2-core/src/main/java/com/netflix/archaius/util/Maps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
archaius2-core/src/test/java/com/netflix/archaius/util/IterablesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.netflix.archaius.util; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class IterablesTest { | ||
@Test | ||
public void testSize() { | ||
assertEquals(0, Iterables.size(ImmutableList.of())); | ||
assertEquals(1, Iterables.size(ImmutableList.of(1))); | ||
assertEquals(2, Iterables.size(ImmutableSet.of(1, 2))); | ||
assertEquals(0, Iterables.size(Collections::emptyIterator)); | ||
assertEquals(1, Iterables.size(() -> Stream.<Object>of("foo").iterator())); | ||
assertEquals(3, Iterables.size(() -> ImmutableList.<Object>of(1, 2, 3).iterator())); | ||
} | ||
} |