diff --git a/Cargo.toml b/Cargo.toml index ed0b4a886..4cd6e311c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,12 +46,15 @@ core = [] futures = "0.3.15" log = "0.4.14" num-format = "0.4.0" -rand = "0.8.4" separator = "0.4.1" serde = { version = "1.0.126", features = ["derive"] } surf = "2.2.0" env_logger = "*" async-std = { version = "1.9.0", features = ["attributes"] } +im-rc = "15.0.0" +rand = { version = "0.8.4", features = ["small_rng"] } +fxhash = "0.2.1" + [workspace] members = [ diff --git a/examples/_examples/framework_benchmark.rs b/examples/framework_benchmark.rs similarity index 74% rename from examples/_examples/framework_benchmark.rs rename to examples/framework_benchmark.rs index ef1410903..a2a7ea5d0 100644 --- a/examples/_examples/framework_benchmark.rs +++ b/examples/framework_benchmark.rs @@ -6,55 +6,45 @@ //! //! +use dioxus::{events::on::MouseEvent, prelude::*}; +use dioxus_html as dioxus_elements; +use fxhash::{FxBuildHasher, FxHasher32}; use std::rc::Rc; -use dioxus::events::on::MouseEvent; -use dioxus_core as dioxus; -use dioxus_core::prelude::*; -use dioxus_web::WebsysRenderer; -use dioxus_html as dioxus_elements; - - - fn main() { - wasm_logger::init(wasm_logger::Config::new(log::Level::Debug)); - console_error_panic_hook::set_once(); log::debug!("starting!"); - wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App)); + dioxus::desktop::launch(App, |c| c); } // We use a special immutable hashmap to make hashmap operations efficient type RowList = im_rc::HashMap, FxBuildHasher>; -// type RowList = im_rc::HashMap, nohash_hasher::BuildNoHashHasher>; static App: FC<()> = |cx| { - let (items, set_items) = use_state_classic(cx, || RowList::default()); - let (selection, set_selection) = use_state_classic(cx, || None as Option); + let items = use_state(cx, || RowList::default()); - let create_rendered_rows = move |from, num| move |_| set_items(create_row_list(from, num)); + let create_rendered_rows = move |from, num| move |_| items.set(create_row_list(from, num)); let append_1_000_rows = - move |_| set_items(create_row_list(items.len(), 1000).union(items.clone())); + move |_| items.set(create_row_list(items.len(), 1000).union((*items).clone())); let update_every_10th_row = move |_| { - let mut new_items = items.clone(); + let mut new_items = (*items).clone(); let mut small_rng = SmallRng::from_entropy(); - new_items - .iter_mut() - .step_by(10) - .for_each(|(_, val)| *val = create_new_row_label(&mut small_rng)); - set_items(new_items); + new_items.iter_mut().step_by(10).for_each(|(_, val)| { + *val = create_new_row_label(&mut String::with_capacity(30), &mut small_rng) + }); + items.set(new_items); }; - let clear_rows = move |_| set_items(RowList::default()); + let clear_rows = move |_| items.set(RowList::default()); let swap_rows = move |_| { // this looks a bit ugly because we're using a hashmap instead of a vec if items.len() > 998 { - let mut new_items = items.clone(); + let mut new_items = (*items).clone(); let a = new_items.get(&0).unwrap().clone(); *new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone(); *new_items.get_mut(&998).unwrap() = a; - set_items(new_items); + items.set(new_items); } }; @@ -83,7 +73,7 @@ static App: FC<()> = |cx| { } } } - table { + table { tbody { {rows} } @@ -93,23 +83,28 @@ static App: FC<()> = |cx| { }) }; +#[derive(Clone)] +struct RowController {} + #[derive(Props)] struct ActionButtonProps { name: &'static str, id: &'static str, action: F, } -fn ActionButton(cx: Context>) -> VNode { +fn ActionButton(cx: Context>) -> VNode +where + F: Fn(MouseEvent), +{ cx.render(rsx! { div { class: "col-sm-6 smallpad" - button {class:"btn btn-primary btn-block", r#type: "button", id: "{cx.id}", onclick: {&cx.action}, + button { class:"btn btn-primary btn-block", r#type: "button", id: "{cx.id}", onclick: {&cx.action}, "{cx.name}" } } }) } - #[derive(PartialEq, Props)] struct RowProps { row_id: usize, @@ -132,22 +127,25 @@ fn Row<'a>(cx: Context<'a, RowProps>) -> VNode { }) } -use fxhash::{FxBuildHasher, FxHasher32}; use rand::prelude::*; -fn create_new_row_label(rng: &mut SmallRng) -> Rc { - let mut label = String::new(); +fn create_new_row_label(label: &mut String, rng: &mut SmallRng) -> Rc { label.push_str(ADJECTIVES.choose(rng).unwrap()); label.push(' '); label.push_str(COLOURS.choose(rng).unwrap()); label.push(' '); label.push_str(NOUNS.choose(rng).unwrap()); - Rc::from(label) + Rc::from(label.as_ref()) } fn create_row_list(from: usize, num: usize) -> RowList { let mut small_rng = SmallRng::from_entropy(); + let mut buf = String::with_capacity(35); (from..num + from) - .map(|f| (f, create_new_row_label(&mut small_rng))) + .map(|f| { + let o = (f, create_new_row_label(&mut buf, &mut small_rng)); + buf.clear(); + o + }) .collect::() } diff --git a/examples/rsx_usage.rs b/examples/rsx_usage.rs index 1198c1f04..da5eef235 100644 --- a/examples/rsx_usage.rs +++ b/examples/rsx_usage.rs @@ -100,7 +100,6 @@ static Example: FC<()> = |cx| { // Using an "ID" associated with your data is a good idea. data.into_iter().map(|(k, v)| rsx!(li { key: "{k}" "{v}" })) }} - // Matching // Matching will throw a Rust error about "no two closures are the same type" @@ -147,7 +146,6 @@ static Example: FC<()> = |cx| { } } } - // Components // Can accept any paths diff --git a/packages/core-macro/examples/fc.rs b/packages/core-macro/examples/fc.rs index 77979f295..f328e4d9d 100644 --- a/packages/core-macro/examples/fc.rs +++ b/packages/core-macro/examples/fc.rs @@ -1,3 +1 @@ - - fn main() {} diff --git a/packages/core-macro/examples/rsxt.rs b/packages/core-macro/examples/rsxt.rs index 2106a24f2..2a6434b5d 100644 --- a/packages/core-macro/examples/rsxt.rs +++ b/packages/core-macro/examples/rsxt.rs @@ -1,5 +1,3 @@ - - pub mod dioxus { pub mod builder { pub struct Builder;