pointer overhead

Started by
27 comments, last by Russell 20 years, 5 months ago
Could pointer overhead (the this pointer, to be specific) cause about a 3% slowdown in code, for a variable that is used frequently? It was previously just an int that was accessed directly, and I stuffed it in a class, and I want to know if this slowdown is because of the extra pointer indirection, or if I''m doing something wrong with my class.

class Color {
private:
    unsigned int color;
public:
    Color () { }
    Color (unsigned int c) : color(c) { }
    void change () { color ^= 1; }
    const unsigned int & to_i () const { return color; }
};
I don''t see any reason why this should be any slower than using an int and working with that directly, other than accessing it through the this pointer.
Advertisement
Hello Russell,

If it now a class member and the only way you can change it is with a class member method then there could be the problem.

Even though you have change() as a short method it is not decleared inline. And I belive compiler will create a function call there.

Try declearing it inline, also inline to_i and see it that speeds up your processing.

Lord Bart
How do you pass objects of Color around? By value or reference? It''s got a trivial copy constructor (generated by the compiler), but it will be invoked every time you pass a Color object to a function by value:

void processColor(Color c);

Use a const reference instead:

void processColor(const Color & c);

Just a guess.

--
Dave Mikesell Software & Consulting
Functions defined in the header file are inline.

--
Dave Mikesell Software & Consulting
Also, I would make to_i() return just an int, not a const & int. Evil programmers can still get around the const & by casting:

int main(){    Color c(5);    cout << c.to_i() << endl;    unsigned int & ss = const_cast<unsigned int &>(c.to_i());    ss = 3;     cout << c.to_i() << endl;}


Prints 5, then 3. By casting away the constness of the reference to your member data, it can now be modified. Returning an int is no more overhead than returning a reference.

--
Dave Mikesell Software & Consulting
quote:Original post by dmikesell
Also, I would make to_i() return just an int, not a const & int.
Indeed.
quote:Evil programmers can still get around the const & by casting:
But that''s not the reason! Evil programmers can do a lot of nasty stuff even if you return by value. I hope you were joking.
quote:Returning an int is no more overhead than returning a reference.
Returning an int is *less* overhead, and that''s the reason to use them. Const references are accessed indirectly (and implicitely) through pointers, but if colour is returned by value, you gain direct access.
quote:Original post by dmikesell
How do you pass objects of Color around? By value or reference? It''s got a trivial copy constructor (generated by the compiler), but it will be invoked every time you pass a Color object to a function by value:

void processColor(Color c);

Use a const reference instead:

void processColor(const Color & c);

Just a guess.
And then you''ll pass a 4-byte pointer instead of a 4-byte int, and get the slowness of indirect access.
Instead of shooting in the dark and asking on Internet message boards go get a profiler and see if it does.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
As far as I know,

If to_i is not decleared as inline return of a & it should have the same overhead as if return by value beacuse the code should work out to load the value form the address of color into into the reg used for the return. Which is what is happen of coping the value into the reg to return on a return by value.
Both have to copy color value into return reg and return.

But if it inline it works out to a mere assign of color to what it is assign to. Agian in inline both & and vlaue should work out at same overhead.

const is just a compile time check so does not effect runtime.

Now if it remain a function call istead of inline yes ther could be a extra indirection over the return by value.

Lord Bart

[edited by - lord bart on October 24, 2003 9:47:33 AM]
No, I really mean you should avoid returning handles to member data, because evil (or perhaps inexperienced) programmers might find a way to get around the constness, breaking your encapsulation.

--
Dave Mikesell Software & Consulting

[edited by - dmikesell on October 24, 2003 9:51:25 AM]
quote:
quote:
--------------------------------------------------------------------------------
Original post by dmikesell
How do you pass objects of Color around? By value or reference? It''s got a trivial copy constructor (generated by the compiler), but it will be invoked every time you pass a Color object to a function by value:

void processColor(Color c);

Use a const reference instead:

void processColor(const Color & c);

Just a guess.
--------------------------------------------------------------------------------

And then you''ll pass a 4-byte pointer instead of a 4-byte int, and get the slowness of indirect access.


What? Color is a class, which has a copy constructor. If you pass a Color object by value, the copy constructor gets called. If you pass by const reference, it does not.

--
Dave Mikesell Software & Consulting

This topic is closed to new replies.

Advertisement