perf(widget): avoid VecDeque clone in segmented_button/table Model::clear
Model::clear() cloned the entire order VecDeque to iterate while remove() mutated it, producing an O(n) allocation proportional to the number of items — needless on a clear() which is going to drop all of them anyway. Replace the clone with std::mem::take(&mut self.order): we iterate the taken VecDeque (transferring ownership), and the inner self.order.remove(index) in each remove() call now finds position()==None and no-ops, since self.order has been swapped with an empty default. Same semantics, zero allocation. Noticeable on large nav/table models (>100 items) and on apps that reset state frequently (settings pages, file lists, context menus).
This commit is contained in:
parent
77262dd0af
commit
1d98eee6de
2 changed files with 8 additions and 2 deletions
|
|
@ -132,7 +132,10 @@ where
|
|||
/// ```
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
for entity in self.order.clone() {
|
||||
// `remove()` mutates `self.order`, so transfer ownership first:
|
||||
// the inner `self.order.remove(index)` then no-ops because
|
||||
// `position()` can't find the entity in the empty VecDeque.
|
||||
for entity in std::mem::take(&mut self.order) {
|
||||
self.remove(entity);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,10 @@ where
|
|||
/// model.clear();
|
||||
/// ```
|
||||
pub fn clear(&mut self) {
|
||||
for entity in self.order.clone() {
|
||||
// `remove()` mutates `self.order`, so transfer ownership first:
|
||||
// the inner `self.order.remove(index)` then no-ops because
|
||||
// `position()` can't find the entity in the empty VecDeque.
|
||||
for entity in std::mem::take(&mut self.order) {
|
||||
self.remove(entity);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue