From 848b1fc4212f5cb60553c4625def992074a58e0b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 9 Jun 2017 11:17:47 +0200 Subject: [PATCH 1/1] 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) --- .../effective/EffectiveSchemaContext.java | 15 +++++-------- .../parser/util/ModuleDependencySort.java | 21 ++++++++++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) 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 0223bdf139..5e670f5af2 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 6c6afb12c8..bc980c03a0 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 @@ -60,7 +60,18 @@ public final class ModuleDependencySort { * returned order. */ public static List sort(final Module... modules) { - 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); } @@ -111,10 +122,10 @@ public final class ModuleDependencySort { // check for existence of module with same namespace if (allNS.containsKey(ns)) { - Module mod = allNS.get(ns); - String name = mod.getName(); - Date revision = mod.getRevision(); - if (!(fromName.equals(name))) { + final Module mod = allNS.get(ns); + final String name = mod.getName(); + final Date revision = mod.getRevision(); + if (!fromName.equals(name)) { LOGGER.warn( "Error while sorting module [{}, {}]: module with same namespace ({}) already loaded: [{}, {}]", fromName, fromRevision, ns, name, revision); -- 2.36.6