2

If I use indirection a lot, but the target has the same address, will the cost of indirection become near-zero?

Example :-

class C{
    public: int database[100000];
    //other functions
};
class B{
    public: C* c=nullptr;
};

First Scenario

int main(){
    //--- first scenario (direct call) ---
    C c;
    for(int n=0;n<100000;n++){
         c.database[n]=43;
         //call some complex function about "c"
         //call some non-related function 
    }
}

Second scenario

int main() {
    //--- second scenario (indirection) ---
    C c;
    B b1;  
    b1.c=c; 
    for(int n=0;n<100000;n++){
         b1.c->database[n]=43;
         //call some complex function about "c", but call in "b1.c" form
         //call some non-related function 
    }
}

Would both cases cost (nearly) the same?
How much possibility that it will be optimized out to be nearly the same?

I believe cost of indirection is low (because no cache miss for c),
but I don't know whether there is a base cost (b1.c-> vs c.)?
(I am new for C++)

In real case, B is an iterator, and C is the related data-structure.

Rishi
  • 1,387
  • 10
  • 14
javaLover
  • 6,347
  • 2
  • 22
  • 67

0 Answers0