Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by wahern on Wednesday April 19, 2023 @03:17AM
(#35625276)
Interface methods in Go are like virtual methods in C++. In principle C++ compilers when statically compiling everything can often remove virtual method indirection for some objects, but I think in general this optimization is still uncommon and difficult to coax out a compiler. Go definitely does not do this, even though in principle it should be easier.
Basically, "interfaces" in Go and C++ actually refer to quite different language features. (Or at least, the author is using the term to describe quite different language features.)
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by pjmlp on Wednesday April 19, 2023 @04:28AM
(#35625770)
This is common when doing LTO, without it there is no guarantee that there isn't some dynamically loaded code that would be broken, this is one area where JIT focused languages have an advantage.
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by gpderetta on Wednesday April 19, 2023 @05:27AM
(#35626134)
Indeed you need LTO for generalized devirtualization, but guarded devirtualization, static classes and final can still help even without LTO.
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by perfmode on Wednesday April 19, 2023 @03:17AM
(#35625277)
There is overhead in interface indirection because the Go runtime needs to perform dynamic dispatch to determine which method to call at runtime.
C++ can optimize interface indirection away because it supports static polymorphism, which allows the compiler to generate specialized code for each concrete type used with a generic interface, eliminating the need for dynamic dispatch.
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by travisd on Wednesday April 19, 2023 @03:14AM
(#35625256)
Go generally is pretty conservative about that kind of thing (namely, compiler optimizations). Go generally abides by a “what you write is what you get” kind of thing, especially when it comes to “non-local” optimizations. It’s generally opposed to anything that’s “clever.” (Just my feeling as someone who uses Go pretty often and who respects the choice they’ve made on that spectrum).
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by perfmode on Wednesday April 19, 2023 @03:18AM
(#35625286)
it’s actually to keep compile times fast
and for the implementation of the compiler to remain simple
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by travisd on Wednesday April 19, 2023 @03:20AM
(#35625303)
Yep, I think those things are all related.
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by pjmlp on Wednesday April 19, 2023 @06:53AM
(#35626632)
The first one is doable in more complex languages, e.g. D, Ada, Delphi,...
Re:Defining interfaces in C++ with ‘concepts’ (C++20)
by MarkMarine on Wednesday April 19, 2023 @03:16AM
(#35625267)
Interfaces always go to the heap in go, so yes, you can take some simple code that could easily live on the stack and make it slower by wrapping it in interfaces with go.
I'm surprised at this, do Go interfaces really introduce much overhead? Of course this depends on the level of performance you care about but surely, being a statically typed language, lots of the same optimisations are available.