Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace AttributeValue::Text with Cow<'static, str> #2308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

matthunz
Copy link
Contributor

pub enum AttributeValue {
    Text(Cow<'static, str>),
    ...
}

This would allow for less clones when using text nodes.

However this does introduce the tradeoff of not automatically converting &str to String, only &'static str is permitted to reduce cloning.

Definitely open to changing any of this! 馃槃

@onweru
Copy link
Contributor

onweru commented Apr 14, 2024

However this does introduce the tradeoff of not automatically converting &str to String, only &'static str is permitted to reduce cloning.

Does this mean I won't be able to do this

#[component]
fn Text(class: String) {
  rsx! { 
     div { class, ... }
  }
}

Text {  class: "some-class" }

Hated having to

Text {  class: "some-class".into() } // or the to_string() 

@matthunz
Copy link
Contributor Author

matthunz commented Apr 14, 2024

Hated having to

Text {  class: "some-class".into() } // or the to_string() 

Unfortunately this would be a breaking change but I'd say the optional solution is #[props(into) class: Cow<'static, str> to avoid having to allocate the String.

Then you can still do
Text { class: "some-class" }

@ealmloff ealmloff added core relating to the core implementation of the virtualdom breaking This is a breaking change labels Apr 15, 2024
@ealmloff
Copy link
Member

ealmloff commented Apr 15, 2024

I would be interested to see benchmarks if this has a large performance impact.

Dioxus will currently not allocate with static strings inline in the rsx:

rsx! {
   div {
      // "hello world" is included in the template which only supports &'static str attributes
      width: "hello world",
   }
}

This PR would enable less allocation for the following code:

const STATIC: &str = "hello world";
const STATICS: &[&str] = &["hello world"];

let index = use_signal(|| 0);
rsx! {
   div {
      // Theoretically this could be part of the template as well, but we are currently not smart enough to know that it is static
      width: STATIC,
      // This shouldn't be part of the template because it is dynamic
      height: STATICS[index()],
   }
}

There is also some overlap with https://github.com/DioxusLabs/dioxus/pull/2258/files#diff-b96a80b4b2e2f0da84fe76143d9f213d159383fd322e41068eba85fcd7fe93f3 which introduces string type with static and dynamic segments (currently only used in debug mode for hot reloading)

@matthunz
Copy link
Contributor Author

matthunz commented Apr 17, 2024

TODO: Greg Johnston's new oco_ref crate might fit great here 馃憖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This is a breaking change core relating to the core implementation of the virtualdom
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants