mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 09:46:19 +01:00
Selection: the selected roots are found once for each selection
selected_roots put the selected objects in paint order and dropped the inner ones, two walks of the document, every time a command or an enabled check asked. A rebuild of the menus asks many times for one selection. The roots are now kept on the thread with the document version (held weakly, so its address is not reused) and the selected objects, and found again only when either changes. With 10,000 rectangles selected, a menu rebuild after Select All went from about 500 ms to about 340 ms on the build host.
This commit is contained in:
1 parent
2c84465fba
commit
0ab10f3935
2 files changed
+59
-2
No files matched your search
@@ -954,9 +954,43 @@ pub(crate) fn selected_roots(s: &Session) -> Result<Vec<NodeId>> {
|
||||
Ok(selection_roots(s.doc()?))
|
||||
}
|
||||
|
||||
/// [`selected_roots`] of one document's selection.
|
||||
/// [`selected_roots`] of one document's selection, found once for each document version and set of selected
|
||||
/// objects and kept on this thread: the menus' enabled checks read it many times for one selection.
|
||||
pub(crate) fn selection_roots(st: &crate::DocState) -> Vec<NodeId> {
|
||||
roots_of(&st.doc, st.selection.in_paint_order(&st.doc))
|
||||
let at = Arc::as_ptr(&st.doc);
|
||||
ROOTS.with_borrow_mut(|kept| {
|
||||
if let Some((doc, objects, roots)) = kept.as_ref()
|
||||
&& doc.as_ptr() == at
|
||||
&& *objects == st.selection.objects
|
||||
{
|
||||
return roots.clone();
|
||||
}
|
||||
#[cfg(test)]
|
||||
ROOTS_FOUND.set(ROOTS_FOUND.get() + 1);
|
||||
let roots = roots_of(&st.doc, st.selection.in_paint_order(&st.doc));
|
||||
*kept = Some((Arc::downgrade(&st.doc), st.selection.objects.clone(), roots.clone()));
|
||||
roots
|
||||
})
|
||||
}
|
||||
|
||||
/// The selected objects and roots of the last [`selection_roots`], with the document version they belong to. It is
|
||||
/// held weakly, so its address is not reused while it is kept, and an edit in place moves it.
|
||||
type KeptRoots = Option<(std::sync::Weak<Document>, Vec<NodeId>, Vec<NodeId>)>;
|
||||
|
||||
thread_local! {
|
||||
static ROOTS: std::cell::RefCell<KeptRoots> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many times [`selection_roots`] found the roots on this thread.
|
||||
static ROOTS_FOUND: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// How many times [`selection_roots`] has found the roots on this thread.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn roots_found() -> u64 {
|
||||
ROOTS_FOUND.get()
|
||||
}
|
||||
|
||||
/// [`super::brushsym::part_only`] while an object of a dynamic symbol instance's art is selected:
|
||||
|
||||
@@ -347,3 +347,26 @@ fn direct_selection_cut_timing() {
|
||||
println!("median {}: cpu {:?}, wall {:?}", ["one at a time", "Cut"][k], median(cpu), median(wall));
|
||||
}
|
||||
}
|
||||
|
||||
/// The selected roots are found once for a document version and its selected objects, however many enabled checks
|
||||
/// read them, and found again after the selection changes, after an edit, and for another selection.
|
||||
#[test]
|
||||
fn selected_roots_are_found_once_for_each_document_and_selection() {
|
||||
let mut s = session(30, square);
|
||||
let all = selected_roots(&s).unwrap();
|
||||
assert_eq!(all.len(), 30);
|
||||
let before = roots_found();
|
||||
for _ in 0..20 {
|
||||
assert_eq!(selected_roots(&s).unwrap(), all);
|
||||
}
|
||||
assert_eq!(roots_found() - before, 0, "reading the same selection again finds nothing");
|
||||
s.execute("select.set", &json!({"ids": [all[3].0, all[7].0]})).unwrap();
|
||||
assert_eq!(selected_roots(&s).unwrap(), vec![all[3], all[7]]);
|
||||
assert_eq!(roots_found() - before, 1, "a new selection is found once");
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
let grouped = selected_roots(&s).unwrap();
|
||||
assert_eq!(grouped.len(), 1, "the group is the one root: {grouped:?}");
|
||||
assert_ne!(grouped, vec![all[3], all[7]]);
|
||||
selected_roots(&s).unwrap();
|
||||
assert_eq!(roots_found() - before, 2, "an edit finds the roots once again");
|
||||
}
|
||||
Reference in new issue
Block a user