From: Robert Varga Date: Fri, 9 Jun 2017 09:17:47 +0000 (+0200) Subject: Do not create temporary array for module sorting X-Git-Tag: release/carbon-sr1~21 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=2d529417d7214a4e0222f7dff84fe24ea245673e;p=yangtools.git Do not create temporary array for module sorting ModuleDependencySort uses an internal iterable anyway, so expose that method and pass our temporary set, skipping an unnecessary copy operation. Change-Id: I2253ed4edc3837ebb9182bcb1b0069d94e2f83e7 Signed-off-by: Robert Varga (cherry picked from commit c0f132ca8118358f1e41feb196da4f068f130430) --- diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java index bf8463df0c..f92d99c4dd 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveSchemaContext.java @@ -43,23 +43,18 @@ public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements); this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements); - Set modulesInit = new HashSet<>(); + final Set modulesInit = new HashSet<>(); for (EffectiveStatement rootEffectiveStatement : rootEffectiveStatements) { if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) { Module module = (Module) rootEffectiveStatement; modulesInit.add(module); } } + this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modulesInit)); - Module[] moduleArray = new Module[modulesInit.size()]; - List sortedModuleList = ModuleDependencySort.sort(modulesInit.toArray(moduleArray)); - this.modules = ImmutableSet.copyOf(sortedModuleList); - - final SetMultimap nsMap = Multimaps.newSetMultimap( - new TreeMap<>(), MODULE_SET_SUPPLIER); - final SetMultimap nameMap = Multimaps.newSetMultimap( - new TreeMap<>(), MODULE_SET_SUPPLIER); - Set modIdBuilder = new HashSet<>(); + final SetMultimap nsMap = Multimaps.newSetMultimap(new TreeMap<>(), MODULE_SET_SUPPLIER); + final SetMultimap nameMap = Multimaps.newSetMultimap(new TreeMap<>(), MODULE_SET_SUPPLIER); + final Set modIdBuilder = new HashSet<>(); for (Module m : modulesInit) { nameMap.put(m.getName(), m); nsMap.put(m.getNamespace(), m); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java index e924ddeb55..ebf6fe278d 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ModuleDependencySort.java @@ -61,7 +61,18 @@ public final class ModuleDependencySort { * returned order. */ public static List sort(final Module... modules) { - final List sorted = sortInternal(Arrays.asList(modules)); + return sort(Arrays.asList(modules)); + } + + /** + * Topological sort of module dependency graph. + * + * @param modules YANG modules + * @return Sorted list of Modules. Modules can be further processed in + * returned order. + */ + public static List sort(final Iterable modules) { + final List sorted = sortInternal(modules); // Cast to Module from Node and return return Lists.transform(sorted, TOPOLOGY_FUNCTION); } @@ -115,7 +126,7 @@ public final class ModuleDependencySort { final Module mod = allNS.get(ns); final String name = mod.getName(); final Date revision = mod.getRevision(); - if (!(fromName.equals(name))) { + if (!fromName.equals(name)) { LOGGER.warn( "Error while sorting module [{}, {}]: module with same namespace ({}) already loaded: [{}, {}]", fromName, fromRevision, ns, name, revision);