Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The only thing worse than no benchmark is a bad benchmark.

I don't think this really shows what `final` does, not to code generation, not to performance, not to the actual semantics of the program. There is no magic bullet - if putting `final` on every single class would always make it faster, it wouldn't be a keyword, it'd be a compiler optimization.

`final` does one specific thing: It tells a compiler that it can be sure that the given object is not going to have anything derive from it.



'Final' cannot be assumed without complete knowledge of all final linking cases, and knowledge that this will not change in the future. The latter can never be assumed by a compiler without indication.

"In theory" adding 'final' only gives a compiler more information, so should only result in same or faster code.

In practice, some optimizations improve performance for more expected or important cases (in the compiler writer's estimation), with worse outcomes in other less expected, less important cases. Without a clear understanding the when and how of these 'final' optimizations, it isn't clear without benchmarking after the fact, when to use it, or not.

That makes any given test much less helpful. Since all we know is 'final' was not helpful in this case. We have no basis to know how general these results are.

But it would be deeply strange if 'final' was generally unhelpful. Informationally it does only one purely helpful thing: reduce the number of linking/runtime contexts the compiler needs to worry about.


Not disagreeing with your point, but it couldn't be a compiler optimization, could it? The compiler isn't able to infer that the class will not be inherited anywhere else, since another compilation unit unknown to the class could inherit.


Possibly not in the default c++ language mode, but check out -fwhole-program-vtables. It can be a useful option in cases where all relevant inheritance relationships are known at compile time.

https://reviews.llvm.org/D16821


Which is good, but may not apply. I have an application where I can't do that because we support plugins and so a couple classes will get overridden outside of the compilation (this was in hindsight a bad decision, but too late to change now). Meanwhile most classes will never be overriden and so I use final to saw that. We are also a multi-repo project (which despite the hype I think is better for us than mono-repo), another reason why -f-whole-program-vtables would be difficult to use - but we could make it work with effort if it wasn't for the plugins.


I assume it could be or is part of link time optimization when compiling an application rather than a library?


> `final` does one specific thing: It tells a compiler that it can be sure that the given object is not going to have anything derive from it.

...and the compiler can optimize using that information.

(It could also do the same without the keyword, with LTO.)


LTO can only apply in specific situations though, if there is any possibility that a plugin derived from the class LTO can do nothing.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: