Archived

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

dummy functions or IF?

This topic is 5149 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

i am working on an OpenGL GUI (in C++), in which i expose a bunch of function pointers in the window objects for events (i.e. there is a "clicked" function pointer, a "mousemove" function pointer, a "keypress" function pointer, etc). the functions pointed to are called whenever they should be, as decided by the GUI system. now, some of these might not be used. for example, the "mousemove" event will not be used in many cases. my question is: would it be faster/better/safer to set the unused function pointers to NULL and have an "if" statement before calling it each time (don''t call it if it is NULL, of course!), or to set them to dummy functions that do nothing? if i use an "if", that "if" will be checked each time any event is supposed to be called. that might be too wasteful. if i use empty dummy functions that are called, there is the possibility of calling an empty function many many times uselessly, also wasteful. i am curious which is more wasteful, since it must be one or the other. and, i am concerned that the compiler might optimize away the dummy functions (in which case, what happens to the function pointers that are assigned to the no-longer-existant dummy functions?). so, what do you think?

Share this post


Link to post
Share on other sites
I'd be interested in knowing the answer to this question. This, of course, implies that I'm not about to reveal the answer myself. I will say this though: The ifs are called whether the function pointer is NULL or not. For all the function pointers that actually call a function, it's a waste. With dummy/default functions, this is not the case.

Personally, I'd go with the dummy/default function method. It has the just mentioned advantage, and it also looks cleaner.

if (myfunc != NULL)
myfunc ();

// or, with the dummy/default method, just...
myfunc ();

quote:

i am concerned that the compiler might optimize away the dummy functions (in which case, what happens to the function pointers that are assigned to the no-longer-existant dummy functions?



As far as I know, if you declare a function, it will exist. The compiler won't pull the rug from under you like that. "Oh that function. I optimized it away. The fastest code is code that never gets called, right?" - compiler

Ro_Akira


[edited by - Ro_Akira on November 11, 2003 11:52:10 AM]

Share this post


Link to post
Share on other sites
Mmm, stupid optimization thread of the day. Using an if to check the pointer may be a bit faster, but using empty functions will be a thousand times safer and more consistent. And no, there''s no way a compiler will "optimize away" entire functions if you request pointers to them. For further reading, see the thousand "premature optimization" threads around.

Share this post


Link to post
Share on other sites
quote:
Original post by Painless
Mmm, stupid optimization thread of the day. Using an if to check the pointer may be a bit faster, but using empty functions will be a thousand times safer and more consistent. And no, there''s no way a compiler will "optimize away" entire functions if you request pointers to them. For further reading, see the thousand "premature optimization" threads around.

thanks for the condescension.

as far as safety is concerned, i am pretty sure i read (here at gamedev) that functions can be optimized away if they do nothing, in which case i''d be screwed with the dummy function method. maybe getting pointers to the functions will prevent that, or maybe i imagined that piece of information completely, but i doesn''t hurt to ask, right?

maybe it does...

Share this post


Link to post
Share on other sites