Abstraction without overhead: traits in Rust
blog.rust-lang.org
Abstraction without overhead: traits in Rust
1–10 of 150 posts
Re: Abstraction without overhead: traits in Rust
#2Re: Abstraction without overhead: traits in Rust
#3There is always overhead when adding abstractions--the only question is whether you pay at runtime or at compile time. C++ (and presumably Rust) choose the latter, Python and Go choose the former.
There's also overhead in the sense of complexity for the programmer, which isn't really either of those two.
Re: Abstraction without overhead: traits in Rust
#4There is always overhead when adding abstractions--the only question is whether you pay at runtime or at compile time. C++ (and presumably Rust) choose the latter, Python and Go choose the former.
Re: Abstraction without overhead: traits in Rust
#5This seems to have one major downside (pointers are twice as big), but lots of upsides:
- allows traits to be implemented for existing types, as opposed
to C++ where the type's declaration has to list all base classes.
- allows a type to be used through dynamic dispatch while allowing users
who don't need this to avoid the vtable overhead.
- one less indirection in the call sequence for dynamic dispatch.
I like it a lot overall, though the idea of 16-byte pointers on 64-bit architectures does make me slightly queasy.Re: Abstraction without overhead: traits in Rust
#6There is always overhead when adding abstractions--the only question is whether you pay at runtime or at compile time. C++ (and presumably Rust) choose the latter, Python and Go choose the former.
And why not mention the overhead of the programmer foregoing these "short cuts" and hand-coding it herself.
Re: Abstraction without overhead: traits in Rust
#7Great article. The main bit of new information for me was that while Rust supports dynamic dispatch, its implementation has a noticeable difference from C++. In C++, the vtable pointer is in the object itself. In Rust, it's stored inside what is essentially a "fat pointer." Pointers to traits ("trait objects") are actually two pointers: the pointer to the vtable and the pointer to the actual object. This seems to hav…
[1]: http://www.phpcompiler.org/articles/virtualinheritance.html
Edit: I don't mean to bash C++ here, BTW; the skinny-pointer approach has a lot of benefits when all you need is single inheritance (and there are early-stage proposals to add it to Rust too). But I don't think it works well for multiple inheritance.
Re: Abstraction without overhead: traits in Rust
#8Re: Abstraction without overhead: traits in Rust
#9Great article. The main bit of new information for me was that while Rust supports dynamic dispatch, its implementation has a noticeable difference from C++. In C++, the vtable pointer is in the object itself. In Rust, it's stored inside what is essentially a "fat pointer." Pointers to traits ("trait objects") are actually two pointers: the pointer to the vtable and the pointer to the actual object. This seems to hav…
Re: Abstraction without overhead: traits in Rust
#10Great article. The main bit of new information for me was that while Rust supports dynamic dispatch, its implementation has a noticeable difference from C++. In C++, the vtable pointer is in the object itself. In Rust, it's stored inside what is essentially a "fat pointer." Pointers to traits ("trait objects") are actually two pointers: the pointer to the vtable and the pointer to the actual object. This seems to hav…
It also makes multiple inheritance (which Rust has, through Java-like interfaces) easy to implement, and fast at runtime. The virtual inheritance of C++ is a real mess, by contrast [1]. [1]: http://www.phpcompiler.org/articles/virtualinheritance.html Edit: I don't mean to bash C++ here, BTW; the skinny-pointer approach has a lot of benefits when all you need is single inheritance (and there are early-stage proposals…