On Concrete Type Monomorphization

โš“ Rust    ๐Ÿ“… 2026-04-30    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 3      

surdeus

A layered software architecture, where the internals of each layer is kept hidden - only exposed via dyn traits. We then realize that we have a hot loop, that executes dynamic dispatch on methods of the dynamic trait, which kills performance. We need to refactor the code such that logic from one layer is pushed down - in order to remove all the vtable lookups.

But wait, I came up with two small procedural macros โ€˜wrappableโ€™ and โ€˜wrappingโ€™ that allows for library authors to make the vtable boundary flexible. Basically, developers using the library are able to create blanket supertraits, enabling the returned dynamic traits to have extra methods - backed by concrete type monomorphization.

Basically, a function defined on the upper layer gets compiled and added to the vtable of each of the internal types from the underlying layer.

This is a pattern that can be applied with just a few lines of code (using these macros).

Before authoring this, I was having a hard time trying to find a solution to this. Any suggestions for an alternative way to add blanket implementations that get concrete type monomorphized (maybe a Nightly feature I need to know about)?

9 posts - 2 participants

Read full topic

๐Ÿท๏ธ Rust_feed