Accessing another object field variables performance cost (locality of reference)

132 Views Asked by At

Reading this question and this answer I know that accessing fields will cost some performance in AOT compilers (thanks to JVM I can forget about it)

Now please guide me about this scenario:

public class Foo
{
    Object fooObject;
}

public class Bar
{
    Foo foo;
    Object fooObjectCopy = foo.fooObject; // fooObject replica of object foo (not a clone)
    void barMethod1()
    {
        doSomething (fooObjectCopy);
    }
    void barMethod2()
    {
        doSomething (foo.fooObject);
    }
}

Both barMethod1() and barMethod2() do the same thing. The question is which one does better (in terms of performance) if:

  1. fooObject is a primitive type
  2. fooObject is an object instance

P.S. Please consider both accessing cost and cache miss.

0

There are 0 best solutions below