Class ClassPathResourceIndex

java.lang.Object
io.quarkus.bootstrap.classloading.ClassPathResourceIndex

public class ClassPathResourceIndex extends Object
We used to have a full index of the resources present in the classpath stored in ClassLoaderState of QuarkusClassLoader.

However it could lead to some major memory consumption, the index being more than 13 MB on real-life projects. This was a problem especially since we also keep a list of the resources in each ClassPathElement.

Going through the whole list of ClassPathElements for each lookup is not a good option so we choose an intermediary approach of having a lossy index referencing the start of the resource path to reduce the number of ClassPathElements to go through. This index still returns exact references as we check the ClassPathElements themselves for the presence of the resource.

In the particular example above, we went down to less than 3 MB for the index, storing ~600 entries instead of 86000+ in the mapping.

We try to be clever as to how we build the resource key to reduce the number of misses. It might need further tuning in the future.

The general idea is to have this index:

  • store a mapping between the prefix of the resource and the ClassPathElements that contain resources starting with this prefix.
  • generate a prefix that is smart: we want to reduce the size of the index but we also want to reduce the impact on performances (e.g. for io.quarkus, we keep one more segment, for versioned classes, we keep 3 more, we make sure META-INF/services/ files are fully indexed...)
  • store some additional information such as the transformed classes, banned resources, the classes that are considered "local" to this class loader (excluding the jars). For these elements, the information is NOT lossy.
When interrogating the index, we get the candidate ClassPathElements and we then check if the resource is actually in a given ClassPathElement before actually considering it.

In most cases, the information that is in the index is that a resource with this prefix is in these ClassPathElement but it might not be the precise resource we are looking for.