Also wrap langtype::Type::Struct in an Rc

This makes copying such types much cheaper and will allow us to
intern common struct types in the future too. This further
drops the sample cost for langtype.rs from ~6.6% down to 4.0%.

We are now also able to share/intern common struct types.

Before:
```
  Time (mean ± σ):      1.073 s ±  0.021 s    [User: 0.759 s, System: 0.215 s]
  Range (min … max):    1.034 s …  1.105 s    10 runs

        allocations:            3074261
```

After:
```
  Time (mean ± σ):      1.034 s ±  0.026 s    [User: 0.733 s, System: 0.201 s]
  Range (min … max):    1.000 s …  1.078 s    10 runs

        allocations:            2917476
```
This commit is contained in:
Milian Wolff 2024-10-23 21:39:36 +02:00 committed by Olivier Goffart
parent efdecf0a13
commit 69c68b22b2
29 changed files with 302 additions and 259 deletions

View file

@ -52,16 +52,7 @@ pub enum Type {
Brush,
/// This is usually a model
Array(Box<Type>),
Struct {
fields: BTreeMap<SmolStr, Type>,
/// When declared in .slint as `struct Foo := { }`, then the name is "Foo"
/// When there is no node, but there is a name, then it is a builtin type
name: Option<SmolStr>,
/// When declared in .slint, this is the node of the declaration.
node: Option<syntax_nodes::ObjectType>,
/// derived
rust_attributes: Option<Vec<SmolStr>>,
},
Struct(Rc<Struct>),
Enumeration(Rc<Enumeration>),
/// A type made up of the product of several "unit" types.
@ -106,8 +97,8 @@ impl core::cmp::PartialEq for Type {
Type::Easing => matches!(other, Type::Easing),
Type::Brush => matches!(other, Type::Brush),
Type::Array(a) => matches!(other, Type::Array(b) if a == b),
Type::Struct { fields, name, node: _, rust_attributes: _ } => {
matches!(other, Type::Struct{fields:f,name:n,node:_, rust_attributes: _ } if fields == f && name == n)
Type::Struct(lhs) => {
matches!(other, Type::Struct(rhs) if lhs.fields == rhs.fields && lhs.name == rhs.name)
}
Type::Enumeration(lhs) => matches!(other, Type::Enumeration(rhs) if lhs == rhs),
Type::UnitProduct(a) => matches!(other, Type::UnitProduct(b) if a == b),
@ -166,15 +157,17 @@ impl Display for Type {
Type::Bool => write!(f, "bool"),
Type::Model => write!(f, "model"),
Type::Array(t) => write!(f, "[{}]", t),
Type::Struct { name: Some(name), .. } => write!(f, "{}", name),
Type::Struct { fields, name: None, .. } => {
write!(f, "{{ ")?;
for (k, v) in fields {
write!(f, "{}: {},", k, v)?;
Type::Struct(t) => {
if let Some(name) = &t.name {
write!(f, "{}", name)
} else {
write!(f, "{{ ")?;
for (k, v) in &t.fields {
write!(f, "{}: {},", k, v)?;
}
write!(f, "}}")
}
write!(f, "}}")
}
Type::PathData => write!(f, "pathdata"),
Type::Easing => write!(f, "easing"),
Type::Brush => write!(f, "brush"),
@ -281,9 +274,7 @@ impl Type {
| (Type::Percent, Type::Float32)
| (Type::Brush, Type::Color)
| (Type::Color, Type::Brush) => true,
(Type::Struct { fields: a, .. }, Type::Struct { fields: b, .. }) => {
can_convert_struct(a, b)
}
(Type::Struct(a), Type::Struct(b)) => can_convert_struct(&a.fields, &b.fields),
(Type::UnitProduct(u), o) => match o.as_unit_product() {
Some(o) => unit_product_length_conversion(u.as_slice(), o.as_slice()).is_some(),
None => false,
@ -803,6 +794,18 @@ pub struct Function {
pub args: Vec<Type>,
}
#[derive(Debug, Clone)]
pub struct Struct {
pub fields: BTreeMap<SmolStr, Type>,
/// When declared in .slint as `struct Foo := { }`, then the name is "Foo"
/// When there is no node, but there is a name, then it is a builtin type
pub name: Option<SmolStr>,
/// When declared in .slint, this is the node of the declaration.
pub node: Option<syntax_nodes::ObjectType>,
/// derived
pub rust_attributes: Option<Vec<SmolStr>>,
}
#[derive(Debug, Clone)]
pub struct Enumeration {
pub name: SmolStr,