slint/internal/compiler/passes/border_radius.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

40 lines
1.6 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 applies the default border-radius to border-top|bottom-left|right-radius.
use crate::diagnostics::BuildDiagnostics;
use crate::expression_tree::{Expression, NamedReference};
use crate::object_tree::Component;
use smol_str::SmolStr;
use std::rc::Rc;
pub const BORDER_RADIUS_PROPERTIES: [&str; 4] = [
"border-top-left-radius",
"border-top-right-radius",
"border-bottom-right-radius",
"border-bottom-left-radius",
];
pub fn handle_border_radius(root_component: &Rc<Component>, _diag: &mut BuildDiagnostics) {
crate::object_tree::recurse_elem_including_sub_components_no_borrow(
root_component,
&(),
&mut |elem, _| {
let bty = if let Some(bty) = elem.borrow().builtin_type() { bty } else { return };
if bty.name == "Rectangle"
&& elem.borrow().is_binding_set("border-radius", true)
&& BORDER_RADIUS_PROPERTIES
.iter()
.any(|property_name| elem.borrow().is_binding_set(property_name, true))
{
let border_radius = NamedReference::new(elem, SmolStr::new_static("border-radius"));
for property_name in BORDER_RADIUS_PROPERTIES.iter() {
elem.borrow_mut().set_binding_if_not_set(SmolStr::new(property_name), || {
Expression::PropertyReference(border_radius.clone())
});
}
}
},
)
}