Estimate project
← All posts

Static and Dynamic Dispatch in Rust

Each of the two mechanisms comes with its own pros and cons and brings extra considerations we have to keep in mind while maintaining our code.

12 min read
RustAsync

In this post, I’d like to dig into the fine details of static and dynamic function calls in Rust. Many of you probably link this subject straight to polymorphism in Rust, yet static vs. dynamic dispatch goes beyond polymorphism, traits, and trait objects; it’s a broader topic, which I hope to expand on below. Apart from the dispatch mechanisms as such, we’ll also look at related matters such as inlining, the Sized trait/method bounds, and dynamic dispatch in an async context – all of them connected in some way with how dispatching works in Rust.

What dispatch means

You can think of the word dispatch as simply a function call, and of static/dynamic dispatch as the way function calls get resolved. As I said at the start, the dispatch mechanism is often tied to polymorphism and to deciding which implementation of a given trait should be invoked, but it can also be viewed more widely, as something that applies to function and method calls in general. I’ll demonstrate both dispatch mechanisms in both settings, with and without polymorphism, so the concept is easier to grasp if you need it.

Static Dispatch

The most basic example of static dispatch is an ordinary function call in Rust:

fn compute(x: i32) -> i32 {
   x + 1
}

fn main() {
   let result = compute(5);
   println!("Result: {}", result);
}

The compute(5) call gets resolved during compilation. Static dispatch is the default, covering most ordinary function calls in which the concrete types are known at compile time. On reflection, this definition extends naturally to any generic case in which static dispatch occurs.

fn max_value<'a, T: PartialOrd>(a: &'a T, b: &'a T) -> &'a T {
   if a > b {
       a
   } else {
       b
   }
}

fn main() {
   // Using max_value with integers
   let num1 = 10;
   let num2 = 20;
   let max_num = max_value(&num1, &num2);
   println!("The maximum number is {}", max_num);

   // Using max_value with floating-point numbers
   let float1 = 15.5;
   let float2 = 8.75;
   let max_float = max_value(&float1, &float2);
   println!("The maximum float is {}", max_float);

   // Using max_value with chars
   let char1 = 'a';
   let char2 = 'z';
   let max_char = max_value(&char1, &char2);
   println!("The maximum char is {}", max_char);
}

Since the types can be mapped to concrete implementations during compilation, this is yet another case of static dispatch, just like the earlier one with plain functions.

The previous example relied on the PartialOrd trait with the default integer implementation, but to make things clearer we can define our own traits and implementations:

trait Speak {
   fn speak(&self) -> String;
}

struct Dog;
struct Cat;

impl Speak for Dog {
   fn speak(&self) -> String {
       "Woof".to_string()
   }
}

impl Speak for Cat {
   fn speak(&self) -> String {
       "Meow".to_string()
   }
}

fn make_noise<T: Speak>(animal: &T) {
   println!("{}", animal.speak());
}

fn main() {
   let dog = Dog;
   let cat = Cat;
   make_noise(&dog); // Here, the compiler knows to call Speak for Dog at compile time.
   make_noise(&cat); // Here, the compiler knows to call Speak for Cat at compile time.
}

The make_noise function takes any type implementing the Speak trait. In such situations, Rust relies on static dispatch to run the speak method for a concrete type. This is what happens:

  1. at compile time, the Rust compiler works out the concrete type of the argument (Dog or Cat).
  2. For every type, Rust produces a dedicated version of the make_noise function – giving one specific version for Dog and a separate one for Cat.

Because these calls are resolved during compilation, there is no need for runtime type checks or dynamic dispatch. Execution is therefore quicker and more predictable, since the function addresses are known in advance and baked into the code. Turning a generic type into several concrete function versions is known as monomorphization, and it makes calls to speak just as efficient as if no traits or generics were involved. Monomorphization has downsides too, though: it may lengthen compilation times and can reduce the effectiveness of the CPU instruction cache, as multiple copies of identical instructions may bloat it.

A note on #[inline] annotations

You’ll often hear that with generics and static dispatch, the compiler inlines functions for concrete types.

That’s only half true – during compilation, the usual inlining mechanisms are in effect. The Rust compiler is pretty good at judging when a function should be inlined, but that judgement hinges on several factors, such as how complex the function is and how often it gets called.

In plenty of situations, it may still pay off to add #[inline] or #[inline(always)] annotations explicitly. rustc is built to inline generic functions aggressively during monomorphization, especially when compiling with optimization flags like --release. When you come across performance-critical parts of your code, or have a small function that gets called often, try marking it explicitly with #[inline] or #[inline(always)] and check whether performance improves. These annotations can also come in handy for a utility function in a library crate that you think would gain from being inlined into the crates depending on it.

Dynamic Dispatch

As you have probably guessed, dynamic dispatch is quite different from static dispatch. It lets you invoke a trait method without knowing the object’s concrete type or implementation at compile time; the resolution takes place at runtime.

Taking our simple Dogs and Cats example, a dynamic version could look like this:

trait Speak {
   fn speak(&self) -> String;
}

struct Dog;
struct Cat;

impl Speak for Dog {
   fn speak(&self) -> String {
       "Woof".to_string()
   }
}

impl Speak for Cat {
   fn speak(&self) -> String {
       "Meow".to_string()
   }
}

fn make_noise(animal: &dyn Speak) {
   println!("{}", animal.speak());
}

fn main() {
   let dog = Dog;
   let cat = Cat;
   make_noise(&dog); // The specific method is looked up in the vtable at runtime.
   make_noise(&cat); // The specific method is looked up in the vtable at runtime.
}

What’s interesting here is, naturally, the make_noise function, whose argument is of type &dyn Speak. This time, the precise method to run is resolved at runtime via a virtual method table (vtable). Effectively, &dyn gives us the type and, when called at runtime, the address of the method for that type. We check those two pieces of information against the vtable to determine exactly which function to call.

Wide Pointers and Trait Objects

We pass animal to our function by address because at compile time the exact size of the argument is unknown, whereas the size of the pointer is known, so the code can be compiled. Since we are effectively passing not just a pointer to the type but also the address of a vtable where everything gets resolved, we need what’s called a wide pointer.

Not every wide pointer is a trait object, but every trait object is a pointer carrying extra information. Here, that means a pointer to data of a concrete type plus a pointer to a vtable holding that type’s implementations of the trait methods.

As it happens, any type capable of holding a wide pointer, such as Box and Arc, can be used for dynamic dispatch. Take a look at this example:

trait Animal {
   fn speak(&self);
}

struct Dog;
struct Cat;

impl Animal for Dog {
   fn speak(&self) {
       println!("Woof!");
   }
}

impl Animal for Cat {
   fn speak(&self) {
       println!("Meow!");
   }
}

fn main() {
   // Using & reference for dynamic dispatch
   let dog = Dog;
   let cat = Cat;
   let animals: Vec<&dyn Animal> = vec![&dog, &cat];
   for animal in animals {
       animal.speak();
   }

   // Using Box for dynamic dispatch
   let boxed_dog: Box<dyn Animal> = Box::new(Dog);
   let boxed_cat: Box<dyn Animal> = Box::new(Cat);
   boxed_dog.speak();
   boxed_cat.speak();

   // Using Arc for dynamic dispatch
   use std::sync::Arc;
   let arc_dog: Arc<dyn Animal> = Arc::new(Dog);
   let arc_cat: Arc<dyn Animal> = Arc::new(Cat);
   arc_dog.speak();
   arc_cat.speak();
}

The Self: Sized Trait Bound

The Sized trait bound, which is special in the context of dynamic dispatch, lets you control whether traits and methods may be used with dynamic dispatch. This can affect anyone using your traits or methods. With the Self: Sized bound, you can block the creation of trait objects and force static dispatch.

Most types in Rust implement the Sized trait automatically; it essentially says that the type’s size is known at compile time. Adding the Self: Sized bound to a trait or to a method inside a trait states that it may only be used with Sized types – that is, types whose size has to be known at compile time.

As explained in the previous section, we reach for a Trait Object precisely because we don’t know the size of the trait passed as an argument to our function. Trait objects are !Sized, meaning they aren’t Sized and their size isn’t known at compile time; the pointer inside a trait object may refer to any instance of an implementing type. Put differently, marking the trait as Sized declares that it can only be used with types of compile-time-known size, and that it cannot be used to build trait objects (so it can’t be used with dynamic dispatch).

trait MyTrait: Sized {
   fn do_something(&self);
}

struct MyStruct;

impl MyTrait for MyStruct {
   fn do_something(&self) {
       println!("Doing something!");
   }
}

fn use_trait_object(trait_obj: &dyn MyTrait) {
   trait_obj.do_something();  // This would not compile because MyTrait is not object-safe due to Self: Sized bound.
}

Putting the Self: Sized bound on one particular method of a trait makes just that method unavailable through a trait object, while the trait object itself can still be created and used.

trait MyTrait {
   fn do_something(&self) where Self: Sized;
   fn do_anything(&self);
}

struct MyStruct;

impl MyTrait for MyStruct {
   fn do_something(&self) {
       println!("Doing something specific!");
   }
   fn do_anything(&self) {
       println!("Doing anything!");
   }
}

fn use_trait_object(trait_obj: &dyn MyTrait) {
   trait_obj.do_something();  // This will not compile because do_something requires Self: Sized.
   trait_obj.do_anything();  // This works fine.
}

Dynamic Dispatch in async code

Rust traits don’t support async functions directly, and using them comes with extra challenges and subtleties worth knowing. An async method returns a Future, whose size isn’t known at compile time because it depends on the state captured by the async block, which by nature makes them !Sized.

Ordinary trait methods returning a Future can’t be used directly, since the unknown size of the futures stops them from being returned straight from a method in a dyn Trait. It’s similar to the restriction imposed by Self: Sized, only reversed – rather than the trait demanding a known size, the return type itself lacks a known size.

To get around this, we can return something Sized that wraps our future, so we can work with it afterwards. For this, trait methods can return Box<dyn Future<Output = T> + Send>. This approach wraps the future in a trait object, letting us deal with various future types through one common interface.

Carrying on with the Dog/Cat example, here’s the async version using the Box method:

use tokio;
use std::future::Future;
use std::pin::Pin;

trait Animal {
   fn speak(&self) -> Pin<Box<dyn Future<Output = String> + Send>>;
}

struct Cat;
struct Dog;

impl Animal for Cat {
   fn speak(&self) -> Pin<Box<dyn Future<Output = String> + Send>> {
       Box::pin(async move {
           let response = "Meow".to_string();
           println!("{}", response);
           response
       })
   }
}

impl Animal for Dog {
   fn speak(&self) -> Pin<Box<dyn Future<Output = String> + Send>> {
       Box::pin(async move {
           let response = "Woof".to_string();
           println!("{}", response);
           response
       })
   }
}

#[tokio::main]
async fn main() {
   let cat = Cat;
   let dog = Dog;

   let cat_says = cat.speak().await;
   let dog_says = dog.speak().await;

   println!("The cat says: {}", cat_says);
   println!("The dog says: {}", dog_says);
}

The speak method now has the return type Pin<Box<dyn Future<Output = String> + Send>>. That return type is a boxed future, which is Sized. Pin is used here to stop the future from being moved in memory once created. It’s a safety measure, as the Future itself may hold self-references. In both Dog and Cat, the async blocks are boxed by hand so they end up on the heap and a pointer to them is returned. The move keyword moves all captured variables into the future’s environment.

The Async_trait library

The previous example showed how to use Box<dyn Future<Output = T> + Send> as the return type of an async function in a trait, but there’s a better way. In fact, a library called async_trait takes care of this for us automatically.

use async_trait::async_trait;
use tokio;

#[async_trait]
trait Animal {
   async fn speak(&self) -> String;
}

struct Cat;
struct Dog;

#[async_trait]
impl Animal for Cat {
   async fn speak(&self) -> String {
       let response = "Meow".to_string();
       println!("{}", response);
       response
   }
}

#[async_trait]
impl Animal for Dog {
   async fn speak(&self) -> String {
       let response = "Woof".to_string();
       println!("{}", response);
       response
   }
}

#[tokio::main]
async fn main() {
   let cat = Cat;
   let dog = Dog;

   let cat_says = cat.speak().await;
   let dog_says = dog.speak().await;

   println!("The cat says: {}", cat_says);
   println!("The dog says: {}", dog_says);
}

async_trait gives us automatic boxing: thanks to the annotation, async trait methods are transformed at compile time to return Box<dyn Future<Output = T> + Send> (or Box<dyn Future<Output = T>> when Sendisn’t required), so we don’t have to write all the boilerplate around pinning and boxing; it’s handled for us.

Summary

To sum up, compared with static dispatch, dynamic dispatch shortens compile times because the same methods don’t have to be compiled over and over for different types. Likewise, the CPU instruction cache no longer gets bloated. Yet, like everything else, it has its downsides: fewer optimizations are applied when compiling our code, and nothing gets inlined for us. On top of that, there’s a small runtime cost for looking up the specific function in the vtable.

Any programmer should understand these mechanisms and their trade-offs when deciding how to design structs/traits and methods and what effect that may have on performance. Each of the two mechanisms comes with its own pros and cons and brings extra considerations we have to keep in mind while maintaining our code.