mirror of
https://github.com/gorules/zen.git
synced 2026-11-30 00:07:34 +00:00
perf(trace): reuse a node's rendered body for rows with the same rule and cell values
This commit is contained in:
@@ -1170,6 +1170,42 @@ pub(crate) struct Edits<'a> {
|
||||
groups: Vec<EditGroup<'a>>,
|
||||
}
|
||||
|
||||
#[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<Cell> {
|
||||
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<Cell> {
|
||||
Self::of(value).filter(|cell| *cell != Cell::Container)
|
||||
}
|
||||
|
||||
fn earlier(value: Option<&Variable>) -> Option<Cell> {
|
||||
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<Cell>,
|
||||
) -> 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<Cell>,
|
||||
) -> 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>,
|
||||
|
||||
@@ -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<Plan<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Bodies {
|
||||
texts: super::value::FastMap<Vec<Cell>, Box<str>>,
|
||||
hits: u32,
|
||||
misses: u32,
|
||||
off: bool,
|
||||
}
|
||||
|
||||
struct Plan<'a> {
|
||||
bodies: Vec<std::cell::RefCell<Bodies>>,
|
||||
positions: Vec<Box<[u32]>>,
|
||||
perf: Vec<String>,
|
||||
order: Vec<Option<Box<[u32]>>>,
|
||||
@@ -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<Cell>,
|
||||
) -> 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<Variable> {
|
||||
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<Cell> = 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);
|
||||
|
||||
Reference in New Issue
Block a user