Improve performance of embedded field path creation

This commit is contained in:
Tobie Morgan Hitchcock 2022-04-07 15:33:57 +01:00
parent c4386f1549
commit 27a01bec08
6 changed files with 23 additions and 24 deletions

View file

@ -20,11 +20,11 @@ impl<'a> Document<'a> {
// Loop through all field statements // Loop through all field statements
for fd in self.fd(opt, txn).await?.iter() { for fd in self.fd(opt, txn).await?.iter() {
// Loop over each field in document // Loop over each field in document
for k in self.current.each(&fd.name).into_iter() { for k in self.current.each(&fd.name).iter() {
// Get the initial value // Get the initial value
let old = self.initial.pick(&k); let old = self.initial.pick(k);
// Get the current value // Get the current value
let mut val = self.current.pick(&k); let mut val = self.current.pick(k);
// Check for a VALUE clause // Check for a VALUE clause
if let Some(expr) = &fd.value { if let Some(expr) = &fd.value {
// Configure the context // Configure the context
@ -113,9 +113,9 @@ impl<'a> Document<'a> {
} }
// Set the value of the field // Set the value of the field
match val { match val {
Value::None => self.current.to_mut().del(ctx, opt, txn, &k).await?, Value::None => self.current.to_mut().del(ctx, opt, txn, k).await?,
Value::Void => self.current.to_mut().del(ctx, opt, txn, &k).await?, Value::Void => self.current.to_mut().del(ctx, opt, txn, k).await?,
_ => self.current.to_mut().set(ctx, opt, txn, &k, val).await?, _ => self.current.to_mut().set(ctx, opt, txn, k, val).await?,
}; };
} }
} }

View file

@ -115,21 +115,21 @@ impl<'a> Document<'a> {
// Loop through all field statements // Loop through all field statements
for fd in self.fd(opt, txn).await?.iter() { for fd in self.fd(opt, txn).await?.iter() {
// Loop over each field in document // Loop over each field in document
for k in out.each(&fd.name).into_iter() { for k in out.each(&fd.name).iter() {
// Process field permissions // Process field permissions
match &fd.permissions.select { match &fd.permissions.select {
Permission::Full => (), Permission::Full => (),
Permission::None => out.del(ctx, opt, txn, &k).await?, Permission::None => out.del(ctx, opt, txn, k).await?,
Permission::Specific(e) => { Permission::Specific(e) => {
// Get the current value // Get the current value
let val = self.current.pick(&k); let val = self.current.pick(k);
// Configure the context // Configure the context
let mut ctx = Context::new(ctx); let mut ctx = Context::new(ctx);
ctx.add_value("value".into(), val); ctx.add_value("value".into(), val);
let ctx = ctx.freeze(); let ctx = ctx.freeze();
// Process the PERMISSION clause // Process the PERMISSION clause
if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
out.del(&ctx, opt, txn, &k).await? out.del(&ctx, opt, txn, k).await?
} }
} }
} }

View file

@ -57,10 +57,9 @@ impl From<Vec<Part>> for Idiom {
} }
impl Idiom { impl Idiom {
pub fn add(&self, n: Part) -> Idiom { pub fn add(mut self, n: Part) -> Idiom {
let mut p = self.parts.to_vec(); self.parts.push(n);
p.push(n); self
Idiom::from(p)
} }
pub fn to_path(&self) -> String { pub fn to_path(&self) -> String {

View file

@ -13,7 +13,7 @@ impl Value {
if !b.value.contains_key(key) { if !b.value.contains_key(key) {
ops.push(Operation { ops.push(Operation {
op: Op::Remove, op: Op::Remove,
path: path.add(key.clone().into()), path: path.clone().add(key.clone().into()),
value: Value::Null, value: Value::Null,
}) })
} }
@ -23,11 +23,11 @@ impl Value {
match a.value.get(key) { match a.value.get(key) {
None => ops.push(Operation { None => ops.push(Operation {
op: Op::Add, op: Op::Add,
path: path.add(key.clone().into()), path: path.clone().add(key.clone().into()),
value: val.clone(), value: val.clone(),
}), }),
Some(old) => { Some(old) => {
let path = path.add(key.clone().into()); let path = path.clone().add(key.clone().into());
ops.append(&mut old.diff(val, path)) ops.append(&mut old.diff(val, path))
} }
} }
@ -36,7 +36,7 @@ impl Value {
(Value::Array(a), Value::Array(b)) if a != b => { (Value::Array(a), Value::Array(b)) if a != b => {
let mut n = 0; let mut n = 0;
while n < min(a.len(), b.len()) { while n < min(a.len(), b.len()) {
let path = path.add(n.into()); let path = path.clone().add(n.into());
ops.append(&mut a.value[n].diff(&b.value[n], path)); ops.append(&mut a.value[n].diff(&b.value[n], path));
n += 1; n += 1;
} }
@ -44,7 +44,7 @@ impl Value {
if n >= a.len() { if n >= a.len() {
ops.push(Operation { ops.push(Operation {
op: Op::Add, op: Op::Add,
path: path.add(n.into()), path: path.clone().add(n.into()),
value: b.value[n].clone(), value: b.value[n].clone(),
}) })
} }
@ -54,7 +54,7 @@ impl Value {
if n >= b.len() { if n >= b.len() {
ops.push(Operation { ops.push(Operation {
op: Op::Remove, op: Op::Remove,
path: path.add(n.into()), path: path.clone().add(n.into()),
value: Value::Null, value: Value::Null,
}) })
} }

View file

@ -26,7 +26,7 @@ impl Value {
.value .value
.iter() .iter()
.enumerate() .enumerate()
.flat_map(|(i, v)| v._each(path.next(), prev.add(Part::from(i)))) .flat_map(|(i, v)| v._each(path.next(), prev.clone().add(Part::from(i))))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
Part::First => match v.value.first() { Part::First => match v.value.first() {
Some(v) => v._each(path.next(), prev.add(p.clone())), Some(v) => v._each(path.next(), prev.add(p.clone())),
@ -44,7 +44,7 @@ impl Value {
.value .value
.iter() .iter()
.enumerate() .enumerate()
.flat_map(|(i, v)| v._each(path.next(), prev.add(Part::from(i)))) .flat_map(|(i, v)| v._each(path.next(), prev.clone().add(Part::from(i))))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
}, },
// Ignore everything else // Ignore everything else

View file

@ -12,14 +12,14 @@ impl Value {
Value::Object(v) => v Value::Object(v) => v
.value .value
.iter() .iter()
.flat_map(|(k, v)| v._every(prev.add(Part::from(k)))) .flat_map(|(k, v)| v._every(prev.clone().add(Part::from(k))))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
// Current path part is an array // Current path part is an array
Value::Array(v) => v Value::Array(v) => v
.value .value
.iter() .iter()
.enumerate() .enumerate()
.flat_map(|(i, v)| v._every(prev.add(Part::from(i)))) .flat_map(|(i, v)| v._every(prev.clone().add(Part::from(i))))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
// Process everything else // Process everything else
_ => vec![prev], _ => vec![prev],