mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-16 23:34:19 +00:00
perf(engine): replace linear scans that made document-sized inputs quadratic (#1921)
* perf(engine): replace linear scans that made document-sized inputs quadratic enum_of re-classified the whole string for every character; widget names, comment mentions and devcompare's common siblings each searched a growing Vec; data.fill's export names and codegen's class names retried every suffix from 2 against a list, cubic in the number of copies of one name; and a batch rename template had no length cap although it is expanded again for every match. * address review: report the pattern in its own length error
This commit is contained in:
1 parent
972ccc6a17
commit
ee4edc7364
11 files changed
+87
-25
No files matched your search
@@ -373,10 +373,11 @@ fn listed<'s>(cmd: &str, s: &'s Session, p: &Value, who: &str, people: &[&str])
|
||||
|
||||
/// Add the names `text` @mentions to `named` as `{name, known, you}`, each once.
|
||||
fn push_mentions(named: &mut Vec<Value>, text: &str, people: &[&str], user: &str) {
|
||||
// `known` and `you` follow from the name, so the name alone tells a repeat.
|
||||
let mut seen: std::collections::BTreeSet<String> = named.iter().filter_map(|v| v.get("name")?.as_str().map(str::to_string)).collect();
|
||||
for (name, known) in mentioned(text, people) {
|
||||
let v = json!({"name": name, "known": known, "you": known && name.eq_ignore_ascii_case(user)});
|
||||
if !named.contains(&v) {
|
||||
named.push(v);
|
||||
if seen.insert(name.to_string()) {
|
||||
named.push(json!({"name": name, "known": known, "you": known && name.eq_ignore_ascii_case(user)}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,8 @@ pub(crate) fn enum_of<T: serde::de::DeserializeOwned>(v: &Value) -> Option<T> {
|
||||
let norm: String = {
|
||||
let mut out = String::new();
|
||||
let mut up = false;
|
||||
// Once, not per character: a long value made this quadratic before it was refused.
|
||||
let no_lowercase = s.chars().all(|c| !c.is_lowercase() || !c.is_alphabetic());
|
||||
for (i, ch) in s.trim().chars().enumerate() {
|
||||
if ch == '_' || ch == '-' || ch == ' ' {
|
||||
up = true;
|
||||
@@ -125,7 +127,7 @@ pub(crate) fn enum_of<T: serde::de::DeserializeOwned>(v: &Value) -> Option<T> {
|
||||
} else if up {
|
||||
out.extend(ch.to_uppercase());
|
||||
up = false;
|
||||
} else if s.chars().all(|c| !c.is_lowercase() || !c.is_alphabetic()) {
|
||||
} else if no_lowercase {
|
||||
out.extend(ch.to_lowercase());
|
||||
} else {
|
||||
out.push(ch);
|
||||
|
||||
@@ -189,14 +189,21 @@ fn fill_layers(s: &mut Session, copy: NodeId, (key, value): (&str, &Value), miss
|
||||
/// Export each copy into `dir` as `<name>.<format>` at `scale`, adding `files` (and `errors`) to
|
||||
/// `out`.
|
||||
fn export_copies(s: &mut Session, ids: &[(NodeId, String)], dir: &str, (format, scale): (&str, f64), out: &mut Value) {
|
||||
let (mut files, mut errors, mut used) = (Vec::new(), Vec::new(), Vec::<String>::new());
|
||||
let (mut files, mut errors) = (Vec::new(), Vec::new());
|
||||
let mut used = std::collections::BTreeSet::<String>::new();
|
||||
// The next number to try per base, so many copies of one name don't retry from 2 each time.
|
||||
let mut next = std::collections::BTreeMap::<String, u64>::new();
|
||||
for (copy, name) in ids {
|
||||
// One file per copy: same or same-looking names get " 2", " 3"… (case-insensitively,
|
||||
// as macOS and Windows file systems compare names).
|
||||
let base = file_name(name);
|
||||
let stem =
|
||||
(1..).map(|k| if k == 1 { base.clone() } else { format!("{base} {k}") }).find(|n| !used.contains(&n.to_lowercase())).unwrap_or(base);
|
||||
used.push(stem.to_lowercase());
|
||||
let from = next.get(&base.to_lowercase()).copied().unwrap_or(1);
|
||||
let (k, stem) = (from..)
|
||||
.map(|k| (k, if k == 1 { base.clone() } else { format!("{base} {k}") }))
|
||||
.find(|(_, n)| !used.contains(&n.to_lowercase()))
|
||||
.unwrap_or((from, base.clone()));
|
||||
next.insert(base.to_lowercase(), k + 1);
|
||||
used.insert(stem.to_lowercase());
|
||||
let path = std::path::Path::new(dir).join(format!("{stem}.{format}")).to_string_lossy().to_string();
|
||||
match s.execute("file.export", &json!({"ids": [copy.0], "format": format, "scale": scale, "path": path})) {
|
||||
Ok(_) => files.push(path),
|
||||
|
||||
@@ -34,7 +34,7 @@ pub use css::{CssNames, css_var_name, css_var_of};
|
||||
use css::{css, tok};
|
||||
pub(crate) use flutter::{FLUTTER_IMPORT, Place, flutter, flutter_indent};
|
||||
use html::html;
|
||||
pub(crate) use html::{Markup, class_name};
|
||||
pub(crate) use html::{Markup, Names, class_name};
|
||||
pub(crate) use swift::swift;
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
|
||||
@@ -30,7 +30,7 @@ fn kind_word(n: &Node) -> &str {
|
||||
/// `rogeuin-hwamyeon`, кнопка → `knopka`, über → `uber` (per-codepoint transliteration, the slug
|
||||
/// libraries' way); a name left with nothing (symbols only) takes `kind`, and one starting with a
|
||||
/// digit keeps the `layer-` prefix.
|
||||
pub(crate) fn class_name(name: &str, kind: &str, used: &mut Vec<String>) -> String {
|
||||
pub(crate) fn class_name(name: &str, kind: &str, used: &mut Names) -> String {
|
||||
let mut base = String::new();
|
||||
for c in deunicode::deunicode(name).chars().flat_map(char::to_lowercase) {
|
||||
if c.is_ascii_alphanumeric() {
|
||||
@@ -57,11 +57,30 @@ pub(crate) fn class_name(name: &str, kind: &str, used: &mut Vec<String>) -> Stri
|
||||
} else {
|
||||
base.to_string()
|
||||
};
|
||||
let name = (1..).map(|k| if k == 1 { base.clone() } else { format!("{base}-{k}") }).find(|n| !used.contains(n)).unwrap_or(base);
|
||||
used.push(name.clone());
|
||||
let from = used.next.get(&base).copied().unwrap_or(1);
|
||||
let (k, name) = (from..)
|
||||
.map(|k| (k, if k == 1 { base.clone() } else { format!("{base}-{k}") }))
|
||||
.find(|(_, n)| !used.used.contains(n))
|
||||
.unwrap_or((from, base.clone()));
|
||||
used.next.insert(base, k + 1);
|
||||
used.insert(name.clone());
|
||||
name
|
||||
}
|
||||
|
||||
/// The names [`class_name`] has given out, and the next suffix to try per base, so many layers
|
||||
/// sharing one name don't each retry every suffix from 2 (that was cubic in their number).
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Names {
|
||||
used: std::collections::BTreeSet<String>,
|
||||
next: std::collections::BTreeMap<String, u64>,
|
||||
}
|
||||
|
||||
impl Names {
|
||||
pub fn insert(&mut self, name: String) {
|
||||
self.used.insert(name);
|
||||
}
|
||||
}
|
||||
|
||||
fn html_escape(s: &str) -> String {
|
||||
s.replace('&', "&").replace('<', "<").replace('>', ">").replace('\n', "<br>")
|
||||
}
|
||||
@@ -104,7 +123,7 @@ pub(crate) struct Markup<'c, 'a> {
|
||||
ctx: &'c Ctx<'a>,
|
||||
/// Html, React, Vue or Leptos.
|
||||
lang: CodeLang,
|
||||
used: Vec<String>,
|
||||
used: Names,
|
||||
pub markup: String,
|
||||
pub styles: String,
|
||||
pub tokens: Tokens,
|
||||
@@ -161,7 +180,7 @@ impl<'c, 'a> Markup<'c, 'a> {
|
||||
Markup {
|
||||
ctx,
|
||||
lang,
|
||||
used: Vec::new(),
|
||||
used: Names::default(),
|
||||
markup: String::new(),
|
||||
styles: String::new(),
|
||||
tokens: Tokens::default(),
|
||||
@@ -276,7 +295,7 @@ impl<'c, 'a> Markup<'c, 'a> {
|
||||
let root = n.id == self.ctx.root;
|
||||
let class = match self.root_class.take().filter(|_| root) {
|
||||
Some(c) => {
|
||||
self.used.push(c.clone());
|
||||
self.used.insert(c.clone());
|
||||
c
|
||||
}
|
||||
None => {
|
||||
@@ -332,7 +351,7 @@ impl<'c, 'a> Markup<'c, 'a> {
|
||||
let open = open("div");
|
||||
let slot_ident = slot.map(|p| match self.ctx.comp {
|
||||
Some(c) => crate::component_codegen::slot_name(c, p),
|
||||
None => class_name(&p.ident, "slot", &mut Vec::new()),
|
||||
None => class_name(&p.ident, "slot", &mut Names::default()),
|
||||
});
|
||||
if let Some(p) = slot.filter(|_| kids.is_empty()) {
|
||||
let content = if leptos {
|
||||
|
||||
@@ -11,7 +11,9 @@ use figcraft_doc::{Document, NodeId, NodeKind, PropDef};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::cmd::CommandSpec;
|
||||
use crate::codegen::{CodeLang, Ctx, FLUTTER_IMPORT, Markup, Place, arkui, class_name, compose, flutter, flutter_indent, indent, literal, swift};
|
||||
use crate::codegen::{
|
||||
CodeLang, Ctx, FLUTTER_IMPORT, Markup, Names, Place, arkui, class_name, compose, flutter, flutter_indent, indent, literal, swift,
|
||||
};
|
||||
use crate::components::{prop_default_value, slot_edited, slots_of, variant_options, variant_props, variant_set};
|
||||
use crate::params::{id_of, string, targets};
|
||||
use crate::{bad, spec};
|
||||
@@ -616,14 +618,14 @@ pub(crate) fn value_expr(doc: &Document, c: &Component, p: &Param, value: &Value
|
||||
/// The `<slot name="…">` of a slot parameter: unique among the component's slot parameters, unlike
|
||||
/// `class_name` called with a fresh list per parameter (two idents can lowercase alike).
|
||||
pub(crate) fn slot_name(c: &Component, p: &Param) -> String {
|
||||
let mut used = Vec::new();
|
||||
let mut used = Names::default();
|
||||
for q in c.params.iter().filter(|q| matches!(q.kind, ParamKind::Slot)) {
|
||||
let name = class_name(&q.ident, "slot", &mut used);
|
||||
if q.ident == p.ident {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
class_name(&p.ident, "slot", &mut Vec::new())
|
||||
class_name(&p.ident, "slot", &mut Names::default())
|
||||
}
|
||||
|
||||
/// The values an instance sets for `c`'s parameters, in order (`None` for other layers): variant
|
||||
@@ -720,7 +722,7 @@ pub(crate) fn usage_with_styles(doc: &Document, id: NodeId, lang: CodeLang, dept
|
||||
.filter(|(name, _, _)| is_slot(name))
|
||||
.map(|(name, expr, _)| {
|
||||
let slot = c.params.iter().find(|q| q.ident == *name);
|
||||
let name = slot.map(|p| slot_name(&c, p)).unwrap_or_else(|| class_name(name, "slot", &mut Vec::new()));
|
||||
let name = slot.map(|p| slot_name(&c, p)).unwrap_or_else(|| class_name(name, "slot", &mut Names::default()));
|
||||
format!("{pad} <template #{name}>\n{expr}{pad} </template>\n")
|
||||
})
|
||||
.collect();
|
||||
@@ -823,7 +825,7 @@ pub fn definition(doc: &Document, c: &Component, lang: CodeLang) -> String {
|
||||
|
||||
/// `data-*` attribute names for variant parameters, unique among them (in order).
|
||||
fn data_attrs(variants: &[&Param]) -> Vec<String> {
|
||||
let mut used = Vec::new();
|
||||
let mut used = Names::default();
|
||||
variants.iter().map(|p| format!("data-{}", class_name(&p.prop, "prop", &mut used))).collect()
|
||||
}
|
||||
|
||||
@@ -832,7 +834,7 @@ fn data_attrs(variants: &[&Param]) -> Vec<String> {
|
||||
fn web(doc: &Document, c: &Component, lang: CodeLang) -> (String, String, String) {
|
||||
let ctx = Ctx { doc, root: c.comp, comp: Some(c), base: 2 };
|
||||
let root_name = c.set.and_then(|s| doc.node(s)).or_else(|| doc.node(c.comp)).map(|n| n.name.clone()).unwrap_or_default();
|
||||
let root_class = class_name(&root_name, "component", &mut Vec::new());
|
||||
let root_class = class_name(&root_name, "component", &mut Names::default());
|
||||
let variants: Vec<&Param> = c.params.iter().filter(|p| matches!(p.kind, ParamKind::Variant(_))).collect();
|
||||
let attr_names = data_attrs(&variants);
|
||||
let pass = |root: NodeId| {
|
||||
|
||||
@@ -288,8 +288,10 @@ pub fn layer_changes(before: &Document, now: &Document, id: NodeId) -> Vec<PropC
|
||||
fn order(before: &Document, now: &Document, id: NodeId) -> Option<(usize, usize)> {
|
||||
let parent = now.node(id)?.parent?;
|
||||
let (pb, pa) = (before.node(parent)?, now.node(parent)?);
|
||||
let common_b: Vec<NodeId> = pb.children.iter().rev().copied().filter(|c| pa.children.contains(c)).collect();
|
||||
let common_a: Vec<NodeId> = pa.children.iter().rev().copied().filter(|c| pb.children.contains(c)).collect();
|
||||
let (in_b, in_a): (std::collections::BTreeSet<NodeId>, std::collections::BTreeSet<NodeId>) =
|
||||
(pb.children.iter().copied().collect(), pa.children.iter().copied().collect());
|
||||
let common_b: Vec<NodeId> = pb.children.iter().rev().copied().filter(|c| in_a.contains(c)).collect();
|
||||
let common_a: Vec<NodeId> = pa.children.iter().rev().copied().filter(|c| in_b.contains(c)).collect();
|
||||
let (ib, ia) = (common_b.iter().position(|c| *c == id)?, common_a.iter().position(|c| *c == id)?);
|
||||
(ib != ia).then_some((ib + 1, ia + 1))
|
||||
}
|
||||
|
||||
@@ -157,8 +157,9 @@ fn state_of(doc: &Document, w: NodeId) -> Map<String, Value> {
|
||||
/// fields), each once, in layer order.
|
||||
fn names(doc: &Document, w: NodeId, key: &str) -> Vec<String> {
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for t in doc.subtree(w).into_iter().filter_map(|i| doc.node(i)?.plugin_data.get(key).cloned()) {
|
||||
if !out.contains(&t) {
|
||||
if seen.insert(t.clone()) {
|
||||
out.push(t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,20 @@ fn non_ascii_layer_names_transliterate_into_readable_classes() {
|
||||
assert!(code.contains("<p class=\"rogeuin\">") && code.contains("<p class=\"rogeuin-2\">"), "{code}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_layers_with_one_name_get_numbered_in_linear_time() {
|
||||
// Each name used to retry every suffix from 2 against a list: cubic in their number.
|
||||
let mut used = crate::codegen::Names::default();
|
||||
let start = std::time::Instant::now();
|
||||
let names: Vec<String> = (0..20_000).map(|_| crate::codegen::class_name("Card / Title", "frame", &mut used)).collect();
|
||||
assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
|
||||
assert_eq!(names[0], "card-title");
|
||||
assert_eq!(names[1], "card-title-2");
|
||||
assert_eq!(names[19_999], "card-title-20000");
|
||||
let distinct: std::collections::BTreeSet<&String> = names.iter().collect();
|
||||
assert_eq!(distinct.len(), names.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_scripts_and_symbol_names_transliterate_or_fall_back_to_the_kind() {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -277,6 +277,11 @@ fn renaming_several_layers_by_the_documented_codes() {
|
||||
assert_eq!(r(None, "$n", Some(1e30), "a"), "1000000000000");
|
||||
assert_eq!(r(None, "$n", Some(f64::NAN), "a"), "1");
|
||||
assert_eq!(r(Some("é"), "e", None, "café é"), "cafe e");
|
||||
// A template is expanded once per match, so its length is capped (1,000 characters).
|
||||
let long = "$nn".repeat(400);
|
||||
assert!(crate::rename::BatchRename::new(Some("a"), false, &long, None, None).is_err());
|
||||
assert!(crate::rename::BatchRename::new(None, false, "x", Some(&long), None).is_err_and(|e| e.contains("pattern")));
|
||||
assert!(crate::rename::BatchRename::new(Some("a"), false, &"$nn".repeat(300), None, None).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -9,6 +9,9 @@ use figcraft_doc::{Document, NodeId};
|
||||
|
||||
/// Longest match pattern accepted (characters).
|
||||
const MAX_FIND: usize = 1_000;
|
||||
/// Longest replacement or pattern template accepted (characters): each is expanded again for
|
||||
/// every match, so its length multiplies the work of a rename.
|
||||
const MAX_TEMPLATE: usize = 1_000;
|
||||
/// Longest name a rename produces (characters).
|
||||
const MAX_NAME: usize = 10_000;
|
||||
/// Widest zero padding of a number code.
|
||||
@@ -50,6 +53,12 @@ impl BatchRename {
|
||||
}
|
||||
Some(f) => Some(Find::Text(f.to_string())),
|
||||
};
|
||||
if replace.chars().count() > MAX_TEMPLATE {
|
||||
return Err(format!("the replacement is longer than {MAX_TEMPLATE} characters"));
|
||||
}
|
||||
if pattern.is_some_and(|p| p.chars().count() > MAX_TEMPLATE) {
|
||||
return Err(format!("the pattern is longer than {MAX_TEMPLATE} characters"));
|
||||
}
|
||||
let start = start.filter(|s| s.is_finite()).map_or(1, |s| s.clamp(0.0, 1e12).round() as u64);
|
||||
Ok(Self { find, replace: replace.to_string(), pattern: pattern.map(str::to_string), start })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user