diff --git a/core/engine/src/compiled/graph/data.rs b/core/engine/src/compiled/graph/data.rs index b2b77bd3..9aca0959 100644 --- a/core/engine/src/compiled/graph/data.rs +++ b/core/engine/src/compiled/graph/data.rs @@ -1170,6 +1170,42 @@ pub(crate) struct Edits<'a> { groups: Vec>, } +#[derive(Clone, PartialEq, Eq, Hash)] +pub(crate) enum Cell { + Skip, + Absent, + Container, + Null, + Bool(bool), + Number(i128, u32), + Text(Symbol), + Mark(u32), +} + +impl Cell { + pub fn of(value: &Variable) -> Option { + match value { + Variable::Null => Some(Cell::Null), + Variable::Bool(b) => Some(Cell::Bool(*b)), + Variable::Number(d) => Some(Cell::Number(d.mantissa(), d.scale())), + Variable::String(s) => Some(Cell::Text(s.clone())), + Variable::Array(_) | Variable::Object(_) => Some(Cell::Container), + Variable::Dynamic(_) => None, + } + } + + fn scalar(value: &Variable) -> Option { + Self::of(value).filter(|cell| *cell != Cell::Container) + } + + fn earlier(value: Option<&Variable>) -> Option { + match value { + Some(value) => Self::of(value), + None => Some(Cell::Absent), + } + } +} + enum Earlier { Absent, Value(Variable), @@ -1285,6 +1321,114 @@ impl<'a> Data<'a> { Some(Edits { groups }) } + pub fn edit_key( + &self, + input: &Data<'a>, + edits: &Edits<'a>, + row: usize, + key: &mut Vec, + ) -> bool { + self.edit_cells(input, edits, row, key).is_some() + } + + fn edit_cells( + &self, + input: &Data<'a>, + edits: &Edits<'a>, + row: usize, + key: &mut Vec, + ) -> Option<()> { + let layer = self.extends(input)?; + if !self.object(row) || !input.object(row) { + return None; + } + let earlier = input.leaves(); + let known = |index: usize| match earlier[index].1.get(row) { + Variable::Null => None, + value => Some(value), + }; + for group in &edits.groups { + if !group.layer.iter().any(|&index| layer.has(index, row)) { + key.push(Cell::Skip); + continue; + } + let based = || { + input + .based(row) + .and_then(|base| Self::lookup(base, &group.top)) + }; + match &group.kind { + EditKind::Dynamic => return None, + EditKind::Flat { column, before, .. } => { + let was = match before { + Some(leaf) => Cell::of(&known(*leaf)?)?, + None => Cell::earlier(based().as_ref())?, + }; + key.push(was); + key.push(Cell::scalar(&column.get(row))?); + } + EditKind::Nested { + fields, + exact, + under, + } => { + let was = match (exact, under.is_empty()) { + (Some(leaf), _) => Some(known(*leaf)?), + (None, false) => match under.iter().any(|&leaf| known(leaf).is_some()) { + true => Some(Variable::empty_object()), + false => return None, + }, + (None, true) => based(), + }; + match &was { + Some(Variable::Object(map)) => { + key.push(Cell::Mark(1)); + let outer = match (exact, under.is_empty()) { + (None, false) => None, + _ => Some(map), + }; + for field in fields { + if !layer.has(field.index, row) { + key.push(Cell::Skip); + continue; + } + let now = Cell::scalar(&field.column.get(row))?; + if now == Cell::Null { + return None; + } + let before = match (field.before, outer) { + (Some(leaf), _) => Cell::of(&known(leaf)?)?, + (None, Some(map)) => { + Cell::earlier(map.borrow().get_str(&field.name))? + } + (None, None) => match based() { + Some(Variable::Object(map)) => { + Cell::earlier(map.borrow().get_str(&field.name))? + } + _ => Cell::Absent, + }, + }; + key.push(before); + key.push(now); + } + } + was => { + key.push(Cell::Mark(2)); + key.push(Cell::earlier(was.as_ref())?); + for &index in &group.layer { + match layer.has(index, row) { + true => key.push(Cell::scalar(&layer.columns[index].get(row))?), + false => key.push(Cell::Skip), + } + } + } + } + } + } + } + Some(()) + } + pub fn edit( &self, input: &Data<'a>, diff --git a/core/engine/src/compiled/trace/batch.rs b/core/engine/src/compiled/trace/batch.rs index d6563480..b31a9fb0 100644 --- a/core/engine/src/compiled/trace/batch.rs +++ b/core/engine/src/compiled/trace/batch.rs @@ -1,6 +1,6 @@ use super::collect::{Fact, Hit, Level, Record, Slot, Template}; use super::rows::RowWriter; -use crate::compiled::graph::data::Data; +use crate::compiled::graph::data::{Cell, Data}; use crate::compiled::host::graph::walker::GraphWalker; use smallvec::SmallVec; use std::cell::OnceCell; @@ -149,7 +149,16 @@ pub(crate) struct Traces<'a> { plan: OnceCell>, } +#[derive(Default)] +struct Bodies { + texts: super::value::FastMap, Box>, + hits: u32, + misses: u32, + off: bool, +} + struct Plan<'a> { + bodies: Vec>, positions: Vec>, perf: Vec, order: Vec>>, @@ -275,6 +284,7 @@ impl<'a> Traces<'a> { }) .collect(); Plan { + bodies: self.events.iter().map(|_| Default::default()).collect(), positions, perf: self .events @@ -308,6 +318,50 @@ impl<'a> Traces<'a> { }) } + fn keyed( + &self, + plan: &Plan<'a>, + at: usize, + position: usize, + walk: &Walk, + key: &mut Vec, + ) -> bool { + let event = &self.events[at]; + let (Some(edits), Some(Value::Data(after)), Encoding::Touched(Some(input))) = + (&plan.edits[at], &event.output, &event.encoding) + else { + return false; + }; + if walk + .inputs + .get(event.slot as usize) + .is_some_and(Option::is_some) + { + return false; + } + key.clear(); + match &event.facts { + Facts::None => key.push(Cell::Mark(0)), + Facts::First(rules) => match rules.get(position).copied().flatten() { + Some(rule) => { + key.push(Cell::Mark(1)); + key.push(Cell::Mark(rule)); + } + None => key.push(Cell::Mark(2)), + }, + Facts::Rows(facts) => match facts.get(position) { + Some(Fact::None) => key.push(Cell::Mark(3)), + Some(Fact::Hit(Hit { rule, refs: None })) => { + key.push(Cell::Mark(4)); + key.push(Cell::Mark(*rule)); + } + _ => return false, + }, + _ => return false, + } + after.edit_key(input, edits, position, key) + } + fn value(&self, walk: &mut Walk, slot: Slot) -> Option { let index = slot as usize; if let Some(Some(value)) = walk.values.get(index) { @@ -388,6 +442,7 @@ impl<'a> Traces<'a> { inputs: SmallVec::from_elem(None, self.slots), }; writer.begin(engine, evaluation, out); + let mut key: Vec = Vec::new(); for (at, event) in self.events.iter().enumerate() { let position = plan.positions[at][row]; if position == ABSENT { @@ -429,6 +484,23 @@ impl<'a> Traces<'a> { } let parent = parents.first().copied(); let mark = out.len(); + let mut bodies = plan.bodies[at].borrow_mut(); + let keyed = + !bodies.off && parent.is_some() && self.keyed(plan, at, position, &walk, &mut key); + if keyed { + if let Some(text) = bodies.texts.get(key.as_slice()) { + out.push_str(text); + bodies.hits += 1; + drop(bodies); + writer.end(event.slot, out); + continue; + } + bodies.misses += 1; + if bodies.misses > 512 && bodies.hits < bodies.misses || bodies.texts.len() > 4096 { + bodies.off = true; + bodies.texts = Default::default(); + } + } writer.request_only(request); let encoded = self.encode( plan, @@ -464,6 +536,10 @@ impl<'a> Traces<'a> { )?; } } + if keyed && !bodies.off { + bodies.texts.insert(key.clone(), out[mark..].into()); + } + drop(bodies); writer.end(event.slot, out); } writer.finish(out);