Holy classloader recursion, Batman!

import org.jvnet.hudson.annotation_indexer.*;
import hudson.cli.declarative.*;

for (int j=0; j<10; j++) {
    long l = System.currentTimeMillis();
    for (int i=0; i<10; i++) {
        for (Class c : Index.list(OptionHandlerExtension.class, Jenkins.getInstance().pluginManager.uberClassLoader,Class.class))
            ; // no-op
    }
    println System.currentTimeMillis()-l;
}

I've installed about 40 random plugins on Jenkins on Tomcat and run the code above on it to get the sense of how long it takes to run them. Each full lookup is taking a whopping 500ms!

5595
5628
5604
5610
5573
5622
5783
6417
5703
5687

The problem is that AntClassLoader.findResources is calling parent.getResources() when it really shouldn't. The parent classloader in this case is DependencyClassLoader, which tries every dependency. The net result is that Tomcat classloader gets hit O(E*V) times for the number of edges and nodes in the plugin dependency graph.

But this makes no sense, because we specifically call AntClassLoader.findResources(String) to prevent recursion into parent classloader. The reason this happens is because AntClassLoader has the following line:

        if (parent != null && (!parentHasBeenSearched || parent != getParent())) {
            // Delegate to the parent:
            base = parent.getResources(name);
            // Note: could cause overlaps in case
            // ClassLoader.this.parent has matches and
            // parentHasBeenSearched is true
        } else {
            // ClassLoader.this.parent is already delegated to for example from
            // ClassLoader.getResources, no need:
            base = new CollectionUtils.EmptyEnumeration();
        }

parent!=getParent() indeed because Ant doesn't call the superclass constructor and pass in the specified parent classloader.

After fixing this in 9a2882dd704bece9b7ca51a52347dad15d79f843, this went down to the following:

69
13
15
14
10
10
10
9
8
9

Problem solved!

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira

--
You received this message because you are subscribed to the Google Groups "Jenkins Issues" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-issues+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to