Skip to content

Commit

Permalink
Use to_smolstr in a few more places to reduce allocation pressure
Browse files Browse the repository at this point in the history
Before:
```
Benchmark 1: ./target/release/slint-viewer ../slint-perf/app.slint
  Time (mean ± σ):     626.1 ms ±  16.0 ms    [User: 548.6 ms, System: 72.6 ms]
  Range (min … max):   610.9 ms … 653.0 ms    10 runs

        allocations:            3395727
        temporary allocations:  466634
```

After:
```
Benchmark 1: ./target/release/slint-viewer ../slint-perf/app.slint
  Time (mean ± σ):     622.6 ms ±  13.9 ms    [User: 556.6 ms, System: 63.5 ms]
  Range (min … max):   609.7 ms … 645.4 ms    10 runs

        allocations:            3371931
        temporary allocations:  459315
```
  • Loading branch information
milianw committed Oct 16, 2024
1 parent 7e453e7 commit 81acf08
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions internal/compiler/object_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ impl Element {
) -> ElementRc {
let base_type = if let Some(base_node) = node.QualifiedName() {
let base = QualifiedTypeName::from_node(base_node.clone());
let base_string = base.to_string();
let base_string = base.to_smolstr();
match parent_type.lookup_type_for_child_element(&base_string, tr) {
Ok(ElementType::Component(c)) if c.is_global() => {
diag.push_error(
Expand Down Expand Up @@ -1509,7 +1509,7 @@ impl Element {
}
}

if r.borrow().base_type.to_string() == "ListView" {
if r.borrow().base_type.to_smolstr() == "ListView" {
let mut seen_for = false;
for se in node.children() {
if se.kind() == SyntaxKind::RepeatedElement && !seen_for {
Expand Down Expand Up @@ -1662,7 +1662,7 @@ impl Element {
match lookup_result.property_type {
Type::Invalid => {
if self.base_type != ElementType::Error {
diag.push_error(if self.base_type.to_string() == "Empty" {
diag.push_error(if self.base_type.to_smolstr() == "Empty" {
format!( "Unknown property {unresolved_name}")
} else {
format!( "Unknown property {unresolved_name} in {}", self.base_type)
Expand Down Expand Up @@ -1871,7 +1871,7 @@ pub fn type_from_node(

let prop_type = tr.lookup_qualified(&qualified_type.members);

if prop_type == Type::Invalid && tr.lookup_element(&qualified_type.to_string()).is_err() {
if prop_type == Type::Invalid && tr.lookup_element(&qualified_type.to_smolstr()).is_err() {
diag.push_error(format!("Unknown type '{}'", qualified_type), &qualified_type_node);
} else if !prop_type.is_property_type() {
diag.push_error(
Expand Down
4 changes: 3 additions & 1 deletion internal/compiler/passes/binding_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::namedreference::NamedReference;
use crate::object_tree::{find_parent_element, Document, ElementRc, PropertyAnimation};
use derive_more as dm;

use smol_str::ToSmolStr;

/// Maps the alias in the other direction than what the BindingExpression::two_way_binding does.
/// So if binding for property A has B in its BindingExpression::two_way_binding, then
/// ReverseAliases maps B to A.
Expand Down Expand Up @@ -478,7 +480,7 @@ fn visit_implicit_layout_info_dependencies(
item: &ElementRc,
vis: &mut impl FnMut(&PropertyPath, ReadType),
) {
let base_type = item.borrow().base_type.to_string();
let base_type = item.borrow().base_type.to_smolstr();
const N: ReadType = ReadType::NativeRead;
match base_type.as_str() {
"Image" => {
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/passes/lower_property_to_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::expression_tree::{BindingExpression, Expression, NamedReference};
use crate::langtype::Type;
use crate::object_tree::{self, Component, Element, ElementRc};
use crate::typeregister::TypeRegister;
use smol_str::format_smolstr;
use smol_str::{format_smolstr, ToSmolStr};
use std::rc::Rc;

/// If any element in `component` declares a binding to `property_name`, then a new
Expand All @@ -36,7 +36,7 @@ pub(crate) fn lower_property_to_element(
}

object_tree::recurse_elem_including_sub_components_no_borrow(component, &(), &mut |elem, _| {
if elem.borrow().base_type.to_string() == element_name {
if elem.borrow().base_type.to_smolstr() == element_name {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/passes/unique_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn assign_unique_id_in_component(component: &Rc<Component>, count: &mut u32) {
let old_id = if !elem_mut.id.is_empty() {
elem_mut.id.clone()
} else {
elem_mut.base_type.to_string().to_ascii_lowercase().into()
elem_mut.base_type.to_smolstr().to_ascii_lowercase().into()
};
elem_mut.id = format_smolstr!("{}-{}", old_id, count);

Expand Down
6 changes: 3 additions & 3 deletions internal/compiler/typeloader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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

use smol_str::SmolStr;
use smol_str::{SmolStr, ToSmolStr};
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -59,10 +59,10 @@ impl ImportedName {

pub fn from_node(importident: syntax_nodes::ImportIdentifier) -> Self {
let external_name =
parser::normalize_identifier(importident.ExternalName().text().to_string().trim());
parser::normalize_identifier(importident.ExternalName().text().to_smolstr().trim());

let internal_name = match importident.InternalName() {
Some(name_ident) => parser::normalize_identifier(name_ident.text().to_string().trim()),
Some(name_ident) => parser::normalize_identifier(name_ident.text().to_smolstr().trim()),
None => external_name.clone(),
};

Expand Down

0 comments on commit 81acf08

Please sign in to comment.