Search code examples
javaperformancecreationoverhead

What is the actual overhead from wrapping objects?


I have a time-sensitive application that gets data from a third party library. What would be the performance hit associated with wrapping their objects into more appropriate interfaces for my application?

Note: I am posting an answer here (Q&A style), but if it's wrong please correct me!


Solution

  • There's some overhead with indirection, but it's hard to measure. The OP's benchmark took about 4 ns per iteration while mine needs about 1 ns (for the fastest experiment). This means that they were measuring mostly the overhead of the ArrayList, Iterator, and cycle probably together with a virtual call overhead.

    The overhead to be measured is so small that you need to use arrays and either add an inner loop or use masking for accessing them.

    The results of my benchmark show that there's a measureable overhead of both using an interface and an indirection. This overhead ranges from maybe 20% to 50%, which looks like a lot. However, the important part is 20-50% of what. It's a fraction of specially crafted benchmark doing nothing but exercising the code. In any realistic piece code the relative overhead will be ten, hundred, or thousand times lower.

    So unless you're designing a high-performance library doing some very basic and fast operations, just forget it. Use indirection and interfaces at will and concentrate on good design. Even if performance is important, there are probably other places where you can gain more.

    WrappingBenchmark