Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Russell

pointer overhead

This topic is 5351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!