this post was submitted on 01 Aug 2023
13 points (100.0% liked)

C++

2232 readers
36 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
top 2 comments
sorted by: hot top controversial new old
[–] addie@feddit.uk 7 points 2 years ago (1 children)

tl:dr; making a class final allows the compiler to save a vtable lookup in some (limited, slightly artificial) places where it couldn't before. No actual performance measurements.

I am sceptical. I'd understood vtable lookups to be extremely cheap on modern architecture; usually cheaper than the if / switch statement you'd have to write as an alternative if you weren't using inheritance. And for really performance-sensitive code, you would never have used virtualized classes anyway. I'd like to see some proper performance results before mangling code and making maintenance difficult.

[–] lysdexic@programming.dev 4 points 2 years ago

I am sceptical. I’d understood vtable lookups to be extremely cheap on modern architecture; usually cheaper than the if / switch statement you’d have to write as an alternative if you weren’t using inheritance.

They are relatively cheap, but I think cheap does not mean free and some references mention a performance hit that can be as high as 7%.

And for really performance-sensitive code, you would never have used virtualized classes anyway.

Yeah, because of this performance hit.

One of the cool things about this article is the fact that it points out how adding 'final' can magically get rid of that performance hit.