Jump to content
  • Advertisement
Sign in to follow this  
Tispe

[c++] Function pointers in std::vector

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

Hi

My game has a main loop in which it calls several functions each frame. However in some game states some functions should not be called. i.e winsock connection established, no more checking, timeouts, polling etc.

Instead of checking with an "if" statement each time to determine if a function should be called or not I thought about having a std::vector which contains function pointers. Each frame all the functions in the vector is called. If my program decides if a function should not be called it would just "pop" that function pointer off the vector.

Which would be preferrable in terms of load, 50x IF checks for each frame or just loop through an array of function pointers and call them all?

What happends after the vector is pushed and popped hundreds of times, will it fragment over the memory and so on making this less desireable?

Looking forward to your input :)

Share this post


Link to post
Share on other sites
Advertisement
It is not obvious what way is more efficient. Test both methods and use a profiler to decide the winner.
The vector will only change the internal buffer if it is too small. So if there is 50 elements in the vector and there will never be more than 50 elements in the vector it will never resize the internal buffer again. If you want to avoid reallocations you can call v.reserve(50); to make room for 50 elements before you add any elements.

Share this post


Link to post
Share on other sites
I'd suggest you redesign so you don't need to call so much per frame, sounds like you need some form of event system instead "on timeout", "on new connection" etc. I'm not entirely sure how vector works (and wether it actually frees memory on pop or just flags it as unused) but can't you make it allocate to a certain size to begin with (and avoid any fragmentation. I'm certain you can avoid the fragmentation issue - allocate say 100 pointers, null them if their not used, you could sort and loop untill NULL. With that in mind, your still doing an "if" check, plus the sorting (which would reduce the amount of checks but cost).

I'd definately try to avoid it entirely.

Share this post


Link to post
Share on other sites
[font="arial, verdana, tahoma, sans-serif"]To be honest this sounds remarkably like premature optimization. Have you profiled your code and discovered that these if checks are slowing you down some measurable amount? To be perfectly honest, I can't imagine that they are. It should be noted however that while removing elements from vectors isn't slow per-say, removing an entry close to the front will force the vector to shift every item after the removed entry up a space in memory. I'm not sure if such a thing would negate any potential performance gains taking out all those if's or not, but I'd say this idea probably is not necessary. [/font]
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"]That's not to say I don't think you should consider designing your code a bit differently so that you don't need a massive list of if checks right after each other to call various functions. [/font]

Share this post


Link to post
Share on other sites
The cost of 50 [font="Courier New"]if[/font]s is probably measured in nanoseconds, whereas your per-frame budget is measured in milliseconds (1 million times larger). Definitely not worth losing sleep over.

If you were going to use a vector efficiently:
* Call the [font="'Courier New"]reserve[/font] function with a good guess of the maximum size of the vector, to pre-allocate a large-enough array and avoid reallocations.
* When removing an item from the vector, don't just remove it the standard way. Instead, copy the last item over the top of the one to be removed, and then call [font="Courier New"]vec.resize(vec.size()-1)[/font] to erase the last item. This way there's no shuffling of the array data.

Share this post


Link to post
Share on other sites
Thanks for the input. The idea Hodgman brought up was really helpful.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!