forked from 1b-agent/opencadstudio-upstream
perf(core): cache ribbon_groups() with OnceLock, avoid per-frame tree allocations
Change CadModule::ribbon_groups() from -> Vec<RibbonGroup> to -> &[RibbonGroup], backed by a per-module OnceLock cache. The ribbon group tree is static data (&'static str, Copy enums) that was being fully reconstructed — Vecs, String clones, enum discriminants — on every call, including all three per-frame call sites (view(), dropdown_overlay(), style_combo_overlay()). - 7 built-in modules + 2 plugin templates cache via function-local OnceLock. Safe because each is a unit struct with exactly one instance per process (plugins run in isolated child processes; see PluginProcess::spawn()). - SharedCadModule avoids the static pattern entirely, storing groups in an instance field (owned.rs). - IPC runner converts &RibbonGroup -> OwnedRibbonGroup directly via new From<&T> impls, avoiding an intermediate clone. - render_small/render_large and the two make_tool_row closures now borrow (&RibbonItem, &[ToolDef]) instead of taking ownership, so the view loop doesn't need to clone items to satisfy the old by-value signatures. - Various match-ergonomics deref fixes (*id, *default, *icon, etc.) from the &RibbonItem pattern change.
This commit is contained in:
1 parent
8164e14a19
commit
73b05e1dec
14 files changed
+947
-690
No files matched your search
+189
-186
@@ -26,7 +26,7 @@ impl CadModule for DrawModule {
|
||||
"Draw"
|
||||
}
|
||||
|
||||
fn ribbon_groups(&self) -> Vec<RibbonGroup> {
|
||||
fn ribbon_groups(&self) -> &[RibbonGroup] {
|
||||
use crate::modules::annotate::{
|
||||
angular_dim, leader_cmd, linear_dim, mleader_cmd, mtext, radius_dim, text,
|
||||
};
|
||||
@@ -43,190 +43,193 @@ impl CadModule for DrawModule {
|
||||
};
|
||||
use properties::match_prop;
|
||||
|
||||
vec![
|
||||
RibbonGroup {
|
||||
title: "Draw",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(line::tool()),
|
||||
RibbonItem::LargeTool(polyline::tool()),
|
||||
RibbonItem::LargeDropdown {
|
||||
id: circle::DROPDOWN_ID,
|
||||
label: "Circle",
|
||||
icon: circle::ICON,
|
||||
items: circle::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "CIRCLE",
|
||||
},
|
||||
RibbonItem::LargeDropdown {
|
||||
id: arc::DROPDOWN_ID,
|
||||
label: "Arc",
|
||||
icon: arc::ICON,
|
||||
items: arc::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "ARC",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: shapes::DROPDOWN_ID,
|
||||
icon: shapes::ICON,
|
||||
items: shapes::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "RECT",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: ellipse::DROPDOWN_ID,
|
||||
icon: ellipse::ICON,
|
||||
items: ellipse::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "ELLIPSE",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: hatch::DROPDOWN_ID,
|
||||
icon: hatch::ICON,
|
||||
items: hatch::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "HATCH",
|
||||
},
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Modify",
|
||||
tools: vec![
|
||||
translate::tool().into(),
|
||||
copy::tool().into(),
|
||||
stretch::tool().into(),
|
||||
rotate::tool().into(),
|
||||
mirror::tool().into(),
|
||||
scale::tool().into(),
|
||||
RibbonItem::Dropdown {
|
||||
id: trim::DROPDOWN_ID,
|
||||
icon: trim::ICON,
|
||||
items: trim::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "TRIM",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: fillet::DROPDOWN_ID,
|
||||
icon: fillet::ICON,
|
||||
items: fillet::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "FILLET",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: array::DROPDOWN_ID,
|
||||
icon: array::ICON,
|
||||
items: array::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "ARRAYRECT",
|
||||
},
|
||||
delete::tool().into(),
|
||||
explode::tool().into(),
|
||||
offset::tool().into(),
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Annotation",
|
||||
tools: vec![
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "ANNOTATION_TEXT",
|
||||
label: "Text",
|
||||
icon: text::ICON,
|
||||
items: vec![
|
||||
(text::tool().id, text::tool().label, text::tool().icon),
|
||||
(mtext::tool().id, mtext::tool().label, mtext::tool().icon),
|
||||
],
|
||||
default: "TEXT",
|
||||
},
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "ANNOTATION_DIMENSIONS",
|
||||
label: "Dimensions",
|
||||
icon: linear_dim::ICON,
|
||||
items: vec![
|
||||
(
|
||||
linear_dim::tool().id,
|
||||
linear_dim::tool().label,
|
||||
linear_dim::tool().icon,
|
||||
),
|
||||
(
|
||||
radius_dim::tool().id,
|
||||
radius_dim::tool().label,
|
||||
radius_dim::tool().icon,
|
||||
),
|
||||
(
|
||||
angular_dim::tool().id,
|
||||
angular_dim::tool().label,
|
||||
angular_dim::tool().icon,
|
||||
),
|
||||
],
|
||||
default: "DIMLINEAR",
|
||||
},
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "ANNOTATION_LEADER",
|
||||
label: "Leader",
|
||||
icon: leader_cmd::ICON,
|
||||
items: vec![
|
||||
(
|
||||
mleader_cmd::tool().id,
|
||||
mleader_cmd::tool().label,
|
||||
mleader_cmd::tool().icon,
|
||||
),
|
||||
(
|
||||
leader_cmd::tool().id,
|
||||
leader_cmd::tool().label,
|
||||
leader_cmd::tool().icon,
|
||||
),
|
||||
],
|
||||
default: "MLEADER",
|
||||
},
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Layers",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(panel::tool()),
|
||||
RibbonItem::LayerComboGroup {
|
||||
row2: vec![
|
||||
layoff::tool(),
|
||||
layfrz::tool(),
|
||||
laylck::tool(),
|
||||
make_current::tool(),
|
||||
],
|
||||
row3: vec![
|
||||
layon::tool(),
|
||||
laythw::tool(),
|
||||
layulk::tool(),
|
||||
match_layer::tool(),
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Block",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(create_block::tool()),
|
||||
RibbonItem::LargeTool(insert_block::tool()),
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Properties",
|
||||
tools: vec![RibbonItem::PropertiesGroup {
|
||||
match_prop: match_prop::tool(),
|
||||
}],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Groups",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(group::tool()),
|
||||
RibbonItem::LargeTool(ungroup::tool()),
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Clipboard",
|
||||
tools: vec![
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "PASTE_MENU",
|
||||
label: "Paste",
|
||||
icon: paste::ICON,
|
||||
items: paste::MENU_ITEMS.to_vec(),
|
||||
default: "PASTECLIP",
|
||||
},
|
||||
copy_clip::tool().into(),
|
||||
cut::tool().into(),
|
||||
],
|
||||
},
|
||||
// Support group lives on the Start tab now (see view.rs:
|
||||
// start_page_view). Removed from the Draw ribbon to declutter.
|
||||
]
|
||||
static GROUPS: std::sync::OnceLock<Vec<RibbonGroup>> = std::sync::OnceLock::new();
|
||||
GROUPS.get_or_init(|| {
|
||||
vec![
|
||||
RibbonGroup {
|
||||
title: "Draw",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(line::tool()),
|
||||
RibbonItem::LargeTool(polyline::tool()),
|
||||
RibbonItem::LargeDropdown {
|
||||
id: circle::DROPDOWN_ID,
|
||||
label: "Circle",
|
||||
icon: circle::ICON,
|
||||
items: circle::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "CIRCLE",
|
||||
},
|
||||
RibbonItem::LargeDropdown {
|
||||
id: arc::DROPDOWN_ID,
|
||||
label: "Arc",
|
||||
icon: arc::ICON,
|
||||
items: arc::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "ARC",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: shapes::DROPDOWN_ID,
|
||||
icon: shapes::ICON,
|
||||
items: shapes::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "RECT",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: ellipse::DROPDOWN_ID,
|
||||
icon: ellipse::ICON,
|
||||
items: ellipse::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "ELLIPSE",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: hatch::DROPDOWN_ID,
|
||||
icon: hatch::ICON,
|
||||
items: hatch::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "HATCH",
|
||||
},
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Modify",
|
||||
tools: vec![
|
||||
translate::tool().into(),
|
||||
copy::tool().into(),
|
||||
stretch::tool().into(),
|
||||
rotate::tool().into(),
|
||||
mirror::tool().into(),
|
||||
scale::tool().into(),
|
||||
RibbonItem::Dropdown {
|
||||
id: trim::DROPDOWN_ID,
|
||||
icon: trim::ICON,
|
||||
items: trim::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "TRIM",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: fillet::DROPDOWN_ID,
|
||||
icon: fillet::ICON,
|
||||
items: fillet::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "FILLET",
|
||||
},
|
||||
RibbonItem::Dropdown {
|
||||
id: array::DROPDOWN_ID,
|
||||
icon: array::ICON,
|
||||
items: array::DROPDOWN_ITEMS.to_vec(),
|
||||
default: "ARRAYRECT",
|
||||
},
|
||||
delete::tool().into(),
|
||||
explode::tool().into(),
|
||||
offset::tool().into(),
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Annotation",
|
||||
tools: vec![
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "ANNOTATION_TEXT",
|
||||
label: "Text",
|
||||
icon: text::ICON,
|
||||
items: vec![
|
||||
(text::tool().id, text::tool().label, text::tool().icon),
|
||||
(mtext::tool().id, mtext::tool().label, mtext::tool().icon),
|
||||
],
|
||||
default: "TEXT",
|
||||
},
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "ANNOTATION_DIMENSIONS",
|
||||
label: "Dimensions",
|
||||
icon: linear_dim::ICON,
|
||||
items: vec![
|
||||
(
|
||||
linear_dim::tool().id,
|
||||
linear_dim::tool().label,
|
||||
linear_dim::tool().icon,
|
||||
),
|
||||
(
|
||||
radius_dim::tool().id,
|
||||
radius_dim::tool().label,
|
||||
radius_dim::tool().icon,
|
||||
),
|
||||
(
|
||||
angular_dim::tool().id,
|
||||
angular_dim::tool().label,
|
||||
angular_dim::tool().icon,
|
||||
),
|
||||
],
|
||||
default: "DIMLINEAR",
|
||||
},
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "ANNOTATION_LEADER",
|
||||
label: "Leader",
|
||||
icon: leader_cmd::ICON,
|
||||
items: vec![
|
||||
(
|
||||
mleader_cmd::tool().id,
|
||||
mleader_cmd::tool().label,
|
||||
mleader_cmd::tool().icon,
|
||||
),
|
||||
(
|
||||
leader_cmd::tool().id,
|
||||
leader_cmd::tool().label,
|
||||
leader_cmd::tool().icon,
|
||||
),
|
||||
],
|
||||
default: "MLEADER",
|
||||
},
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Layers",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(panel::tool()),
|
||||
RibbonItem::LayerComboGroup {
|
||||
row2: vec![
|
||||
layoff::tool(),
|
||||
layfrz::tool(),
|
||||
laylck::tool(),
|
||||
make_current::tool(),
|
||||
],
|
||||
row3: vec![
|
||||
layon::tool(),
|
||||
laythw::tool(),
|
||||
layulk::tool(),
|
||||
match_layer::tool(),
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Block",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(create_block::tool()),
|
||||
RibbonItem::LargeTool(insert_block::tool()),
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Properties",
|
||||
tools: vec![RibbonItem::PropertiesGroup {
|
||||
match_prop: match_prop::tool(),
|
||||
}],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Groups",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(group::tool()),
|
||||
RibbonItem::LargeTool(ungroup::tool()),
|
||||
],
|
||||
},
|
||||
RibbonGroup {
|
||||
title: "Clipboard",
|
||||
tools: vec![
|
||||
RibbonItem::LargeDropdown {
|
||||
id: "PASTE_MENU",
|
||||
label: "Paste",
|
||||
icon: paste::ICON,
|
||||
items: paste::MENU_ITEMS.to_vec(),
|
||||
default: "PASTECLIP",
|
||||
},
|
||||
copy_clip::tool().into(),
|
||||
cut::tool().into(),
|
||||
],
|
||||
},
|
||||
// Support group lives on the Start tab now (see view.rs:
|
||||
// start_page_view). Removed from the Draw ribbon to declutter.
|
||||
]
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user