0

GroovyClassloader behaviour understanding ,

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
GroovyScriptEngineImpl groovyEngineImpl = (GroovyScriptEngineImpl) engine;

in a loop,

for (int i = 0; i < 10; i++) {
            long startTime = System.currentTimeMillis();
            classLoader = new GroovyClassLoader(groovyEngineImpl.getClassLoader().getParent());
            fileName = fileName + i;
            Class groovyClass = classLoader.parseClass(s,fileName);
            long endTime = System.currentTimeMillis();
            System.out.println("Total elapsed time in execution o  " + (endTime-startTime));
            startTime = System.currentTimeMillis();
            groovyClass = classLoader.parseClass(s,fileName);
            endTime = System.currentTimeMillis();
            System.out.println("Second Time Total elapsed time in execution o  " + (endTime-startTime));


}

I have a couple of questions regarding the above code:

  1. In a for loop I'm creating a new groovyclassloder object, and parsing the groovy script twice. When the loop iterates for the second time, and tries to parse the groovyscript again, what will occur ?
  2. What will happen on the second time when another object is created. Will the classloader manage to get the class form the classpath or again recompile it again?
  3. When recompile is triggered, how does groovy know what the source is changed?
Community
  • 1
  • 1
anish
  • 6,884
  • 13
  • 74
  • 140
  • I assume after your last question when I showed you where the source code for all this is, you didn't go and have a look at the source code? – tim_yates Feb 03 '12 at 14:25
  • @tim_yates hi tim can u help me on this, i have gone to the source code – anish Feb 03 '12 at 14:25

1 Answers1

0
  1. Each time round the loop you are throwing away the classloader and creating a new one. This new classLoader will have no knowledge of classes loaded by the classLoader you have thrown away
  2. It depends on the type of s. If it is a file, it will check if it needs a recompile, and if not it will use the same class. If it is a String or something, then it will have to recompile the class from theis String again
  3. https://github.com/groovy/groovy-core/blob/master/src/main/groovy/lang/GroovyClassLoader.java#L845
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • what i don't understand is why for second and third time execuition time is too less (Each time round the loop you are throwing away the classloader and creating a new one) for i==0 first parse time Total elapsed time in execution o 1570 Second parse Time Total elapsed time in execution o 33 / for i==1 first parseTotal elapsed time in execution o 16 Second parse Time Total elapsed time in execution o 16, – anish Feb 04 '12 at 04:16