News for hackers. Stuff that matters.
Re:“Clean” Code, Horrible Performance by zelos on Tuesday February 28, 2023 @04:44AM (#34967490)
That does depend on how the abstraction is defined, of course. I once worked on optimising a 2D Canvas C++ class which had a nice top level virtual interface so you could replace it with a different implementation. It was also crazily slow because it defined:

  virtual void setPixel(int x, int y, int color) = 0;
and then implemented flood fill etc in terms of that.
Re:“Clean” Code, Horrible Performance by int_19h on Tuesday February 28, 2023 @06:31PM (#34976464)
This isn't necessarily bad if all the other operations are also virtual. The idea is that you can quickly get something working (e.g. when porting) just by implementing setPixel(), and then gradually fill in other primitives with properly hardware-optimized versions. And you do need a virtual setPixel() in any case because some API client might need that one call.
Re:“Clean” Code, Horrible Performance by Arelius on Tuesday February 28, 2023 @07:33PM (#34977107)
I'm sorry, why do you need a virtual setPixel?

You can still easily get something working just by implementing setPixel without virtual dispatch, the linker has no problem inlining that call at compile time.

If some arbitrary API needs it to be virtual it's easy to implement the virtual call in just that specific case, instead of burdening your entire system with a virtual call that'll always be static in practice.

Re:“Clean” Code, Horrible Performance by int_19h on Tuesday February 28, 2023 @08:35PM (#34977618)
For the same reason why you need a virtual drawLine etc. Because some code wants to render something, and doesn't want to care if it's rendering onto an on-screen surface, into a file etc.

Your entire system will not be burdened with virtual calls in places where you use concrete implementations (so long as they're final/sealed, anyway). The overhead is only there if you try to use the abstraction generically, but why would you do that in the case where the virtual call "will always be static in practice"?