I have a question. I am trying to create the code to update my game but I have gotten into a "dilemma". I don't want to use virtual and the only reason for it is EVERYBODY i talked to (in forums, chats, friends) say that virtuals make the code really slow, so I did a research and found out that the lookup it does from vtable can decrease the performance by almost half. So, I use it for tasks that doesn't need to be updated every frame. Everything worked fine until I got to the update/render functions. Then I started thinking to find a workaround. Got an idea but first I would like to ask people who have knowledge on that one before implementing it.
My game engine is very event driven. I can send any kind of data using events in between subsystems (graphics, ui, scripting). So, I am thinking of sending an event "renderScene" every frame. It sounds great to me but there is a problem. The structure of the event handlers are not that great and I really don't want to improve this right now because it does a really decent job and my goal is to finish my game instead of fixing up the engine and never finish it (happened to me, so don't want to go back to it again).
My event handler have a function that registers events to functions (i call the handlers). But the problem with that function is, I need to do function bindings and stuff to register member functions. So, I found a workaround it - I create a static function and call the member function from it. This is how exactly a static function look like:
void GraphicsSubsystem::renderScene(Subsystem * subsystem, Event * event) {
GraphicsSubsystem * graphics = static_cast<GraphicsSubsystem *>(subsystem);
graphics->renderScene();
}
void ScriptingSubsystem::runLine(Subsystem * subsystem, Event * event) {
ScriptingSubsystem * scripting = static_cast<ScriptingSubsystem *>(subsystem);
Event1<String> * e = static_cast<Event1<String> *>(event);
scripting->runLine(e->getArg());
}
There arguments are always the abstract subsystem class and the base event class. The runLine function I have no problem casting because I dont run a line of code on every frame. However, the renderScene function makes me a little bit uncomfortable.
tl;dr So, here is my question. Is static casting an object on every frame faster than calling a virtual function on every frame?
Yes, a static cast is a quite fast operation. A static cast is static, i.e. all parameters are known at compile time and at runtime a pointer is modified by a constant.
However, you also shouldn't be over-pessimistic on virtual function calls. While they are slower than normal function calls, they are still very, very fast on many timescales, especially compared to the cost of rendering a scene. I can hardly imagine a game that is made slow by O(1) virtual function calls per frame. Make a clean design first, and worry about slowness when you observe a slow game and can profile to see where exactly it is slow.
The general answer is it depends on the particular circumstances.
However, try to imagine what the runtime does on virtual function on invocation - it takes the this pointer, looks up the pointer of the function in the virtual method table and runs the function.
For me, it's unclear why this should be slower than your static casting.
If I were you, I would create a prototype code / stub using both methods and invoked it a million times and measured the performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With