Skip to content
0:00 / 0:00

#dynamicdispatch#vtable#dyntrait

Two Ways To Do Dynamic Dispatch

Logan Smith

51K subscribers • 6 videos on Hallway

YouTube
  • Recommended by 3 people: 3 in engineering communities.
  • Presented by Logan Smith
  • Linked in the descriptionldionne/dyno

Intermediate. Assumes working knowledge.

140K views3 years ago3 recommendations

Programming languagesSystems programmingRustC++Performance and optimization

Logan Smith compares how Rust and C++ implement dynamic dispatch, exploring vtables, wide pointers, and manual pointer management.

Rust and C++ both have built-in (but different-flavored) support for dynamic dispatch, and both also let you open the hood and implement it the other way that's not built-in. In this one we look at both languages' approaches, weigh the pros and cons, and ultimately come away seeing that they're both the right choice in different situations (isn't that just so unsatisfying and typical). Special guest appearances from vtables, vptrs, wide pointers, thunks, unsafe code, drop glue, virtual destructors, the Rule of Three, and me re-recording the live coding section like 5 times because I kept messing it up. the Sean Parent talk - youtube.com/watch?v=QGcVXgEVMJg godbolt to the demo - rust.godbolt.org/z/6YrK3vhGj dyno - github.com/ldionne/dyno NonNull - doc.rust-lang.org/stable/std/ptr/struct.NonNull.html thunk - en.wikipedia.org/wiki/Thunk std::function - en.cppreference.com/w/cpp/utility/functional/function std::any - en.cppreference.com/w/cpp/utility/any I use the amazing Manim library for animating these videos, and I edit them with Blender and Audacity. manim.community/ blender.org/ audacityteam.org/

Why it's here

3 people recommend this

What people say. Recommended for its clear explanation of how dynamic dispatch and dyn Trait work under the hood in Rust compared to C++.

u/MrEchowin r/rust • Recommends

On “Result<(), Box<dyn Error>> vs anyhow::Result<()>”

Here is a very informative video about how Rust handles dynamic dispatch compared to C++: link to this video Since Result is an enum, its size is the max size of its variant + a discriminant (not accounting for niche optimisation). So if for example you have Result<i32, Box<dyn Error>>, the size of the Result will be 24 bytes (16 bytes trait object + discriminant and padding for alignment) whereas with anyhow it will only be 16 bytes. Having smaller types in general will help with cache, might mean you can pass parameters by register instead of the stack etc..

u/ZZaaacccin r/rust • Recommends

On “Visualizing Rust's Vtables: How dyn Trait Works In Memory (Comparison to C++ CRTP & virtual functions)”

Great write-up! For anyone wanting some more information on this topic, Logan Smith has an excellent video on YouTube.

u/ZZaaacccin r/rust • Recommends

On “Which parts of Rust do you find most difficult to understand?”

That's something which makes a lot more sense once you try and implement `dyn Trait` yourself instead of letting the compiler do it. Logan Smith has a pretty good YouTube link to this video explaining it from first principles, mostly to contrast to C++.

u/MalbaCatoin r/rust • Recommends

On “What's the role of manual pointer and vtable management in Anyhow?”

This is not much of an answer to your question directly, but I really like linking to Logan's videos so here you go.