< prev index next >

src/java.base/share/classes/java/lang/module/SystemModuleFinder.java

Print this page
rev 15833 : 8168073: Speed up URI creation during module bootstrap
Reviewed-by: alanb, mchung, psandoz


  27 
  28 import java.io.ByteArrayInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.UncheckedIOException;
  32 import java.net.URI;
  33 import java.net.URLConnection;
  34 import java.nio.ByteBuffer;
  35 import java.util.Collections;
  36 import java.util.HashMap;
  37 import java.util.HashSet;
  38 import java.util.Map;
  39 import java.util.Objects;
  40 import java.util.Optional;
  41 import java.util.Set;
  42 import java.util.function.Supplier;
  43 
  44 import jdk.internal.jimage.ImageLocation;
  45 import jdk.internal.jimage.ImageReader;
  46 import jdk.internal.jimage.ImageReaderFactory;


  47 import jdk.internal.module.ModuleHashes;
  48 import jdk.internal.module.ModuleHashes.HashSupplier;
  49 import jdk.internal.module.SystemModules;
  50 import jdk.internal.module.ModulePatcher;
  51 import jdk.internal.perf.PerfCounter;
  52 
  53 /**
  54  * A {@code ModuleFinder} that finds modules that are linked into the
  55  * run-time image.
  56  *
  57  * The modules linked into the run-time image are assumed to have the
  58  * ConcealedPackages attribute.
  59  */
  60 
  61 class SystemModuleFinder implements ModuleFinder {
  62 
  63     private static final PerfCounter initTime
  64         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.initTime");
  65     private static final PerfCounter moduleCount
  66         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.modules");
  67     private static final PerfCounter packageCount
  68         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.packages");
  69     private static final PerfCounter exportsCount
  70         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.exports");
  71     // ImageReader used to access all modules in the image
  72     private static final ImageReader imageReader;
  73 


  74     // the set of modules in the run-time image
  75     private static final Set<ModuleReference> modules;
  76 
  77     // maps module name to module reference
  78     private static final Map<String, ModuleReference> nameToModule;
  79 
  80     /**
  81      * For now, the module references are created eagerly on the assumption
  82      * that service binding will require all modules to be located.
  83      */
  84     static {
  85         long t0 = System.nanoTime();
  86         imageReader = ImageReaderFactory.getImageReader();
  87 
  88         String[] names = moduleNames();
  89         ModuleDescriptor[] descriptors = descriptors(names);
  90 
  91         int n = names.length;
  92         moduleCount.add(n);
  93 


 149     }
 150 
 151     private static boolean isFastPathSupported() {
 152        return SystemModules.MODULE_NAMES.length > 0;
 153     }
 154 
 155     private static String[] moduleNames() {
 156         if (isFastPathSupported())
 157             // module names recorded at link time
 158             return SystemModules.MODULE_NAMES;
 159 
 160         // this happens when java.base is patched with java.base
 161         // from an exploded image
 162         return imageReader.getModuleNames();
 163     }
 164 
 165     private static ModuleReference toModuleReference(ModuleDescriptor md,
 166                                                      HashSupplier hash)
 167     {
 168         String mn = md.name();
 169         URI uri = URI.create("jrt:/" + mn);

 170 
 171         Supplier<ModuleReader> readerSupplier = new Supplier<>() {
 172             @Override
 173             public ModuleReader get() {
 174                 return new ImageModuleReader(mn, uri);
 175             }
 176         };
 177 
 178         ModuleReference mref =
 179             new ModuleReference(md, uri, readerSupplier, hash);
 180 
 181         // may need a reference to a patched module if --patch-module specified
 182         mref = ModulePatcher.interposeIfNeeded(mref);
 183 
 184         return mref;
 185     }
 186 
 187     private static HashSupplier hashSupplier(int index, String name) {
 188         if (isFastPathSupported()) {
 189             return new HashSupplier() {




  27 
  28 import java.io.ByteArrayInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.UncheckedIOException;
  32 import java.net.URI;
  33 import java.net.URLConnection;
  34 import java.nio.ByteBuffer;
  35 import java.util.Collections;
  36 import java.util.HashMap;
  37 import java.util.HashSet;
  38 import java.util.Map;
  39 import java.util.Objects;
  40 import java.util.Optional;
  41 import java.util.Set;
  42 import java.util.function.Supplier;
  43 
  44 import jdk.internal.jimage.ImageLocation;
  45 import jdk.internal.jimage.ImageReader;
  46 import jdk.internal.jimage.ImageReaderFactory;
  47 import jdk.internal.misc.JavaNetUriAccess;
  48 import jdk.internal.misc.SharedSecrets;
  49 import jdk.internal.module.ModuleHashes;
  50 import jdk.internal.module.ModuleHashes.HashSupplier;
  51 import jdk.internal.module.SystemModules;
  52 import jdk.internal.module.ModulePatcher;
  53 import jdk.internal.perf.PerfCounter;
  54 
  55 /**
  56  * A {@code ModuleFinder} that finds modules that are linked into the
  57  * run-time image.
  58  *
  59  * The modules linked into the run-time image are assumed to have the
  60  * ConcealedPackages attribute.
  61  */
  62 
  63 class SystemModuleFinder implements ModuleFinder {
  64 
  65     private static final PerfCounter initTime
  66         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.initTime");
  67     private static final PerfCounter moduleCount
  68         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.modules");
  69     private static final PerfCounter packageCount
  70         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.packages");
  71     private static final PerfCounter exportsCount
  72         = PerfCounter.newPerfCounter("jdk.module.finder.jimage.exports");
  73     // ImageReader used to access all modules in the image
  74     private static final ImageReader imageReader;
  75 
  76     private static final JavaNetUriAccess jnua = SharedSecrets.getJavaNetUriAccess();
  77 
  78     // the set of modules in the run-time image
  79     private static final Set<ModuleReference> modules;
  80 
  81     // maps module name to module reference
  82     private static final Map<String, ModuleReference> nameToModule;
  83 
  84     /**
  85      * For now, the module references are created eagerly on the assumption
  86      * that service binding will require all modules to be located.
  87      */
  88     static {
  89         long t0 = System.nanoTime();
  90         imageReader = ImageReaderFactory.getImageReader();
  91 
  92         String[] names = moduleNames();
  93         ModuleDescriptor[] descriptors = descriptors(names);
  94 
  95         int n = names.length;
  96         moduleCount.add(n);
  97 


 153     }
 154 
 155     private static boolean isFastPathSupported() {
 156        return SystemModules.MODULE_NAMES.length > 0;
 157     }
 158 
 159     private static String[] moduleNames() {
 160         if (isFastPathSupported())
 161             // module names recorded at link time
 162             return SystemModules.MODULE_NAMES;
 163 
 164         // this happens when java.base is patched with java.base
 165         // from an exploded image
 166         return imageReader.getModuleNames();
 167     }
 168 
 169     private static ModuleReference toModuleReference(ModuleDescriptor md,
 170                                                      HashSupplier hash)
 171     {
 172         String mn = md.name();
 173 
 174         URI uri = jnua.create("jrt", "/".concat(mn));
 175 
 176         Supplier<ModuleReader> readerSupplier = new Supplier<>() {
 177             @Override
 178             public ModuleReader get() {
 179                 return new ImageModuleReader(mn, uri);
 180             }
 181         };
 182 
 183         ModuleReference mref =
 184             new ModuleReference(md, uri, readerSupplier, hash);
 185 
 186         // may need a reference to a patched module if --patch-module specified
 187         mref = ModulePatcher.interposeIfNeeded(mref);
 188 
 189         return mref;
 190     }
 191 
 192     private static HashSupplier hashSupplier(int index, String name) {
 193         if (isFastPathSupported()) {
 194             return new HashSupplier() {


< prev index next >