Skip to content

How fast is ClassGraph

Luke Hutchison edited this page Jun 12, 2020 · 2 revisions

ClassGraph is the fastest classpath and module scanning mechanism:

  • ClassGraph parses the classfile binary format directly to determine the class graph. This is significantly faster than reflection-based methods, because no classloading needs to be performed to determine how classes are related, and additionally, class static initializer blocks are not called (which can be time consuming, and can trigger side effects).
  • ClassGraph implements its own highly-optimized bytecode parser, and does not depend upon large, abstract bytecode parsing libraries like ObjectWeb ASM. In fact, ClassGraph does not have any dependencies at all.
  • ClassGraph also implements its own highly-optimized zipfile central directory parser, which is capable of handling jarfiles nested within jarfiles, to arbitrary nesting depth, without first extracting the inner jarfile (as long as the inner jarfile is stored rather than deflated). (The Java ZipFile API is not capable of this.)
  • ClassGraph has been carefully profiled, tuned and parallelized so that multiple threads are concurrently engaged in reading from disk/SSD, decompressing jarfiles, and parsing classfiles. Consequently, ClassGraph runs at close to the theoretical maximum possible speed for a classpath scanner, and scanning speed is primarily limited by raw filesystem bandwidth.
  • Wherever possible, lock-free datastructures are used to eliminate thread contention, and shared caches are used to avoid duplicating work. Recyclers are used to avoid allocating more objects than necessary, and to avoid repeating the overhead of opening jarfiles and modules.
  • ClassGraph includes comprehensive mechanisms for accepting and rejecting, so that only the necessary resources are scanned.
  • ClassGraph has several different mechanisms for opening files on disk (including FileInputStream and memory-mapped NIO FileChannel), switching automatically between them according to OS-specific tuning and benchmarking, depending on the filesize. This gives the fastest possible mechanism for scanning classfiles in directories, jars, and modules. ClassGlaph also only reads as many bytes as necessary from a classfile to get the required class metadata.

ClassGraph is typically several times faster at scanning large classpaths consisting of many directories or jarfiles than the widely-used library Reflections. If ClassGraph is slower than Reflections for your usecase, that is because it has discovered a larger set of classpath elements to scan than Reflections. You can limit what is scanned using accept / reject criteria (see the wiki for more info).