From: Robert Varga Date: Mon, 18 Nov 2019 11:36:45 +0000 (+0100) Subject: Optimize AbstractTypeGenerator.listKeys() X-Git-Tag: v4.0.8~14 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=aaf3b063417074cc92900629e917d6d0c5092dcd Optimize AbstractTypeGenerator.listKeys() In case of an empty key definition we end up allocating short-lived empty ArrayLists, which is not efficient. Also lists typically have a few elements, so we end up over-allocating them. This fixes both cases, by checking the definition first and returning an empty list in case the definition is empty, as well as properly allocating efficient implementation where needed. Change-Id: I99e8843abf6040654fab96052352cbfb29590ecf Signed-off-by: Robert Varga (cherry picked from commit efe9042ecb51521850d9b90f4d8460a35c9bd9ea) --- diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java index bace161c8f..53d214f414 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java @@ -51,6 +51,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; @@ -1788,15 +1789,19 @@ abstract class AbstractTypeGenerator { * an empty list is returned. */ private static List listKeys(final ListSchemaNode list) { - final List listKeys = new ArrayList<>(); - final List keyDefinition = list.getKeyDefinition(); - if (keyDefinition != null) { - for (final QName keyDef : keyDefinition) { - listKeys.add(keyDef.getLocalName()); - } + switch (keyDefinition.size()) { + case 0: + return Collections.emptyList(); + case 1: + return Collections.singletonList(keyDefinition.get(0).getLocalName()); + default: + final List listKeys = new ArrayList<>(keyDefinition.size()); + for (final QName keyDef : keyDefinition) { + listKeys.add(keyDef.getLocalName()); + } + return listKeys; } - return listKeys; } /**