Skip to main content
GameDev.net gamedev.net
🔒 Locked

Smart Pointer performance overhead

Started by cypherx Feb 23, 2005 at 6:40 PM 3 replies 1.1k views
Original Post
cypherx
cypherx
I'm currently using smart pointers in my game engine, but I'm a little nervous about the performance cost of dereferencing. Here's a really simplified version of my render loop:

class Drawable
{
    virtual void Render()=0; 
};

struct Object
{
    ptr<Drawable> drawable; 
    /* also contains other object properties */ 
}; 

for (list< ptr<Object> >::iterator iter = objectList.begin(); iter != objectList.end(); iter++)
{
    (*iter)->drawable->Render(); 
}





So to render any object the engine has to: 1) Dereference list< ptr >::iterator 2) Dereference ptr 3) Dereference Object* 4) Dereference ptr 5) Dereference Drawable* 6) Call the virtual function Render() (is that another level of indirection with the vtable?) This is working fine for now, but my scenes are tiny...I wonder if all the dereferencing will become a bottle neck when I have thousands of objects. Has this been a problem for anyone? -Alex [Edited by - cypherx on February 23, 2005 7:07:18 PM]
SumDude
SumDude
i don't think it would become much of a problem unless your recreating Doom 3 or Half life 2.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are inte
me22
me22
You can make a local copy of the location in a plain-old-pointer if you need to access stuff through it multipule times.

I think that the overhead of the smart pointer itself ought not to be significant, since the alternative is quite messy.
Sneftel
Sneftel
Is it really that difficult to *put* 1000 objects in your scene, and *see* if it's too slow? Performance metrics like this are usually more or less useless; the only valid metric is real-world performance in your actual situation.
Promit
Promit
It's probably (almost definitely) of no significant cost.

But if you're really worried about it, you could steal boost::shared_ptr (loki might have a smart pointer too, not sure). In this case, the code is probably metaprogrammed to resolve the dereference at compile time in most, if not all cases.

[EDIT] Read this!
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.