Skip to content

Commit 79e1592

Browse files
milianwogoffart
authored andcommitted
Intern some more common types
This is just for completeness, we "only" save ~13k allocations with no measurable speed impact: Before: ``` Time (mean ± σ): 1.019 s ± 0.033 s [User: 0.716 s, System: 0.203 s] Range (min … max): 0.957 s … 1.061 s 10 runs allocations: 2679001 ``` After: ``` Time (mean ± σ): 1.015 s ± 0.015 s [User: 0.715 s, System: 0.201 s] Range (min … max): 0.997 s … 1.038 s 10 runs allocations: 2666889 ```
1 parent df1dd3b commit 79e1592

File tree

7 files changed

+75
-52
lines changed

7 files changed

+75
-52
lines changed

internal/compiler/expression_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ declare_builtin_function_types!(
161161
ItemFontMetrics: (Type::ElementReference) -> crate::typeregister::font_metrics_type(),
162162
StringToFloat: (Type::String) -> Type::Float32,
163163
StringIsFloat: (Type::String) -> Type::Bool,
164-
ImplicitLayoutInfo(..): (Type::ElementReference) -> crate::layout::layout_info_type(),
164+
ImplicitLayoutInfo(..): (Type::ElementReference) -> crate::typeregister::layout_info_type(),
165165
ColorRgbaStruct: (Type::Color) -> Type::Struct(Rc::new(Struct {
166166
fields: IntoIterator::into_iter([
167167
(SmolStr::new_static("red"), Type::Int32),
@@ -792,7 +792,7 @@ impl Expression {
792792
// invalid because the expression is unreachable
793793
Expression::ReturnStatement(_) => Type::Invalid,
794794
Expression::LayoutCacheAccess { .. } => Type::LogicalLength,
795-
Expression::ComputeLayoutInfo(..) => crate::layout::layout_info_type(),
795+
Expression::ComputeLayoutInfo(..) => crate::typeregister::layout_info_type(),
796796
Expression::SolveLayout(..) => Type::LayoutCache,
797797
Expression::MinMax { ty, .. } => ty.clone(),
798798
Expression::EmptyComponentFactory => Type::ComponentFactory,

internal/compiler/layout.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use crate::diagnostics::BuildDiagnostics;
77
use crate::expression_tree::*;
8-
use crate::langtype::{ElementType, PropertyLookupResult, Struct, Type};
8+
use crate::langtype::{ElementType, PropertyLookupResult, Type};
99
use crate::object_tree::{Component, ElementRc};
1010

1111
use smol_str::{format_smolstr, SmolStr};
@@ -485,24 +485,6 @@ impl BoxLayout {
485485
}
486486
}
487487

488-
/// The [`Type`] for a runtime LayoutInfo structure
489-
pub fn layout_info_type() -> Type {
490-
Type::Struct(Rc::new(Struct {
491-
fields: ["min", "max", "preferred"]
492-
.iter()
493-
.map(|s| (SmolStr::new_static(s), Type::LogicalLength))
494-
.chain(
495-
["min_percent", "max_percent", "stretch"]
496-
.iter()
497-
.map(|s| (SmolStr::new_static(s), Type::Float32)),
498-
)
499-
.collect(),
500-
name: Some("slint::private_api::LayoutInfo".into()),
501-
node: None,
502-
rust_attributes: None,
503-
}))
504-
}
505-
506488
/// Get the implicit layout info of a particular element
507489
pub fn implicit_layout_info_call(elem: &ElementRc, orientation: Orientation) -> Expression {
508490
let mut elem_it = elem.clone();
@@ -538,7 +520,7 @@ pub fn implicit_layout_info_call(elem: &ElementRc, orientation: Orientation) ->
538520
// hard-code the value for rectangle because many rectangle end up optimized away and we
539521
// don't want to depend on the element.
540522
Expression::Struct {
541-
ty: layout_info_type(),
523+
ty: crate::typeregister::layout_info_type(),
542524
values: [("min", 0.), ("max", f32::MAX), ("preferred", 0.)]
543525
.iter()
544526
.map(|(s, v)| {

internal/compiler/llr/lower_expression.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ fn compute_layout_info(
500500
llr_Expression::ExtraBuiltinFunctionCall {
501501
function: "grid_layout_info".into(),
502502
arguments: vec![cells, spacing, padding],
503-
return_ty: crate::layout::layout_info_type(),
503+
return_ty: crate::typeregister::layout_info_type(),
504504
}
505505
}
506506
crate::layout::Layout::BoxLayout(layout) => {
@@ -510,13 +510,13 @@ fn compute_layout_info(
510510
llr_Expression::ExtraBuiltinFunctionCall {
511511
function: "box_layout_info".into(),
512512
arguments: vec![bld.cells, spacing, padding, bld.alignment],
513-
return_ty: crate::layout::layout_info_type(),
513+
return_ty: crate::typeregister::layout_info_type(),
514514
}
515515
} else {
516516
llr_Expression::ExtraBuiltinFunctionCall {
517517
function: "box_layout_info_ortho".into(),
518518
arguments: vec![bld.cells, padding],
519-
return_ty: crate::layout::layout_info_type(),
519+
return_ty: crate::typeregister::layout_info_type(),
520520
}
521521
};
522522
match bld.compute_cells {
@@ -684,13 +684,7 @@ fn box_layout_data(
684684
let repeater_count =
685685
layout.elems.iter().filter(|i| i.element.borrow().repeated.is_some()).count();
686686

687-
let element_ty = Type::Struct(Rc::new(Struct {
688-
fields: IntoIterator::into_iter([("constraint".into(), crate::layout::layout_info_type())])
689-
.collect(),
690-
name: Some("BoxLayoutCellData".into()),
691-
node: None,
692-
rust_attributes: None,
693-
}));
687+
let element_ty = crate::typeregister::box_layout_cell_data_type();
694688

695689
if repeater_count == 0 {
696690
let cells = llr_Expression::Array {
@@ -702,7 +696,7 @@ fn box_layout_data(
702696
get_layout_info(&li.element, ctx, &li.constraints, orientation);
703697
make_struct(
704698
"BoxLayoutCellData",
705-
[("constraint", crate::layout::layout_info_type(), layout_info)],
699+
[("constraint", crate::typeregister::layout_info_type(), layout_info)],
706700
)
707701
})
708702
.collect(),
@@ -725,13 +719,13 @@ fn box_layout_data(
725719
get_layout_info(&item.element, ctx, &item.constraints, orientation);
726720
elements.push(Either::Left(make_struct(
727721
"BoxLayoutCellData",
728-
[("constraint", crate::layout::layout_info_type(), layout_info)],
722+
[("constraint", crate::typeregister::layout_info_type(), layout_info)],
729723
)));
730724
}
731725
}
732726
let cells = llr_Expression::ReadLocalVariable {
733727
name: "cells".into(),
734-
ty: Type::Array(Rc::new(crate::layout::layout_info_type())),
728+
ty: Type::Array(Rc::new(crate::typeregister::layout_info_type())),
735729
};
736730
BoxLayoutDataResult { alignment, cells, compute_cells: Some(("cells".into(), elements)) }
737731
}
@@ -755,7 +749,7 @@ fn grid_layout_cell_data(
755749
make_struct(
756750
"GridLayoutCellData",
757751
[
758-
("constraint", crate::layout::layout_info_type(), layout_info),
752+
("constraint", crate::typeregister::layout_info_type(), layout_info),
759753
("col_or_row", Type::Int32, llr_Expression::NumberLiteral(col_or_row as _)),
760754
("span", Type::Int32, llr_Expression::NumberLiteral(span as _)),
761755
],
@@ -771,7 +765,7 @@ pub(super) fn grid_layout_cell_data_ty() -> Type {
771765
fields: IntoIterator::into_iter([
772766
(SmolStr::new_static("col_or_row"), Type::Int32),
773767
(SmolStr::new_static("span"), Type::Int32),
774-
(SmolStr::new_static("constraint"), crate::layout::layout_info_type()),
768+
(SmolStr::new_static("constraint"), crate::typeregister::layout_info_type()),
775769
])
776770
.collect(),
777771
name: Some("GridLayoutCellData".into()),
@@ -831,7 +825,7 @@ pub fn get_layout_info(
831825
name: "layout_info".into(),
832826
value: layout_info.into(),
833827
};
834-
let ty = crate::layout::layout_info_type();
828+
let ty = crate::typeregister::layout_info_type();
835829
let fields = match &ty {
836830
Type::Struct(s) => &s.fields,
837831
_ => panic!(),
@@ -869,12 +863,7 @@ fn compile_path(path: &crate::expression_tree::Path, ctx: &ExpressionContext) ->
869863
fn llr_path_elements(elements: Vec<llr_Expression>) -> llr_Expression {
870864
llr_Expression::Cast {
871865
from: llr_Expression::Array {
872-
element_ty: Type::Struct(Rc::new(Struct {
873-
fields: Default::default(),
874-
name: Some("PathElement".into()),
875-
node: None,
876-
rust_attributes: None,
877-
})),
866+
element_ty: crate::typeregister::path_element_type(),
878867
values: elements,
879868
as_model: false,
880869
}

internal/compiler/object_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,12 +2703,12 @@ pub fn inject_element_as_repeated_element(repeated_element: &ElementRc, new_root
27032703
let li_v = crate::layout::create_new_prop(
27042704
&new_root,
27052705
"layoutinfo-v",
2706-
crate::layout::layout_info_type(),
2706+
crate::typeregister::layout_info_type(),
27072707
);
27082708
let li_h = crate::layout::create_new_prop(
27092709
&new_root,
27102710
"layoutinfo-h",
2711-
crate::layout::layout_info_type(),
2711+
crate::typeregister::layout_info_type(),
27122712
);
27132713
let expr_h = crate::layout::implicit_layout_info_call(old_root, Orientation::Horizontal);
27142714
let expr_v = crate::layout::implicit_layout_info_call(old_root, Orientation::Vertical);

internal/compiler/passes/default_geometry.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,16 @@ fn gen_layout_info_prop(elem: &ElementRc, diag: &mut BuildDiagnostics) {
209209
return;
210210
}
211211

212-
let li_v =
213-
crate::layout::create_new_prop(elem, "layoutinfo-v", crate::layout::layout_info_type());
214-
let li_h =
215-
crate::layout::create_new_prop(elem, "layoutinfo-h", crate::layout::layout_info_type());
212+
let li_v = crate::layout::create_new_prop(
213+
elem,
214+
"layoutinfo-v",
215+
crate::typeregister::layout_info_type(),
216+
);
217+
let li_h = crate::layout::create_new_prop(
218+
elem,
219+
"layoutinfo-h",
220+
crate::typeregister::layout_info_type(),
221+
);
216222
elem.borrow_mut().layout_info_prop = Some((li_h.clone(), li_v.clone()));
217223
let mut expr_h = implicit_layout_info_call(elem, Orientation::Horizontal);
218224
let mut expr_v = implicit_layout_info_call(elem, Orientation::Vertical);
@@ -316,7 +322,7 @@ fn explicit_layout_info(e: &ElementRc, orientation: Orientation) -> Expression {
316322
}
317323
values.insert("min_percent".into(), Expression::NumberLiteral(0., Unit::None));
318324
values.insert("max_percent".into(), Expression::NumberLiteral(100., Unit::None));
319-
Expression::Struct { ty: crate::layout::layout_info_type(), values }
325+
Expression::Struct { ty: crate::typeregister::layout_info_type(), values }
320326
}
321327

322328
/// Replace expression such as `"width: 30%;` with `width: 0.3 * parent.width;`

internal/compiler/passes/lower_layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::langtype::Type;
1313
use crate::layout::*;
1414
use crate::object_tree::*;
1515
use crate::typeloader::TypeLoader;
16-
use crate::typeregister::TypeRegister;
16+
use crate::typeregister::{layout_info_type, TypeRegister};
1717
use smol_str::format_smolstr;
1818
use std::cell::RefCell;
1919
use std::collections::HashSet;

internal/compiler/typeregister.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,27 @@ pub struct BuiltinTypes {
8383
pub strarg_callback_type: Type,
8484
pub logical_point_type: Type,
8585
pub font_metrics_type: Type,
86+
pub layout_info_type: Type,
87+
pub path_element_type: Type,
88+
pub box_layout_cell_data_type: Type,
8689
}
8790

8891
impl BuiltinTypes {
8992
fn new() -> Self {
93+
let layout_info_type = Type::Struct(Rc::new(Struct {
94+
fields: ["min", "max", "preferred"]
95+
.iter()
96+
.map(|s| (SmolStr::new_static(s), Type::LogicalLength))
97+
.chain(
98+
["min_percent", "max_percent", "stretch"]
99+
.iter()
100+
.map(|s| (SmolStr::new_static(s), Type::Float32)),
101+
)
102+
.collect(),
103+
name: Some("slint::private_api::LayoutInfo".into()),
104+
node: None,
105+
rust_attributes: None,
106+
}));
90107
Self {
91108
enums: BuiltinEnums::new(),
92109
logical_point_type: Type::Struct(Rc::new(Struct {
@@ -119,6 +136,20 @@ impl BuiltinTypes {
119136
return_type: None,
120137
args: vec![Type::String],
121138
})),
139+
layout_info_type: layout_info_type.clone(),
140+
path_element_type: Type::Struct(Rc::new(Struct {
141+
fields: Default::default(),
142+
name: Some("PathElement".into()),
143+
node: None,
144+
rust_attributes: None,
145+
})),
146+
box_layout_cell_data_type: Type::Struct(Rc::new(Struct {
147+
fields: IntoIterator::into_iter([("constraint".into(), layout_info_type)])
148+
.collect(),
149+
name: Some("BoxLayoutCellData".into()),
150+
node: None,
151+
rust_attributes: None,
152+
})),
122153
}
123154
}
124155
}
@@ -633,3 +664,18 @@ pub fn logical_point_type() -> Type {
633664
pub fn font_metrics_type() -> Type {
634665
BUILTIN.with(|types| types.font_metrics_type.clone())
635666
}
667+
668+
/// The [`Type`] for a runtime LayoutInfo structure
669+
pub fn layout_info_type() -> Type {
670+
BUILTIN.with(|types| types.layout_info_type.clone())
671+
}
672+
673+
/// The [`Type`] for a runtime PathElement structure
674+
pub fn path_element_type() -> Type {
675+
BUILTIN.with(|types| types.path_element_type.clone())
676+
}
677+
678+
/// The [`Type`] for a runtime BoxLayoutCellData structure
679+
pub fn box_layout_cell_data_type() -> Type {
680+
BUILTIN.with(|types| types.box_layout_cell_data_type.clone())
681+
}

0 commit comments

Comments
 (0)