Search code examples
javajit

Do more methods mean the code is less performant?


Do more methods, even if they are not called, have an affect on the performance of a particular class...

By performance, I mean anything, like does it take longer to create the object, does it take longer to actually execute a method...etc...

Now my understanding is that the code will only be compiled by the JIT compiler if it reaches a code block/method that it has not reached before...which would lead me to think that I am no affecting anything by adding default methods. Yes it will add to the "size" of the (byte) code but doesn't actually affect performance?

Am I right or wrong?

Here is the example:

public interface MyInterface {
    void someMethod();

    public default void myDefaultMethod() {
        System.out.println("hey");
    }
}

public class MyClass implements MyInterface {

    public void someMethod() {
    }
}

public static void main(String[] args) {
    MyClass c = new MyClass();
    c.someMethod();
    c.myDefaultMethod();
}

If I then change MyInterface and add LOTS of default methods, even if they are never called, will it have an affect:

public interface MyInterface {
    void someMethod();

    public default void myDefaultMethod() {
        System.out.println("hey");
    }

    public default void myDefaultMethod1() {
        System.out.println("hey1");
    }

    public default void myDefaultMethod2() {
        System.out.println("hey1");
    }

    // ...

    public default void myDefaultMethod100() {
        System.out.println("hey100");
    }
}

Solution

  • You're right in one sense. There's some nuance, though.

    Do more methods, even if they are not called, have an affect on the performance of a particular class...

    "Performance" usually refers to speed of execution of the program. Code that is never executed will never (directly) consume any CPU time. Thus, code that is never executed cannot (directly) affect execution time. So in that sense, you are correct.

    By performance, I mean anything, like does it take longer to create the object, does it take longer to actually execute a method...etc...

    No, and no. There's no reason having extra methods lying around would affect object creation time, as that's a function of object size, and at least in Java objects don't directly contain their methods, if memory serves. Having extra methods definitely won't (directly) affect execution of unrelated methods.

    Now my understanding is that the code will only be compiled by the JIT compiler if it reaches a code block/method that it has not reached before...

    This isn't totally right. The JITC can revisit the same section of code over and over again if it determines that doing so would be beneficial.

    ... which would lead me to think that I am no affecting anything by adding default methods. Yes it will add to the "size" of the (byte) code but doesn't actually affect performance?

    You're right that the bytecode file would be larger. You're wrong in that that wouldn't make a difference.

    Code size can have a significant impact on performance. Small programs that can fit mostly/entirely in cache will have a significant advantage over larger programs, as pieces of code don't have to be loaded from RAM or the HDD/SSD, which are much slower than the cache.

    The amount of code needed to do this might be pretty large, though, so maybe for a method or two it wouldn't matter that much. I'm not sure at what point code size in Java becomes a problem. Never tried to find out.

    If you never call those methods, it might be possible that the bits of code that make up those methods are never loaded, which removes their cache-related performance penalty. I'm not certain if splitting the program code like this is possible, though.


    So in the end, it probably wouldn't be harmful, so long as you don't have an excessive number of methods. Having methods around that are never called, though, might be problem for code maintainability, which is always a factor that you should consider.