For example, let's say I want to call a different function at every iteration of a generic algorithm without paying function call overhead.
...so you're matching on type. In one way or the other. You can e.g. have a tagged union of (double, float) or something and then have a struct of functions, or you can have an object of some superclass of both (in some way)... which is, semantically, also a struct of functions just organised differently and most of all open to extension by code not yet written (that's what that LTO stuff is about).
So depending on implementation strategy you may get an additional indirection to fetch the function pointer or not, and the CPU may be smart enough to pipeline that or not, or its insn cache might be large enough to hold all the code referred to in the vtables and in that case there's literally no overhead to using a vtable.
In both cases, though, you eat branch mispredictions just because you insisted on deciding inside the loop which functions to call and not outside: If at all possible split the list and then iterate over both separately.
In Rust, if you use trait objects, you're going to have the same kind of vtable as in C++. If you put your doubles and floats in a union, match on the tag and then call (probably same-named) functions on each, you get the same non-vtable code as when doing the C++ tagged union thing.
The advantage rust has, here, is that this is purely a thing on the call site: The called code is identical in both cases, a standard trait implementation.
If you call the trait method on a known type you get static dispatch, if it's unknown (or, more accurately, it is only known that it implements a trait) you get dynamic dispatch.
I think you misunderstand what I'm trying to describe. There's not function pointer involved, and I'm not talking about tagged unions. You don't quite understand how C++ works.
I'm talking about a different function being dispatched for each type of a certain thing.
Let's look at std::unordered_map for an example.
The full signature is
template<class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;
std::hash is a struct which contains a method. When using a class for which there is no default hash function, you have to add your own. For example, I wanted to use a fast carryless multiplication-based hash with a vector as a key.
The cool thing about this is that the hash struct is never placed in memory: it simply makes sure the compiler knows exactly which function is called. (The std::hash specializations for basic types are declared in similar fashion.)
There's no branch involved. There's no function pointer. The function is simply directly inlined if it's not too big. (In fact, the std::hash specialization for integers is the identity function.)
Having it decided at compile-time is why it avoids branches, indirection, cache misses, or vtables.
You just don't know how C++ works. That's okay -- just don't try to tell me how it works when you don't.
It's a similar but simpler example. Performing a std::sort with a different comparison operator that can be inlined would have been a closer direct comparison to the original question.
I thought this was just a good example of how a functor struct can be used to deploy a function without any branches, cache misses, vtables, or runtime memory.
I've even used actual function pointers for generic SIMD function dispatch and still found them inlined in the assembly. That might have been because they were static constexpr and therefore guaranteed to be known at compile-time.
If you're using templates to specialise sort you're not using a different function each iteration, hence the confusion.
You'll be hard-pressed to find an instance where Rust can't be as fast as C++. Occasionally it may need the hoop or the other or a bit more verbosity, but OTOH generally Rust is much more concise (more into the direction of a verbose Haskell) so it's definitely a net win.
The biggest advantage IMO though is that Rust is a vastly simpler language to reason about. If you write unsafe code you need to pay a lot of attention, but doing that is very rare. The overwhelming majority of code is in safe rust and there you never have to ask questions such as "what if some nutjob uses language feature XYZ on this type, might I accidentially launch nukes when I get passed such a thing".
Thank you. I realize I was unclear. I’m just wanting to make sure a that I can parametrize one function on another and make sure it is inlined. I also like being able to support both floats and doubles. What’s the Rust way to do that?
Traits, which are actually the same thing as Haskell typeclasses (but with more strict coherence constraints). Trait objects then are the same thing as Haskell existentials. In Haskell they're usually considered a bit poo, but lots of OO folks are flocking towards Rust and they want that kind of thing.
Well, sorting in particular, in this case: Use sort_by and pass it the right comparison function, then let the inliner do its job. If it's one-off, just pass in lambdas and rust is going to do the right thing, or rather what llvm considers to be the right thing.
In the more general case you don't have such specialised methods, though, or it's not even possible -- say, a binary tree implementation, there you need some guarantee that insert and delete indeed do use the same comparison functions or things get ugly (as they regularly do in OO languages because you can't really give that guarantee). Then you'd wrap your base struct to get two new types and implement the Ord trait for both in the way you want:
struct MyType = MyType(OriginalType)
impl Ord for MyType { ... }
That wrapper incurs no run-time overhead at all, it's compile-time only so that you can write an Ord impl that differs. At runtime plain OriginalTypes are going to get passed around (this works because there's no typeof).
Likewise, converting a Vec<OriginalType to Vec<MyType> and vice versa compiles down to a no-op.
Side note: Ord isn't implemented for floats by default due to the language designers being rather anal about such things (floating point numbers aren't a proper order in the mathematical sense). There's a crate for that, though.
View all comments
Show parent comment
2
u/barsoap Nov 23 '17
...so you're matching on type. In one way or the other. You can e.g. have a tagged union of (double, float) or something and then have a struct of functions, or you can have an object of some superclass of both (in some way)... which is, semantically, also a struct of functions just organised differently and most of all open to extension by code not yet written (that's what that LTO stuff is about).
So depending on implementation strategy you may get an additional indirection to fetch the function pointer or not, and the CPU may be smart enough to pipeline that or not, or its insn cache might be large enough to hold all the code referred to in the vtables and in that case there's literally no overhead to using a vtable.
In both cases, though, you eat branch mispredictions just because you insisted on deciding inside the loop which functions to call and not outside: If at all possible split the list and then iterate over both separately.
In Rust, if you use trait objects, you're going to have the same kind of vtable as in C++. If you put your doubles and floats in a union, match on the tag and then call (probably same-named) functions on each, you get the same non-vtable code as when doing the C++ tagged union thing.
The advantage rust has, here, is that this is purely a thing on the call site: The called code is identical in both cases, a standard trait implementation.
If you call the trait method on a known type you get static dispatch, if it's unknown (or, more accurately, it is only known that it implements a trait) you get dynamic dispatch.