Minor performance improvement to java.lang.ModuleLayer.findModule(String name)

Andrej Golovnin andrej.golovnin at gmail.com
Thu Dec 7 10:00:28 UTC 2017


Hi all,

when we try to find a module which is not in the current layer, then
we access the map 'nameToModule' in the parent layers two times in the
lines 849-850:

846        return layers()
847                   .skip(1)  // skip this layer
848                   .map(l -> l.nameToModule)
849                   .filter(map -> map.containsKey(name))  // <-- first access
850                   .map(map -> map.get(name))                // <--
second access
851                   .findAny();

This can be improved when we map to the module in the line 848 and
then filter for non-null values:

846        return layers()
847                   .skip(1)  // skip this layer
848                   .map(l -> l.nameToModule.get(name))
849                   .filter(Objects::nonNull)
850                   .findAny();

The suggested change is attached as diff.

Best regards,
Andrej Golovnin
-------------- next part --------------
diff --git a/src/java.base/share/classes/java/lang/ModuleLayer.java b/src/java.base/share/classes/java/lang/ModuleLayer.java
--- a/src/java.base/share/classes/java/lang/ModuleLayer.java
+++ b/src/java.base/share/classes/java/lang/ModuleLayer.java
@@ -845,9 +845,8 @@
 
         return layers()
                 .skip(1)  // skip this layer
-                .map(l -> l.nameToModule)
-                .filter(map -> map.containsKey(name))
-                .map(map -> map.get(name))
+                .map(l -> l.nameToModule.get(name))
+                .filter(Objects::nonNull)
                 .findAny();
     }
 


More information about the jigsaw-dev mailing list