Use SmolStr for NamedReferenceInner

Heaptrack showed a lot of memory allocations when the strings
in the NamedReference got copied around. Instead, we now use
SmolStr to benefit from SSO. This already removes ~320000
allocations for the benchmark app I am using here. Runtime
also improves by ca 1.8%.

Before:
```
Benchmark 1: ./target/release/slint-viewer ../slint-perf/app.slint
  Time (mean ± σ):     676.6 ms ±  15.6 ms    [User: 594.6 ms, System: 77.4 ms]
  Range (min … max):   662.9 ms … 703.4 ms    10 runs

        allocations:            5202354
        temporary allocations:  857511
```

After:
```
Benchmark 1: ./target/release/slint-viewer ../slint-perf/app.slint
  Time (mean ± σ):     664.2 ms ±   6.7 ms    [User: 589.2 ms, System: 74.0 ms]
  Range (min … max):   659.0 ms … 682.4 ms    10 runs

        allocations:            4886888
        temporary allocations:  857508
```
This commit is contained in:
Milian Wolff 2024-09-25 11:48:55 +02:00 committed by Olivier Goffart
parent a4eb794e43
commit 08a3a6cc4a

View file

@ -5,6 +5,7 @@
This module contains the [`NamedReference`] and its helper
*/
use smol_str::SmolStr;
use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
@ -154,7 +155,7 @@ struct NamedReferenceInner {
/// The element.
element: Weak<RefCell<Element>>,
/// The property name
name: String,
name: SmolStr,
}
impl NamedReferenceInner {
@ -173,8 +174,8 @@ impl NamedReferenceInner {
let result = if let Some(r) = named_references.get(name) {
r.clone()
} else {
let r = Rc::new(Self { element: Rc::downgrade(element), name: name.to_owned() });
named_references.insert(name.to_owned(), r.clone());
let r = Rc::new(Self { element: Rc::downgrade(element), name: name.into() });
named_references.insert(name.into(), r.clone());
r
};
drop(named_references);
@ -195,7 +196,7 @@ impl NamedReferenceInner {
/// Must be put inside the Element and owns all the NamedReferenceInner
#[derive(Default)]
pub struct NamedReferenceContainer(RefCell<HashMap<String, Rc<NamedReferenceInner>>>);
pub struct NamedReferenceContainer(RefCell<HashMap<SmolStr, Rc<NamedReferenceInner>>>);
impl NamedReferenceContainer {
/// Returns true if there is at least one NamedReference pointing to the property `name` in this element.