From 67fe1e8284b341e9a189cc4d390992728fc79f2e Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 19 Nov 2026 11:33:49 +0100 Subject: [PATCH] perf(trace): pack lists without a string per distinct item --- core/engine/src/compiled/trace/v1/rows.rs | 43 ++++++++++++++--------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/core/engine/src/compiled/trace/v1/rows.rs b/core/engine/src/compiled/trace/v1/rows.rs index 428bd5fa..64edbbb0 100644 --- a/core/engine/src/compiled/trace/v1/rows.rs +++ b/core/engine/src/compiled/trace/v1/rows.rs @@ -403,8 +403,7 @@ impl<'t> RowWriter<'t> { at.push(found); } if !(firsts.len() * 2 > count && (1..count).all(|i| at[i] != at[i - 1])) { - let texts: Vec = firsts.iter().map(|&i| item(i).to_string()).collect(); - packed = emit_packed(&at, &texts, out); + packed = emit_packed(&at, firsts.len(), |k| item(firsts[k]), out); } } if !packed { @@ -503,7 +502,7 @@ impl<'t> RowWriter<'t> { at.push(k as u32); } out.push_str(",\"rules\":"); - if emit_packed(&at, &texts, out) { + if emit_packed(&at, texts.len(), |k| texts[k].as_str(), out) { return true; } out.push('['); @@ -914,23 +913,35 @@ fn pack_list(items: &[Variable], out: &mut String) -> Option { if firsts.len() * 2 > n && (1..n).all(|i| at[i] != at[i - 1]) { return None; } - let mut texts: Vec = Vec::with_capacity(firsts.len()); + let mut texts = String::new(); + let mut ends: Vec = Vec::with_capacity(firsts.len()); for &i in &firsts { - let mut text = String::new(); - if !write_clean(&items[i], &mut text) { + if !write_clean(&items[i], &mut texts) { return None; } - texts.push(text); + ends.push(texts.len()); } - emit_packed(&at, &texts, out).then_some(true) + let text = |k: usize| { + let start = match k { + 0 => 0, + _ => ends[k - 1], + }; + &texts[start..ends[k]] + }; + emit_packed(&at, firsts.len(), text, out).then_some(true) } -fn emit_packed(at: &[u32], texts: &[String], out: &mut String) -> bool { +fn emit_packed<'t>( + at: &[u32], + distinct: usize, + text: impl Fn(usize) -> &'t str, + out: &mut String, +) -> bool { let n = at.len(); if n < 4 { return false; } - let len = |i: usize| texts[at[i] as usize].len(); + let len = |i: usize| text(at[i] as usize).len(); let plain: usize = (0..n).map(|i| len(i) + 1).sum::() + 1; let mut runs: Vec<(u32, usize)> = Vec::new(); for &k in at { @@ -941,10 +952,10 @@ fn emit_packed(at: &[u32], texts: &[String], out: &mut String) -> bool { } let runs_size: usize = runs .iter() - .map(|(k, count)| texts[*k as usize].len() + super::core::digits(*count) + 4) + .map(|(k, count)| text(*k as usize).len() + super::core::digits(*count) + 4) .sum::() + 12; - let pick_size: usize = texts.iter().map(|t| t.len() + 1).sum::() + let pick_size: usize = (0..distinct).map(|k| text(k).len() + 1).sum::() + at.iter() .map(|&k| super::core::digits(k as usize) + 1) .sum::() @@ -957,7 +968,7 @@ fn emit_packed(at: &[u32], texts: &[String], out: &mut String) -> bool { out.push(','); } out.push('['); - out.push_str(&texts[*k as usize]); + out.push_str(text(*k as usize)); if *count > 1 { out.push(','); push_digits(*count, out); @@ -969,11 +980,11 @@ fn emit_packed(at: &[u32], texts: &[String], out: &mut String) -> bool { } if (pick_size as f64) < plain8 { out.push_str("{\"$pick\":["); - for (i, text) in texts.iter().enumerate() { - if i > 0 { + for k in 0..distinct { + if k > 0 { out.push(','); } - out.push_str(text); + out.push_str(text(k)); } out.push_str("],\"at\":["); for (i, k) in at.iter().enumerate() {