Skip to content

Commit

Permalink
micro-optim ItemRc::component() returns a ref instead of cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
Guiguiprim authored and ogoffart committed Jun 29, 2023
1 parent d421638 commit d27e0a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/backends/winit/accesskit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl AccessKitAdapter {

fn encode_item_node_id(&self, item: &ItemRc) -> Option<NodeId> {
let component = item.component();
let component_ptr = ComponentRef::as_ptr(ComponentRc::borrow(&component));
let component_ptr = ComponentRef::as_ptr(ComponentRc::borrow(component));
let component_id = *(self.component_ids.borrow().get(&component_ptr)?);
let index = item.index();
Some(NodeId(
Expand Down
6 changes: 3 additions & 3 deletions internal/core/item_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<T: Clone> ItemCache<T> {
/// Otherwise call the `update_fn` to compute that value, and track property access
/// so it is automatically invalided when property becomes dirty.
pub fn get_or_update_cache_entry(&self, item_rc: &ItemRc, update_fn: impl FnOnce() -> T) -> T {
let component = &(*item_rc.component()) as *const _;
let component = &(**item_rc.component()) as *const _;
let mut borrowed = self.map.borrow_mut();
match borrowed.entry(component).or_default().entry(item_rc.index()) {
std::collections::hash_map::Entry::Occupied(mut entry) => {
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<T: Clone> ItemCache<T> {
item_rc: &ItemRc,
callback: impl FnOnce(&T) -> Option<U>,
) -> Option<U> {
let component = &(*item_rc.component()) as *const _;
let component = &(**item_rc.component()) as *const _;
self.map
.borrow()
.get(&component)
Expand All @@ -148,7 +148,7 @@ impl<T: Clone> ItemCache<T> {

/// free the cache for a given item
pub fn release(&self, item_rc: &ItemRc) {
let component = &(*item_rc.component()) as *const _;
let component = &(**item_rc.component()) as *const _;
if let Some(sub) = self.map.borrow_mut().get_mut(&component) {
sub.remove(&item_rc.index());
}
Expand Down
22 changes: 11 additions & 11 deletions internal/core/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::pin::Pin;
use vtable::*;

fn find_sibling_outside_repeater(
component: crate::component::ComponentRc,
component: &crate::component::ComponentRc,
comp_ref_pin: Pin<VRef<ComponentVTable>>,
index: usize,
sibling_step: &dyn Fn(&crate::item_tree::ComponentItemTree, usize) -> Option<usize>,
Expand All @@ -28,7 +28,7 @@ fn find_sibling_outside_repeater(
current_sibling = sibling_step(&item_tree, current_sibling)?;

if let Some(node) = step_into_node(
&component,
component,
&comp_ref_pin,
current_sibling,
&item_tree,
Expand Down Expand Up @@ -200,8 +200,8 @@ impl ItemRc {
self.index
}
/// Returns a reference to the component holding this item
pub fn component(&self) -> vtable::VRc<ComponentVTable> {
self.component.clone()
pub fn component(&self) -> &vtable::VRc<ComponentVTable> {
&self.component
}

fn find_child(
Expand Down Expand Up @@ -288,7 +288,7 @@ impl ItemRc {

// We need to leave the repeater:
find_sibling_outside_repeater(
parent.clone(),
parent,
parent_ref_pin,
parent_item_index,
sibling_step,
Expand Down Expand Up @@ -334,7 +334,7 @@ impl ItemRc {
step_in: &dyn Fn(ItemRc) -> ItemRc,
step_out: &dyn Fn(&crate::item_tree::ComponentItemTree, usize) -> Option<usize>,
) -> Self {
let mut component = self.component();
let mut component = self.component().clone();
let mut comp_ref_pin = vtable::VRc::borrow_pin(&self.component);
let mut item_tree = crate::item_tree::ComponentItemTree::new(&comp_ref_pin);

Expand Down Expand Up @@ -364,21 +364,21 @@ impl ItemRc {

// Step out of the repeater
let root_component = root.component();
let root_comp_ref = vtable::VRc::borrow_pin(&root_component);
let root_comp_ref = vtable::VRc::borrow_pin(root_component);
let mut parent_node = Default::default();
root_comp_ref.as_ref().parent_node(&mut parent_node);

while let Some(parent) = parent_node.upgrade() {
// .. not at the root of the item tree:
component = parent.component();
component = parent.component().clone();
comp_ref_pin = vtable::VRc::borrow_pin(&component);
item_tree = crate::item_tree::ComponentItemTree::new(&comp_ref_pin);

let index = parent.index();

if let Some(next) = step_out(&item_tree, index) {
if let Some(item) = step_into_node(
&parent.component(),
parent.component(),
&comp_ref_pin,
next,
&item_tree,
Expand All @@ -394,14 +394,14 @@ impl ItemRc {
}
}

root = ItemRc::new(component, 0);
root = ItemRc::new(component.clone(), 0);
if let Some(item) = subtree_step(root.clone()) {
return step_in(item);
}

// Go up one more level:
let root_component = root.component();
let root_comp_ref = vtable::VRc::borrow_pin(&root_component);
let root_comp_ref = vtable::VRc::borrow_pin(root_component);
parent_node = Default::default();
root_comp_ref.as_ref().parent_node(&mut parent_node);
}
Expand Down

0 comments on commit d27e0a4

Please sign in to comment.