c++ - Does the compiler optimize in this case? -
i using visual studio , creating game engine in c++, it's in 2d. purpose learn modern game engine architecture , component based systems in general. means have performance oriented mindset (even though performance isn't of issue when developing 2d on modern hardware).
would compiler optimze this?
template<class t> inline t & scene::accesscomponent(unsigned int id) { if (dynamic_cast(t*) < transform > ) { ... } else if (dynamic_cast(t*) < collidable > ) { ... } else if (dynamic_cast(t*) < anothercomponenttype > ) { ... } etc ... }
i heard if function enters same logic gate on sequential calls compiler can optimize future calls. work templated functions aswell? compiler optimize if t of same type on sequential calls?
i cant profile because code mess, different parts of engine allow accesscomponent call not implemented yet. kind of in planning phase.
this code should not done in performance-oriented code. suffers multiple branches. logically, there at least 2 branches every dynamic_cast check: branch generated compiler (to check rtti) , branch generated if
statement.
branch predictors in modern hardware powerful, limited. @ bare minimum, quantity limited, , if distribution of events more or less uniform, fail.
and when branch prediction succeeds, still loose cycles on branch calculation itself.
i see no reason dynamic cast @ all. since classes polymorphic, call corresponding virtual methods.
wiki
Comments
Post a Comment