DarkGuardsman
DarkGuardsman

Reputation: 126

Performance code of wrappering

Question: What is the performance cost of wrapping an object inside of another object? Both in RAM and CPU usage increase from calling the object directly.

Extra Details: I'm working on developing a framework that will wrapper code to isolate plugins from the core implementation of a program. The question keeps popping up in my head "What is the performance cost". I know it will be more than direct access to the code being wrapped. However, I can not seem to find any solid sources or data online to show exactly what results to expect. Which is an issue as I'm developing this to replace the implementation for roughly 40 projects. So when asked what level of overhead is expected in the update I need to have some data.

Also, in case anyone is unsure what I mean by wrapped code. It is the process in which an object is created using an interface that sends all method calls to an object stored in the object. This is done to hide the implementation of an object when the original object can not be modified or changed to include the interface. Below is an example of what I'm talking about.

public class WrapperObject implements WrapperInterface
{
    private SomeObjectClass theObject;

    public WrapperObject(SomeObjectClass theObject)
    {
        this.theObject = theObject;
    }

    public void method1()
    {
        theObject.method1();
    }

    public void method2()
    {
        theObject.method2("someData", 1);
    }
}

As well I do know about JIT in java and other optimization technics the JVM does. I'm asking what I can expect from the overhead of using a system like this for a large project. As I will be wrapping or converting just about everything before it gets to the plugin code. This includes methods that may be called a few 1000 times a second as well as methods only called once.

I will also be building prototypes in the next few days to test performance as well. I'm just looking for some design input to better understand what to expect. Rather than looking blindly at my code making guesses that waste time.

Upvotes: 2

Views: 289

Answers (1)

Sasha Shpota
Sasha Shpota

Reputation: 10300

It is hard to estimate performance/CPU consumption because it really depend on what are you going to do with that objects and how you're going to process them. But you can measure RAM consumption relatively well.

Without going into details you can measure size of an object as sum of

  • size of headers (16 bytes for 64 bit JDK)
  • size of primitive members (find sizes in the documentation)
  • size of reference members (8 bytes for 64 bit JDK)
  • offset size (few bytes for equalization)

Your particular object will have headers, one reference plus offset.

Note this is approximation the exact size depend on a lot of other factors, but it will help you imagine how much memory will be consumed.

For more details on sizes of different types take a look on this question

Upvotes: 2

Related Questions