perf: take Tree::new items by reference (#32)

Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
This commit is contained in:
sandercm
2024-05-16 10:21:38 +02:00
committed by GitHub
parent fc2495396f
commit b07b537067
3 changed files with 12 additions and 11 deletions
+4 -4
View File
@@ -74,14 +74,14 @@ fn init(criterion: &mut Criterion) {
group.bench_function("empty", |bencher| { group.bench_function("empty", |bencher| {
bencher.iter(|| { bencher.iter(|| {
let items: Vec<TreeItem<usize>> = vec![]; let items: Vec<TreeItem<usize>> = vec![];
black_box(Tree::new(black_box(items))).unwrap(); black_box(Tree::new(black_box(&items))).unwrap();
}); });
}); });
group.bench_function("example-items", |bencher| { group.bench_function("example-items", |bencher| {
bencher.iter(|| { bencher.iter(|| {
let items = example_items(); let items = example_items();
black_box(Tree::new(black_box(items))).unwrap(); black_box(Tree::new(black_box(&items))).unwrap();
}); });
}); });
@@ -96,7 +96,7 @@ fn renders(criterion: &mut Criterion) {
group.bench_function("empty", |bencher| { group.bench_function("empty", |bencher| {
let items: Vec<TreeItem<usize>> = vec![]; let items: Vec<TreeItem<usize>> = vec![];
let tree = Tree::new(items).unwrap(); let tree = Tree::new(&items).unwrap();
let mut state = TreeState::default(); let mut state = TreeState::default();
bencher.iter_batched( bencher.iter_batched(
|| (tree.clone(), Buffer::empty(buffer_size)), || (tree.clone(), Buffer::empty(buffer_size)),
@@ -109,7 +109,7 @@ fn renders(criterion: &mut Criterion) {
group.bench_function("example-items", |bencher| { group.bench_function("example-items", |bencher| {
let items = example_items(); let items = example_items();
let tree = Tree::new(items).unwrap(); let tree = Tree::new(&items).unwrap();
let mut state = TreeState::default(); let mut state = TreeState::default();
state.open(vec!["b"]); state.open(vec!["b"]);
state.open(vec!["b", "d"]); state.open(vec!["b", "d"]);
+1 -1
View File
@@ -81,7 +81,7 @@ impl App {
fn draw(&mut self, frame: &mut Frame) { fn draw(&mut self, frame: &mut Frame) {
let area = frame.size(); let area = frame.size();
let widget = Tree::new(self.items.clone()) let widget = Tree::new(&self.items)
.expect("all item identifiers are unique") .expect("all item identifiers are unique")
.block( .block(
Block::bordered() Block::bordered()
+7 -6
View File
@@ -42,7 +42,7 @@ mod tree_state;
/// terminal.draw(|frame| { /// terminal.draw(|frame| {
/// let area = frame.size(); /// let area = frame.size();
/// ///
/// let tree_widget = Tree::new(items) /// let tree_widget = Tree::new(&items)
/// .expect("all item identifiers are unique") /// .expect("all item identifiers are unique")
/// .block(Block::bordered().title("Tree Widget")); /// .block(Block::bordered().title("Tree Widget"));
/// ///
@@ -52,7 +52,7 @@ mod tree_state;
/// ``` /// ```
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Tree<'a, Identifier> { pub struct Tree<'a, Identifier> {
items: Vec<TreeItem<'a, Identifier>>, items: &'a [TreeItem<'a, Identifier>],
block: Option<Block<'a>>, block: Option<Block<'a>>,
scrollbar: Option<Scrollbar<'a>>, scrollbar: Option<Scrollbar<'a>>,
@@ -81,7 +81,7 @@ where
/// # Errors /// # Errors
/// ///
/// Errors when there are duplicate identifiers in the children. /// Errors when there are duplicate identifiers in the children.
pub fn new(items: Vec<TreeItem<'a, Identifier>>) -> std::io::Result<Self> { pub fn new(items: &'a [TreeItem<'a, Identifier>]) -> std::io::Result<Self> {
let identifiers = items let identifiers = items
.iter() .iter()
.map(|item| &item.identifier) .map(|item| &item.identifier)
@@ -166,7 +166,7 @@ where
fn tree_new_errors_with_duplicate_identifiers() { fn tree_new_errors_with_duplicate_identifiers() {
let item = TreeItem::new_leaf("same", "text"); let item = TreeItem::new_leaf("same", "text");
let another = item.clone(); let another = item.clone();
Tree::new(vec![item, another]).unwrap(); Tree::new(&[item, another]).unwrap();
} }
impl<Identifier> StatefulWidget for Tree<'_, Identifier> impl<Identifier> StatefulWidget for Tree<'_, Identifier>
@@ -190,7 +190,7 @@ where
return; return;
} }
let visible = state.flatten(&self.items); let visible = state.flatten(self.items);
state.last_biggest_index = visible.len().saturating_sub(1); state.last_biggest_index = visible.len().saturating_sub(1);
if visible.is_empty() { if visible.is_empty() {
return; return;
@@ -349,7 +349,8 @@ mod render_tests {
#[must_use] #[must_use]
#[track_caller] #[track_caller]
fn render(width: u16, height: u16, state: &mut TreeState<&'static str>) -> Buffer { fn render(width: u16, height: u16, state: &mut TreeState<&'static str>) -> Buffer {
let tree = Tree::new(TreeItem::example()).unwrap(); let items = TreeItem::example();
let tree = Tree::new(&items).unwrap();
let area = Rect::new(0, 0, width, height); let area = Rect::new(0, 0, width, height);
let mut buffer = Buffer::empty(area); let mut buffer = Buffer::empty(area);
StatefulWidget::render(tree, area, &mut buffer, state); StatefulWidget::render(tree, area, &mut buffer, state);