Java generic Interface performance

二次信任 提交于 2020-12-08 06:25:23

问题


Simple question, but tricky answer I guess.

Does using Generic Interfaces hurts performance?

Example:

public interface Stuff<T> {

    void hello(T var);
}

vs

public interface Stuff {

    void hello(Integer var);  <---- Integer used just as an example
}

My first thought is that it doesn't. Generics are just part of the language and the compiler will optimize it as though there were no generics (at least in this particular case of generic interfaces).

Is this correct?


回答1:


There is potential for a minor performance loss, because the compiler sometimes adds synthetic bridge methods. Consider the following example:

public class GenericPerformance {
    public static void main(final String[] args) {
        final Stuff<Integer> stuff = new IntStuff();
        final Integer data = stuff.getData();
        stuff.putData(data);
    }
}

interface Stuff<T> {
    T getData();
    void putData(T stuff);
}

class IntStuff implements Stuff<Integer> {
    private Integer stuff;
    public Integer getData() {
        return stuff;
    }
    public void putData(final Integer stuff) {
        this.stuff = stuff;
    }
}

If you look at the generated bytecode, you will see: In the main method, the erased interface methods

java.lang.Object Stuff.getData()
void Stuff.putData(java.lang.Object)

are invoked. That methods, implemented in IntStuff with the signatures

java.lang.Object getData()
void putData(java.lang.Object)

both with the modifiers public bridge synthetic, delegate to the "real" methods

java.lang.Integer IntStuff.getData()
void putData(java.lang.Integer)

The first synthetic method merely returns the Integer result, while the second performs a cast from Object to Integer before calling putData(Integer).

If you change the stuff variable to type IntStuff, then both Integer methods are called instead of the synthetic Object methods.




回答2:


Yup - java generics are an entirely compile-time construct. The JVM sees it as a normal interface. So there's no runtime performance gain or loss using generics.




回答3:


They are simply a helper at compile-time to get type safety.

Generics are implemented by type erasure: generic type information is present only at compile time, after which it is erased by the compiler.

Taken from: http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html




回答4:


Actually, I believe that in your example there IS a small performance difference. The compiled form of the first version assumes that hello() receives an Object, while in the second one it assumes an Integer. Therefore, when you instantiate StuffImpl<Integer>, the calls to hello() will be a little slower due to the implicit cast added by the compiler.

-- CORRECTION --

The implicit cast will not be added to hello(). However, if you add a getter method that returns T, the cast will be added to the returned value.

The bottom line is - usage of generics introduces negative performance impact in some cases, when compared with non-generic code restricted to specific types.



来源:https://stackoverflow.com/questions/2745944/java-generic-interface-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!