2

I had some unexpected benchmark results with the following snippet of Groovy Code:

class A{
    def n(){
        return 1
    }
}
class B{
    def n(){
        return 2
    }
}
class C{
    def n(){
        return 3
    }
}
class D{
    def n(){
        return 4
    }
}

def bench(loops){
    def a = new A()
    def b = new B()
    def c = new C()
    def d = new D()
    def ret=0
    for(i=0; i<loops; i++){
        ret = ret + getN(a) + getN(b) + getN(c) + getN(d)
    }
    return ret
}

def getN(clazz){
    return clazz.n()
}

I have to say that I used the compiled class files and invoked it with the Reflection API. Long story to tell (dont ask ;). For the first impressions I used 10 rounds + 5 warmup with 10000 loops. (junit-benchmark framework) My JDK is Verison 1.7.0_09 and I used Groovy 2.1. I compiled the code 2 times with and without invokedynamic support. The point is, that the benchmark with invokedynamic took very much longer than the normal compiled one. I also did other benchmarks with fibonacci numbers, which behaved like expected (indy took about half of the time).

Does anybody know what's going wrong here?

Thanks.

Thorben
  • 953
  • 13
  • 28

1 Answers1

1

I'm no expert at Groovy, but I have a impression that I've read somewhere that Groovy creates something like a "God class". That class contains all the methods that appear in Groovy source code and subclasses only overrides certain methods. Making such class allows for usual invokevirtual calls rather than dynamic lookups. That could be the explanation for slowdown.

Try some runtime injections of methods to classes to break the static analysis of Groovy and force the compiler to output a dynamic lookup code.

  • I tried the Groovycode from http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/. The invokedynamic Performance was better with that. In that benchmark the objects are more complex. If somebody can confirm your thesis with the 'god module' I mark the question as solved. – Thorben Jun 27 '13 at 20:07