-
type Children = Box<dyn FnOnce(Scope) -> Fragment>;
pub fn Component(
cx: Scope,
children: Option<Children>,
) -> impl IntoView {
...
}
pub fn Component<Children>(
cx: Scope,
children: Option<Children>,
) -> impl IntoView
where
Children: FnOnce(Scope) -> Fragment,
{ ... } Why use the dynamic dispatch |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Fairly simple reason: in terms of performance the primary constraints on WASM are binary size and the actual DOM rendering process in the browser. Adding a generic bound to a component will cause the Rust compiler to monomorphize it, creating a completely new copy of the component code for each different generic. Because closures are always unique types, this would copy the size of the component code x the number of times you used it. There's no difference in terms of the DOM rendering costs between the two, so there's no difference there. And the cost of dynamic dispatch is small relative to that larger rendering cost. If you find yourself in a situation in which you think it's making a difference, you can simply use a "render prop" (i.e., passing a function as a prop) instead of |
Beta Was this translation helpful? Give feedback.
Fairly simple reason: in terms of performance the primary constraints on WASM are binary size and the actual DOM rendering process in the browser. Adding a generic bound to a component will cause the Rust compiler to monomorphize it, creating a completely new copy of the component code for each different generic. Because closures are always unique types, this would copy the size of the component code x the number of times you used it.
There's no difference in terms of the DOM rendering costs between the two, so there's no difference there. And the cost of dynamic dispatch is small relative to that larger rendering cost.
If you find yourself in a situation in which you think it's making a …