slint/internal/compiler/passes/lower_accessibility.rs
Milian Wolff f6da161dbb
Reduce SmolStr <-> &str conversions and copies (#6688)
SmolStr has an Arc internally for large strings. This allows
cheap copies of large strings, but we lose that ability
when we convert the SmolStr to a &str and then reconstruct a
SmolStr from that slice.

I was hoping for some larger gains here, considering the impact
of this code change, but it only removes ~50k allocations,
while the impact on the runtime is not noticeable at all.

Still, I believe this is the right thing to do.

Before:
```
        allocations:            2338981

  Time (mean ± σ):     988.3 ms ±  17.9 ms    [User: 690.2 ms, System: 206.4 ms]
  Range (min … max):   956.4 ms … 1016.3 ms    10 runs
```

After:
```
        allocations:            2287723

  Time (mean ± σ):     989.8 ms ±  23.2 ms    [User: 699.2 ms, System: 197.6 ms]
  Range (min … max):   945.3 ms … 1021.4 ms    10 runs
```
2024-11-19 10:07:16 +01:00

78 lines
3.3 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//! Pass that lowers synthetic `accessible-*` properties
use crate::diagnostics::BuildDiagnostics;
use crate::expression_tree::{Expression, NamedReference};
use crate::langtype::EnumerationValue;
use crate::object_tree::{Component, ElementRc};
use smol_str::SmolStr;
use std::rc::Rc;
pub fn lower_accessibility_properties(component: &Rc<Component>, diag: &mut BuildDiagnostics) {
crate::object_tree::recurse_elem_including_sub_components_no_borrow(
component,
&(),
&mut |elem, _| {
if elem.borrow().repeated.is_some() {
return;
};
apply_builtin(elem);
let accessible_role_set = match elem.borrow().bindings.get("accessible-role") {
Some(role) => {
if let Expression::EnumerationValue(val) = &role.borrow().expression {
debug_assert_eq!(val.enumeration.name, "AccessibleRole");
debug_assert_eq!(val.enumeration.values[0], "none");
if val.value == 0 {
return;
}
} else {
diag.push_error(
"The `accessible-role` property must be a constant expression".into(),
&*role.borrow(),
);
}
true
}
// maybe it was set on the parent
None => elem.borrow().is_binding_set("accessible-role", false),
};
for prop_name in crate::typeregister::reserved_accessibility_properties()
.map(|x| x.0)
.chain(std::iter::once("accessible-role"))
{
if accessible_role_set {
if elem.borrow().is_binding_set(prop_name, false) {
let nr = NamedReference::new(elem, SmolStr::new_static(prop_name));
elem.borrow_mut().accessibility_props.0.insert(prop_name.into(), nr);
}
} else if let Some(b) = elem.borrow().bindings.get(prop_name) {
diag.push_error(
format!("The `{prop_name}` property can only be set in combination to `accessible-role`"),
&*b.borrow(),
);
}
}
},
)
}
fn apply_builtin(e: &ElementRc) {
let bty = if let Some(bty) = e.borrow().builtin_type() { bty } else { return };
if bty.name == "Text" {
e.borrow_mut().set_binding_if_not_set("accessible-role".into(), || {
let enum_ty = crate::typeregister::BUILTIN.with(|e| e.enums.AccessibleRole.clone());
Expression::EnumerationValue(EnumerationValue {
value: enum_ty.values.iter().position(|v| v == "text").unwrap(),
enumeration: enum_ty,
})
});
let text_prop = NamedReference::new(e, SmolStr::new_static("text"));
e.borrow_mut().set_binding_if_not_set("accessible-label".into(), || {
Expression::PropertyReference(text_prop)
});
}
}