Use SmolStr in more places of the compiler infrastructure

This removes a lot of allocations and speeds up the compiler step
a bit. Sadly, this patch is very invasive as it touches a lot of
files. That said, each individual hunk is pretty trivial.

For a non-trivial real-world example, the impact is significant,
we get rid of ~29% of all allocations and improve the runtime by
about 4.8% (measured until the viewer loop would start).

Before:
```
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
```

After:
```
Benchmark 1: ./target/release/slint-viewer ../slint-perf/app.slint
  Time (mean ± σ):     639.5 ms ±  17.8 ms    [User: 556.9 ms, System: 76.2 ms]
  Range (min … max):   621.4 ms … 666.5 ms    10 runs

        allocations:            3544318
        temporary allocations:  495685
```
This commit is contained in:
Milian Wolff 2024-10-03 18:06:59 +02:00 committed by Olivier Goffart
parent 08a3a6cc4a
commit 0f6c3a4fd7
75 changed files with 679 additions and 555 deletions

View file

@ -14,6 +14,7 @@ use napi::JsString;
use napi::JsUnknown;
use slint_interpreter::Compiler;
use slint_interpreter::Value;
use smol_str::StrExt;
use std::collections::HashMap;
use std::path::PathBuf;
@ -147,14 +148,14 @@ impl JsComponentCompiler {
let mut o = env.create_object().ok()?;
for value in en.values.iter() {
let value = value.replace('-', "_");
let value = value.replace_smolstr("-", "_");
o.set_property(
env.create_string(&value).ok()?,
env.create_string(&value).ok()?.into_unknown(),
)
.ok()?;
}
return Some((en.name.clone(), o.into_unknown()));
return Some((en.name.to_string(), o.into_unknown()));
}
_ => return None,
}

View file

@ -13,6 +13,7 @@ use napi::bindgen_prelude::*;
use napi::{Env, JsBoolean, JsNumber, JsObject, JsString, JsUnknown, Result};
use napi_derive::napi;
use slint_interpreter::Value;
use smol_str::SmolStr;
#[napi(js_name = "ValueType")]
pub enum JsValueType {
@ -229,7 +230,7 @@ pub fn to_value(env: &Env, unknown: JsUnknown, typ: &Type) -> Result<Value> {
} else {
to_value(env, prop, pro_ty)?
};
Ok((pro_name.clone(), prop_value))
Ok((pro_name.to_string(), prop_value))
})
.collect::<Result<_, _>>()?,
))
@ -260,7 +261,7 @@ pub fn to_value(env: &Env, unknown: JsUnknown, typ: &Type) -> Result<Value> {
}
Type::Enumeration(e) => {
let js_string: JsString = unknown.try_into()?;
let value: String = js_string.into_utf8()?.as_str()?.into();
let value: SmolStr = js_string.into_utf8()?.as_str()?.into();
if !e.values.contains(&value) {
return Err(napi::Error::from_reason(format!(
@ -269,7 +270,7 @@ pub fn to_value(env: &Env, unknown: JsUnknown, typ: &Type) -> Result<Value> {
)));
}
Ok(Value::EnumerationValue(e.name.clone(), value))
Ok(Value::EnumerationValue(e.name.to_string(), value.to_string()))
}
Type::Invalid
| Type::Model