I think we're talking across each other. I do not want to fight you, my aim is to show you some things instead. I wanted to grab some time to provide you with some examples and links, which I couldn't do mobile. Sorry for the late-ish reply.
In C++ if you're using a base class with virtual functions, the optimizer can't inline it.
When the compiler has total visibility of an abstract virtual class as well as its implementations, it is, to the contrary, able to optimize through a process of "devirtualization". Here are some examples:
One TU gets fully inlined. This makes sense -- the compiler can trivially prove that there is only one possible implementation for this call.
Two TUs without LTO fails to get devirtualized. The compiler can no longer prove that it doesn't need to emit a vtable.
Two TUs with LTO get correctly devirtualized, however. Because, although the compiler doesn't have total visibility into the inheritance tree, it builds sufficient meta-information that to enable the linker, which then does.
LTO is a very standard, common optimization flag (-flto). This is to say that code that's compiled with standard optimization flags (ie. released code), will get devirtualized. Admittedly, my assumptions are with modern GCC. I have also had the misfortune of working with ancient embedded toolchains that failed to devirtualize. In my experience, however, for the vast majority of x86_64/ARM64 code, devirtualization mostly succeeds, and no uncommon complex tricks were used.
That all being said, when it comes to C++, if you want to guarantee that your interfaces are compile-time polymorphic, the correct pattern is CRTPexample (or more modern, concepts example). You might, in your fervor, dismiss compile-time polymorphism as useless. However, please note how this can be useful in defining the ontology of "what an I2C driver is" while you can have many different implementations for this driver. Caller code can take this I2C concept, without worrying about the driver itself.
This gracefully brings me to this point:
I have a small standard library (for C++) and I don't use an interface for iterators. For the most part I pass around a slice. Since implementing it I haven't ran into a situation where I needed to iterate or something generated except maybe files from a directory, but I prefer to solve it by doing something else which is too long for a comment
Firstly, if you want to post some controversial things, saying "something else which is too long for a comment" is somewhat disingenuous as I don't think you're sufficiently engaging with your audience. You have this "solution" to iterators, but you hide them behind disinterest. That's slightly insulting. I am now sitting at 2:38AM writing you a post which I hope will prove useful whereas you simply say "too long to comment".
With that aside out of the way, I am curious about your standard library. When I had mentioned the standard library, I was specifically referring to C++' STL (standard template library). Before I get back to interfaces, if you're trying to iterate over files, I would suggest having a look at std::filesystem::recursive_directory_iterator which is one of the utilities in the STL which does precisely what you need. The STL does, in fact, have some problems (as most things in life), however, for the most part, it is a very useful tool.
Interfaces are frequently used in the standard library. One of the problems that a std::span (the C++ name for slice), cannot encapsulate is iteration over non-contiguous memory. Let's say you want to copy data from one vector to another. You could write a function like
This, unfortunately, does have a run-time cost, however tiny it is. The std::span is created and passed (on some arcane architectures---via the stack rather than registers) into Copy. But, you argue---it doesn't matter. And, fair enough---it doesn't.
However, a time comes along when you need to copy elements out of a linked list. Unfortunately, a span is contiguous memory since, for the most part it's defined as
You spend some time thinking and you decide you need some sort of concept that expresses the idea of "here's an object that we can read and iterate over". Since this an iterator, you call it an "iterator", and since it's an input to your function, you call it an "input iterator", so you write
As we've learned, with the power of concepts, we can make it so that there is no runtime cost associated with it, so we make it a concept
template<typename T>
concept InputIterator =requires(T t){// We can "increment the iterator"
t++;// We can check if there's a next element in the iterator
{ t.done()}->std::convertible_to<bool>;// We can "dereference" the iterator to get the value currently pointed to.
{*t }->std::same_as<T>;}
Beautiful! This works with linked lists, vectors, arrays, spans, ArrayLists, graphs (because you can iterate nodes), whatever! You feel good about yourself, so you decide to create a new "output interface" and your copy is now super generic:
That's awesome. Good job! Luckily for you, Alexander Stepanov already had this idea 30 years ago and we already have both std::input_iterator and std::output_iterator in the standard library. These generic interface concepts provide you with many useful STL algorithms:
std::copy(std::output_iterator, std::input_iterator) does what we implemented.
std::transform(std::output_iterator, std::input_iterator) is like copy, except it allows you to pass a function to transform the object at each copy.
std::copy_if(std::output_iterator, std::input_iterator) is like copy, except it allows you to pass a function to decide whether you want to copy.
std::merge(std::output_iterator, std::input_iterator, std::input_iterator) takes two iterators and merges them together. It's super cool---it can merge an array and a vector, an array and a set, a list and a set. All this with the power of interfaces.
IMO it's pointless finding a open source project and checking how many times there's more than one implementation for an interface, and how much faster something would be if they didn't use it. Do you really not think an optimizer won't inline when the function is virtual? I wrote the article with non-system languages in mind.
There is no pointless curiosity. Are you really certain in your claims? If you are so certain, can you prove it? Perhaps the results are surprising. You haven't done the work to research the claim in either direction. I've shared some assembly wherein virtuals get completely optimized. Are things really as true as your conviction, or is it possible that you are wrong? If you are right, when are you right? Why are you right? That is the evidence I would have enjoyed to read. Look how much effort I put to respond to your, as you admit---shitpost---I want to provide you with a different perspective. I want to offer you evidence (to the best of my ability), to help widen your horizons. I want us to advance our craft, and that means widening each other's horizons. Why do you not respect me the same?
I'm not sure what your language of choice is, since it doesn't feel like it's C++, but there is so much to unpack in any other language too. In Python, for example, MRO is likely to result in some pretty awful performance when used naively. I don't exactly know---I haven't benchmarked nor profiled. What can a Python engineer do to avoid it? Why is MRO so deeply problematic? What could a Python engineer do instead? Can we improve the performance in CPython? Is there a way to do "compile-time" (ie. type-check-time) interfaces without paying the price of runtime MRO? Why does Python use MRO instead of vtables?
There are many interesting rabbit holes you can go down and explore this. In my experience, when it comes to these sorts of things, my gut convictions are often wrong to a lesser or greater degree. Finding evidence for my convictions is how I learn how I am wrong, and how I remain curious.
I really wish you take programming as a curious and fun endeavor, and you change your apathetic perspective.
I don't know you, but I wish you well and I wish you find truth not in conviction, but in researching truth itself.
When I wrote the article I had C# and Java in mind, but I write a lot of C++ so I'll comment about C++
When the compiler has total visibility of an abstract virtual class as well as its implementations, it is, to the contrary, able to optimize through a process of "devirtualization".
I personally like to do a unity build with gcc using -O2 for optimizations, I haven't really bothered with LTO. Many people don't do either, but that's what I had in mind when I said virtual functions aren't inlined. When a function is large enough I'm not sure if lto is enough to get it inlined.
LTO is a very standard
I don't disagree
++, if you want to guarantee that your interfaces are compile-time polymorphic, the correct pattern is CRTP example
I rather not have so many templates. I know they're somewhat fast when there's little to no code inside the template but it seems like this would put a lot of code in headers and make things SLOW
Right now my C++ project compiles in 7 seconds and incremental builds are <2, often <1sec. I mostly can do it because a lot of the time I touch files that only include project headers and my standard lib. This is with UB sanitizer on too.
Really, I don't think there's reasons to use an interface except the two cases I mentioned (creating a tree from data, having an IO interface in a standard lib). No need for CRTP
if you want to post some controversial things, saying "something else which is too long for a comment" is somewhat disingenuous
My standard lib has nothing to do with anything in the article
You have this "solution" to iterators, but you hide them behind disinterest
No, it's really just using a slice (or C++ 'span') with the type you want. Only directory iterators are different which is in my wip async lib. The problem I've been thinking about is how to make it usable and efficient (at least having little bookkeeping). There's no promises, locks, and I'm a little worried I'll end up causing pain with how everything is a callback. I don't think a large switch table is the right move. But anyway, this isn't related to the article either. No interfaces in my async lib either.
Interfaces are frequently used in the standard library. One of the problems that a std::span (the C++ name for slice), cannot encapsulate is iteration over non-contiguous memory
Yep, I hit that problem too. In one case I used a span<span<T>>. In another I called myobj.get() which gave me a span, and if it was zero length it was over. Yes, it's an extra loop but it also let the optimizer unroll the inner loop if it wanted to. Another benefit is instead of calling next() every time I call it once per chunk
power of concepts
I like the requires statement, I use it all the time. My lib only supports C++20 and higher
If you are so certain, can you prove it?
I seen it at a few workplaces, and I also don't really care about other peoples programming opinions. It was meant to be a short post, not a research paper. I don't think anyone even cares about a thing I said in the article except maybe you
I'm not sure what your language of choice is, since it doesn't feel like it's C++
I like C# and C++. Oddly enough I hate all system languages except for C++ (including C, unless its small and a lib). I also think C++ is bad but it's still my favorite
I wonder if you'll like any of my other articles. They're on the homepage
I also don't really care about other people's programming opinions
Okay then! Conversation over, I guess! Why would I pour so much time and effort if you're not going to care? You're a jerk but here really I'm the idiot.
Are you new to reddit? Have you seen what people say here? Even in person I seen some silly suggestions that more than a handful of people believe. I have to know the person first before I take anything they say seriously.
I did reply to you, and I think everything I said was clear besides the above? IDK if you think I'm 'wrong' because I disagreed with some of the things you said
View all comments
Show parent comment
1
u/siva_sokolica Oct 12 '26
I think we're talking across each other. I do not want to fight you, my aim is to show you some things instead. I wanted to grab some time to provide you with some examples and links, which I couldn't do mobile. Sorry for the late-ish reply.
When the compiler has total visibility of an abstract virtual class as well as its implementations, it is, to the contrary, able to optimize through a process of "devirtualization". Here are some examples:
LTO is a very standard, common optimization flag (
-flto). This is to say that code that's compiled with standard optimization flags (ie. released code), will get devirtualized. Admittedly, my assumptions are with modern GCC. I have also had the misfortune of working with ancient embedded toolchains that failed to devirtualize. In my experience, however, for the vast majority of x86_64/ARM64 code, devirtualization mostly succeeds, and no uncommon complex tricks were used.That all being said, when it comes to C++, if you want to guarantee that your interfaces are compile-time polymorphic, the correct pattern is CRTP example (or more modern, concepts example). You might, in your fervor, dismiss compile-time polymorphism as useless. However, please note how this can be useful in defining the ontology of "what an I2C driver is" while you can have many different implementations for this driver. Caller code can take this I2C concept, without worrying about the driver itself.
This gracefully brings me to this point:
Firstly, if you want to post some controversial things, saying "something else which is too long for a comment" is somewhat disingenuous as I don't think you're sufficiently engaging with your audience. You have this "solution" to iterators, but you hide them behind disinterest. That's slightly insulting. I am now sitting at 2:38AM writing you a post which I hope will prove useful whereas you simply say "too long to comment".
With that aside out of the way, I am curious about your standard library. When I had mentioned the standard library, I was specifically referring to C++' STL (standard template library). Before I get back to interfaces, if you're trying to iterate over files, I would suggest having a look at
std::filesystem::recursive_directory_iteratorwhich is one of the utilities in the STL which does precisely what you need. The STL does, in fact, have some problems (as most things in life), however, for the most part, it is a very useful tool.Interfaces are frequently used in the standard library. One of the problems that a
std::span(the C++ name for slice), cannot encapsulate is iteration over non-contiguous memory. Let's say you want to copy data from one vector to another. You could write a function likeOne day you say you'd like to copy out of an array into a vector, so you say that
std::spanis a "generalization" of those:This, unfortunately, does have a run-time cost, however tiny it is. The
std::spanis created and passed (on some arcane architectures---via the stack rather than registers) intoCopy. But, you argue---it doesn't matter. And, fair enough---it doesn't.However, a time comes along when you need to copy elements out of a linked list. Unfortunately, a span is contiguous memory since, for the most part it's defined as
You spend some time thinking and you decide you need some sort of concept that expresses the idea of "here's an object that we can read and iterate over". Since this an iterator, you call it an "iterator", and since it's an input to your function, you call it an "input iterator", so you write
As we've learned, with the power of concepts, we can make it so that there is no runtime cost associated with it, so we make it a concept
Beautiful! This works with linked lists, vectors, arrays, spans, ArrayLists, graphs (because you can iterate nodes), whatever! You feel good about yourself, so you decide to create a new "output interface" and your copy is now super generic:
That's awesome. Good job! Luckily for you, Alexander Stepanov already had this idea 30 years ago and we already have both
std::input_iteratorandstd::output_iteratorin the standard library. These generic interface concepts provide you with many useful STL algorithms:std::copy(std::output_iterator, std::input_iterator)does what we implemented.std::transform(std::output_iterator, std::input_iterator)is like copy, except it allows you to pass a function to transform the object at each copy.std::copy_if(std::output_iterator, std::input_iterator)is like copy, except it allows you to pass a function to decide whether you want to copy.std::merge(std::output_iterator, std::input_iterator, std::input_iterator)takes two iterators and merges them together. It's super cool---it can merge an array and a vector, an array and a set, a list and a set. All this with the power of interfaces.There are many more advanced (and admittedly much more useful) algorithms in the STL too. I would suggest watching C++ Seasoning by the amazing Sean Parent and, if this keeps interesting you and as advance your understanding---reading Stepanov's Elements of Programming. This is probably the most important programming book, period.
There is no pointless curiosity. Are you really certain in your claims? If you are so certain, can you prove it? Perhaps the results are surprising. You haven't done the work to research the claim in either direction. I've shared some assembly wherein virtuals get completely optimized. Are things really as true as your conviction, or is it possible that you are wrong? If you are right, when are you right? Why are you right? That is the evidence I would have enjoyed to read. Look how much effort I put to respond to your, as you admit---shitpost---I want to provide you with a different perspective. I want to offer you evidence (to the best of my ability), to help widen your horizons. I want us to advance our craft, and that means widening each other's horizons. Why do you not respect me the same?
I'm not sure what your language of choice is, since it doesn't feel like it's C++, but there is so much to unpack in any other language too. In Python, for example, MRO is likely to result in some pretty awful performance when used naively. I don't exactly know---I haven't benchmarked nor profiled. What can a Python engineer do to avoid it? Why is MRO so deeply problematic? What could a Python engineer do instead? Can we improve the performance in CPython? Is there a way to do "compile-time" (ie. type-check-time) interfaces without paying the price of runtime MRO? Why does Python use MRO instead of vtables?
There are many interesting rabbit holes you can go down and explore this. In my experience, when it comes to these sorts of things, my gut convictions are often wrong to a lesser or greater degree. Finding evidence for my convictions is how I learn how I am wrong, and how I remain curious.
I really wish you take programming as a curious and fun endeavor, and you change your apathetic perspective.
I don't know you, but I wish you well and I wish you find truth not in conviction, but in researching truth itself.