Hacker News new | ask | show | jobs
by kaba0 1152 days ago
Unless you are in a hot loop (where you may not use virtual methods to begin with), I don’t think that performance difference is significant. Virtual calls have a slight overhead, but far from serious, and similarly not inlining something that you call only a single time for example is not the end of the world.
1 comments

The problem with not inlining is less with the overhead of the function call itself, and more the loss of further optimization opportunities. Consider this (trivial) example:

    main() {
      int x = foo() + 3;
    }

    int foo() {
      return 5;
    }
Without inlining you have both the overhead of the call and the arithmetic addition. If you can inline the call then you get:

    main() {
      int x = 5 + 3;
    }
But more importantly, the optimizer can now also eliminate the addition too:

    main() {
      int x = 8;
    }
This is obviously a trivial example, but in real-world code, the optimization options opened up after inlining are important.