Visualizing Rust's Vtables: How dyn Trait Works In Memory

(sofiabelen.github.io)

152 points | by torutofu 15 hours ago

6 comments

  • tialaramex 7 hours ago
    This has a section on Object Safety, I checked and the article was written this week, but "Object Safety" is a confusing name for this idea, and so for a little while now Rust calls this idea "dyn compatibility" because the most important thing you're getting if a trait is "dyn compatible" is that you can use "dyn Trait" - https://doc.rust-lang.org/1.98.1/reference/items/traits.html...

    That link more comprehensively explains the rules too.

    [Edit: Realized the end of the article explains this, the author began writing it months ago, likely before they read about the improved "dyn compatibility" naming]

    • loeg 7 hours ago
      The aside about C++ seems a little confused too?

      > C++ doesn’t have this problem at all, since virtual dispatch always goes through pointers and return types are always pointers too.

      Uh. C++ can return objects by value. Maybe it isn't idiomatic. And I don't know if there's a convenient spelling like `Self`. But it runs into the same problem, of course -- you would need to know the concrete value type returned to have storage for it.

      And Rust has the ~same solution as imagined for C++ here, I think? Have a `DynClone` trait that returns `Box<dyn Trait>` instead of `Self`.

      • neerajsi 7 hours ago
        I agree with what you're saying. It's called: https://en.wikipedia.org/wiki/Object_slicing. Clearly a footgun in c++. You have to return a pointer to something that is not locally allocated.
      • gmueckl 4 hours ago
        Polymorphism in C++ can only be correct on pointers and references (which are pointers internally).

        Stack-allocated objects and polymorphism only combine if the object is initialized as its true type and a pointer or reference to it is handed out. Obviously, this can create object lifetime issues if the pointer or reference escapes the lifetime of the stack frame containing the object.

        • quotemstr 4 hours ago
          What does your explanation have to do with the fact that C++ can express by-value returns of complex objects?
          • mpyne 37 minutes ago
            Everything. The issue is that the compiler won't even bother with polymorphism through a vtable for a polymorphic type (one with a vtable), unless the object is accessed through a pointer or reference.

            If you have a value of the type itself (not a pointer or reference), then polymorphism doesn't even enter the equation in C++, even if you initialize from a derived type.

            E.g. in this code:

                Base b(m_catalog.makeDerived());
                b.call_virt_func();
            
            Even if `call_virt_func` is declared virtual, it will be `Base::call_virt_func()` that is called here, guaranteed. From a language perspective, we already know that `b` is a `Base`, you literally declared and defined it that way.

            Runtime polymorphism is therefore only a game for pointers or references; it is the process of resolving the indirection that even allows for polymorphism to become a thing in C++. But this means that the compiler cannot know the actual type at compile-time for a polymorphic type, unless it can perform devirtualization as an optimization pass.

            So although C++ will certainly allow you to define a class method that returns a virtual type by value (and not by pointer or reference), even for complex types, it is almost certainly a bug to do this unless you know for sure what the type will be statically, at compile time. Because the object you create as the return value will be forced to be the return type declared at compile time, "forgetting" the fact that it was created from a type deeper in the inheritance chain. This is the 'slicing problem' that was mentioned in the earlier comment.

  • evmar 3 hours ago
    In my own journey of discovery I found https://cheats.rs/ very helpful, and in particular its "memory layout" section has visualizations. (No affiliation with the site, just a happy reader!)
  • Waterluvian 4 hours ago
    I never did well in English and I usually don’t know it when I see it. But it was immediately evident that the writing style of this blog was sparking joy. Even the intro section was just so well structured that I had to read it again.
  • returningfory2 7 hours ago
    Very nice. As a follow up would be interesting to also reverse engineer the structure of the vtable itself. I guess it’s a list of pointers to the method implementations?
  • ketzu 9 hours ago
    > In Rust, that question is answered by the borrow-checker at compile time:

    (About zero sized objects being the same)

    But why does that mean the programmer never has to check? (or if they want that information from the borrow checker how would they get it?) It's not motivated as the intro above for c++ was just "In C++, we might do this to check if two pointers refer to the same object".

    So the borrow checker knows already, why does that stop the programmer from wanting to know or separate these cases?

    • steveklabnik 8 hours ago
      > if they want that information from the borrow checker how would they get it?

      If you want to compare if two pointers point to the same place, you use https://doc.rust-lang.org/stable/std/ptr/fn.eq.html

      Rust just defaults to value equality over reference equality. This is true for everything, not just ZSTs.

      (I find the post's framing of "it's stored in the borrow checker" to be a bit odd, but I can't put my finger on exactly what it is. The borrow checker doesn't determine these sorts of semantics, it checks for liveliness and aliasing, so "do these pointers alias" isn't inherently not the borrow checker's job, it just strikes me as an odd way to put it. Maybe it's because you don't "ask the borrow checker for that information" really.)

      • ketzu 8 hours ago
        As the article itself discusses, pointers to zero sized objects are not necessarily different (they write it is only the case in debug mode).

        > I find the post's framing of "it's stored in the borrow checker" to be a bit odd

        That's exactly what I wanted to say as well.

        I feel like it would have been better to just skip the borrow checker mention and just go "in rust this can not be done reliably ..(section about pointers being the same)"

    • TazeTSchnitzel 8 hours ago
      I'm not sure if this quite answers your question, but the difference in philosophy here is that C++ objects with pointers always have identity, whereas a Rust object only has identity if it has a non-zero size. It's an application of the zero-overhead principle, "you only pay for what you use".
      • phire 1 hour ago
        > whereas a Rust object only has identity if it has a non-zero size.

        Rust also has the complication that function pointers are not guaranteed to have an unique identity; If multiple functions compile to the same code, the compiler is allowed de-duplicate them.

        The documentation [0] also warns it's also possible for the compiler to create multiple versions of the same function. And while I've absolutely seen the compiler to create multiple optimised versions of functions in disassembled code (partial inlining based on the caller), I'm not sure it's possible to get pointers to more than one version.

        [0] https://doc.rust-lang.org/std/ptr/fn.fn_addr_eq.html

      • ketzu 8 hours ago
        "zero sized objects don't have identity" actually would answer my question and is a really interesting factoid, thanks!
        • tialaramex 7 hours ago
          This is why C++ doesn't have ZSTs, it wants all objects to have identities, the obvious way to distinguish them is by where they are in memory, but ZSTs don't have distinct addresses in memory.
          • ameliaquining 1 hour ago
            I'm not a C++ expert. Why does C++ want all objects to have identities? Presumably it has some feature or something Rust doesn't have that requires this?
            • mpyne 32 minutes ago
              It's pretty deeply tied into C++'s object lifetime mechanics, which rely on storage being available and reserved for the use of that object's lifetime. If multiple objects with valid lifetimes had a situation where one lifetime needs to end, what should happen to the other objects' lifetimes?

              C++ actually did end up evolving the ability to define a zero-sized class without a unique memory address, but mostly to allow optimizations like the empty base optimization to apply in other situations where it could make sense, especially with templated or constexpr code.

    • wrs 8 hours ago
      I've tried (admittedly for just a few minutes) to come up with a case where you'd need to compare two pointers to ZSTs in any real algorithm, and failed. Can you think of one?
    • loeg 8 hours ago
      It's not obvious to me that the borrow checker actually knows, so much as it can prove there are no illegal conflicts between mut and non-mut references to the same object.

      If you somehow need this property in your programs, I think you can just add a 1-byte member and use pointer equality. (I'm not a Rust expert.)

      • wahern 1 hour ago
        I assumed it's a figure of speech, but, yeah, the borrow checker only enforces function-local invariants, with no interprocedural analysis or memorization. Parameter aliasing is just a logical deduction from assuming local invariants are maintained at all call sites. That's why the borrow checker imposes very minimal cost (esp when it was block based), and doesn't even require static compilation. Traits and other aspects of the type system are where the complexity and slow compile times come from.

        I guess maybe the semantics of ZSTs complicate the aliasing story a tad?

  • 7e 4 hours ago
    The title includes the word “visualizing” but there is not a single diagram in this article. Is English not the first language of the author?