These methods use dynamic dispatch (receive a trait object &Debug
as parameter):
These methods use static dispatch and are written in terms of the related entry
method:
Why does the first list of methods use dynamic dispatch instead of static dispatch? If static dispatch was used, would their use be restricted?
Static dispatch uses monomorphization, which causes a separate copy of the code to be created for every concrete type.
When you have a function that is used with many concrete types, you may incur a large penalty at compile time to create and optimize all of these versions. This will happen even if monomorphization doesn't add performance in these cases.
Instead, you can choose to use a trait object, which creates a single implementation of the code (for &Trait
).
These methods were added as part of RFC 640, but the discussion doesn't seem to mention this aspect. In fact, they were originally implemented with static dispatch. Only later were they changed to accept a trait object:
Restructure debug builders to minimize codegen
Switching from generic bounds to trait objects and having un-inlined inner methods should cut down on the size of Debug impls, since we care about the speed of a Debug implementation way less than binary bloat.