0
\$\begingroup\$

If I'm making a small library for handling 2D shapes, should polymorphism generally be avoided? I know that it can worsen performance but it's not like polymorphism is never used in game development so I'm unsure of where it actually isn't appropriate.

If we assume we have quite many shapes that all need constant collision detection and similar, how detrimental will polymorphism be to performance? I ask because it's tempting to have an abstract Shape that concrete child classes like Polygon and Circle could inherit from, allowing me to iterate over and handle different kinds of shapes quite smoothly. But will calling purely virtual functions of Shape many many times cause performance hits? I'm not too familiar with exactly how polymorphism can affect performance.

\$\endgroup\$
2
  • \$\begingroup\$ We have some past Q&A about polymorphic approaches to collision detection that may have useful tips for you. \$\endgroup\$
    – DMGregory
    Sep 4, 2023 at 21:17
  • \$\begingroup\$ I feel like you are getting into the trap of premature optimisation. Calling a virtual function versus calling a non-virtual function shouldn't have any noticeable performance impact. Make something that works, regardless if it uses polymorphism or not, and if, later on, your profiling shows that part of the code to be impacting performance, you can re-visit it and optimise it as needed. \$\endgroup\$ Sep 6, 2023 at 12:56

0

You must log in to answer this question.

Browse other questions tagged .