Efficiency of service lookup in layers

David M. Lloyd david.lloyd at redhat.com
Thu Mar 24 16:34:39 UTC 2016


While investigating the JAXP default implementation issue (see 
core-libs-dev), I discovered that when ServiceLoader is loading services 
from a Layer, it is simply iterating every module in the layer to find 
implementations.  What if there are thousands or tens of thousands of 
modules?  Would it not be better to let the Layer's Configuration 
specify some kind of resolver, in order to allow Layer implementations 
to use a more efficient algorithm (like an index) and have control over 
service resolution decisions?

The code in question is in this method of the LayerLookupIterator nested 
class of ServiceLoader:

         Iterator<ModuleDescriptor> descriptors(Layer layer, String 
service) {
             return layer.modules().stream()
                     .map(Module::getDescriptor)
                     .filter(d -> d.provides().get(service) != null)
                     .iterator();
         }

Note that the collection returned by layer.modules() is not presently 
customizable in any way as it is derived from a Map which is constructed 
internally in private code.

-- 
- DML


More information about the jigsaw-dev mailing list