passing by value and by pointer using position independent code

391 views Asked by At

I've been trying to figure out a few things to do with position independent code, specifically using gcc with -fpic.

I've written a function which measures the time taken to pass a pointer across the stack a number of times, and then does the same by passing a copy. In my example the effect I see only works with struct or simple classes with no inheritance or virtual functions.

Why when using PIC does the pointer become slower (by a little)? and why does the copy become much much faster as oposed to with no PIC?

I'm compiling with -O3 also.

class basicClass
{
    private:
    char ar[2000];
};

void PassCopy(basicClass cpy)
{
    static long count = 0;
    count++;
    if(count < 100000)
    PassCopy(cpy);
}

void PassPtr(basicClass * ptr)
{
    static long count = 0;
    count++;
    if(count < 100000)
        PassRegPtr(ptr);
}

void RunCopyTest()
{
    basicClass c;
    
    timeval tv1, tv2;
    
    gettimeofday(&tv1, NULL);
    PassCopy(c);
    gettimeofday(&tv2, NULL);
    
    long long diff1 = (tv2.tv_sec * MICROSECONDS_PER_SEC + tv2.tv_usec)
                    - (tv1.tv_sec * MICROSECONDS_PER_SEC + tv1.tv_usec);
    
    basicClass *pc= new basicClass();
    gettimeofday(&tv1, NULL);
    PassRegPtr(pc);
    gettimeofday(&tv2, NULL);
    delete pc;
    
    long long diff2 = (tv2.tv_sec * MICROSECONDS_PER_SEC + tv2.tv_usec)
                    - (tv1.tv_sec * MICROSECONDS_PER_SEC + tv1.tv_usec);
    
    std::cout << "Diff 1 = " << diff1 << " Diff 2 = " << diff2 << std::endl;
}
1

There are 1 answers

0
jxh On

Your code is using tail recursion, so at -O3, the code is optimized into a while loop. Thus, your code is really measuring the expense of copy constructors with and without -fPIC.

There is a lot to consider when you are trying to optimize shared libraries (the most common use of the -fPIC flag). If you have not done so, consult Ulrich Drepper's excellent article "How To Write Shared Libraries".