Skip to content

Commit

Permalink
feat(tachys): optimize AnyView + AddAnyAttr to reduce compile/link times
Browse files Browse the repository at this point in the history
- Pre-erased `AddAnyAttr` output to `AnyView` to prevent excessive monomorphizations.
- Introduced `DynValueAttr` to minimize deep generic recursion using dynamic dispatch.
- Marked heavy generic functions with `#[inline(never)]` to reduce code bloat.
- Ensured single dynamic call for attribute application to simplify logic and improve efficiency.

This change attempts to address compile-time blow-ups and linker crashes for large projects using `AnyView` with dynamic attributes.
  • Loading branch information
geoffreygarrett committed Jan 17, 2025
1 parent 17b3e26 commit 0db3233
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions tachys/src/view/any_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,36 @@ use std::{
#[cfg(feature = "ssr")]
use std::{future::Future, pin::Pin};

// Minimizes deep generics via a single dynamic dispatch
trait DynValueAttr: DynValue {
fn apply_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView;
}

trait DynValue: Send {
fn dyn_add_any_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView;

fn dyn_any(self: Box<Self>) -> Box<dyn Any + Send>;
}

impl<T> DynValueAttr for T
where
T: Send + RenderHtml + 'static,
T::State: 'static,
{
fn apply_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView {
self.add_any_attr(attr).into_any()
}
}

impl<T> DynValue for T
where
T: Send,
T: RenderHtml + 'static,
T::State: 'static,
{
#[inline(never)]
fn dyn_add_any_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView {
self.add_any_attr(attr).into_any()
DynValueAttr::apply_attr(self, attr)
}

fn dyn_any(self: Box<Self>) -> Box<dyn Any + Send> {
Expand Down Expand Up @@ -348,9 +364,11 @@ impl Render for AnyView {
}
}

// Pre-erases output to reduce compile-time explosions
impl AddAnyAttr for AnyView {
type Output<SomeNewAttr: Attribute> = Self;
type Output<SomeNewAttr: Attribute> = AnyView;

#[inline(never)]
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
Expand All @@ -359,7 +377,8 @@ impl AddAnyAttr for AnyView {
Self::Output<NewAttr>: RenderHtml,
{
let attr = attr.into_cloneable_owned();
self.value.dyn_add_any_attr(attr.into_any_attr())
let any_attr = attr.into_any_attr();
self.value.dyn_add_any_attr(any_attr)
}
}

Expand Down

0 comments on commit 0db3233

Please sign in to comment.