framework_benchmark.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. use dioxus::{events::MouseEvent, prelude::*};
  2. use fxhash::FxBuildHasher;
  3. use std::rc::Rc;
  4. fn main() {
  5. dioxus::desktop::launch(App, |c| c).unwrap();
  6. }
  7. // We use a special immutable hashmap to make hashmap operations efficient
  8. type RowList = im_rc::HashMap<usize, Rc<str>, FxBuildHasher>;
  9. static App: FC<()> = |cx, _props| {
  10. let items = use_state(cx, || RowList::default());
  11. let create_rendered_rows = move |from, num| move |_| items.set(create_row_list(from, num));
  12. let append_1_000_rows =
  13. move |_| items.set(create_row_list(items.len(), 1000).union((*items).clone()));
  14. let update_every_10th_row = move |_| {
  15. let mut new_items = (*items).clone();
  16. let mut small_rng = SmallRng::from_entropy();
  17. new_items.iter_mut().step_by(10).for_each(|(_, val)| {
  18. *val = create_new_row_label(&mut String::with_capacity(30), &mut small_rng)
  19. });
  20. items.set(new_items);
  21. };
  22. let clear_rows = move |_| items.set(RowList::default());
  23. let swap_rows = move |_| {
  24. // this looks a bit ugly because we're using a hashmap instead of a vec
  25. if items.len() > 998 {
  26. let mut new_items = (*items).clone();
  27. let a = new_items.get(&0).unwrap().clone();
  28. *new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone();
  29. *new_items.get_mut(&998).unwrap() = a;
  30. items.set(new_items);
  31. }
  32. };
  33. let rows = items.iter().map(|(key, value)| {
  34. rsx!(Row {
  35. key: "{key}",
  36. row_id: *key as usize,
  37. label: value.clone(),
  38. })
  39. });
  40. cx.render(rsx! {
  41. div { class: "container"
  42. div { class: "jumbotron"
  43. div { class: "row"
  44. div { class: "col-md-6", h1 { "Dioxus" } }
  45. div { class: "col-md-6"
  46. div { class: "row"
  47. ActionButton { name: "Create 1,000 rows", id: "run", onclick: {create_rendered_rows(0, 1_000)} }
  48. ActionButton { name: "Create 10,000 rows", id: "runlots", onclick: {create_rendered_rows(0, 10_000)} }
  49. ActionButton { name: "Append 1,000 rows", id: "add", onclick: {append_1_000_rows} }
  50. ActionButton { name: "Update every 10th row", id: "update", onclick: {update_every_10th_row} }
  51. ActionButton { name: "Clear", id: "clear", onclick: {clear_rows} }
  52. ActionButton { name: "Swap rows", id: "swaprows", onclick: {swap_rows} }
  53. }
  54. }
  55. }
  56. }
  57. table {
  58. tbody {
  59. {rows}
  60. }
  61. }
  62. span {}
  63. }
  64. })
  65. };
  66. #[derive(Props)]
  67. struct ActionButtonProps<'a> {
  68. name: &'static str,
  69. id: &'static str,
  70. onclick: &'a dyn Fn(MouseEvent),
  71. }
  72. fn ActionButton<'a>(cx: Context<'a>, props: &'a ActionButtonProps) -> DomTree<'a> {
  73. rsx!(cx, div { class: "col-sm-6 smallpad"
  74. button { class:"btn btn-primary btn-block", r#type: "button", id: "{props.id}", onclick: {props.onclick},
  75. "{props.name}"
  76. }
  77. })
  78. }
  79. #[derive(PartialEq, Props)]
  80. struct RowProps {
  81. row_id: usize,
  82. label: Rc<str>,
  83. }
  84. fn Row<'a>(cx: Context<'a>, props: &'a RowProps) -> DomTree<'a> {
  85. rsx!(cx, tr {
  86. td { class:"col-md-1", "{props.row_id}" }
  87. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  88. a { class: "lbl", "{props.label}" }
  89. }
  90. td { class: "col-md-1"
  91. a { class: "remove", onclick: move |_| {/* remove */}
  92. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  93. }
  94. }
  95. td { class: "col-md-6" }
  96. })
  97. }
  98. use rand::prelude::*;
  99. fn create_new_row_label(label: &mut String, rng: &mut SmallRng) -> Rc<str> {
  100. label.push_str(ADJECTIVES.choose(rng).unwrap());
  101. label.push(' ');
  102. label.push_str(COLOURS.choose(rng).unwrap());
  103. label.push(' ');
  104. label.push_str(NOUNS.choose(rng).unwrap());
  105. Rc::from(label.as_ref())
  106. }
  107. fn create_row_list(from: usize, num: usize) -> RowList {
  108. let mut small_rng = SmallRng::from_entropy();
  109. let mut buf = String::with_capacity(35);
  110. (from..num + from)
  111. .map(|f| {
  112. let o = (f, create_new_row_label(&mut buf, &mut small_rng));
  113. buf.clear();
  114. o
  115. })
  116. .collect::<RowList>()
  117. }
  118. static ADJECTIVES: &[&str] = &[
  119. "pretty",
  120. "large",
  121. "big",
  122. "small",
  123. "tall",
  124. "short",
  125. "long",
  126. "handsome",
  127. "plain",
  128. "quaint",
  129. "clean",
  130. "elegant",
  131. "easy",
  132. "angry",
  133. "crazy",
  134. "helpful",
  135. "mushy",
  136. "odd",
  137. "unsightly",
  138. "adorable",
  139. "important",
  140. "inexpensive",
  141. "cheap",
  142. "expensive",
  143. "fancy",
  144. ];
  145. static COLOURS: &[&str] = &[
  146. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  147. "orange",
  148. ];
  149. static NOUNS: &[&str] = &[
  150. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  151. "pizza", "mouse", "keyboard",
  152. ];