Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimize fn_append_internal #43

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use frame::Frame;
use functions::*;
use value::{ArrayFlags, Value};

use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;
use std::cell::RefCell;
use std::collections::{hash_map, HashMap};
Expand Down Expand Up @@ -124,7 +125,7 @@ impl<'a> Evaluator<'a> {
is_partial,
..
} => self.evaluate_function(input, proc, args, is_partial, frame, None)?,
AstKind::Wildcard => self.evaluate_wildcard(node, input, frame)?,
AstKind::Wildcard => self.evaluate_wildcard(input)?,
AstKind::Descendent => self.evaluate_descendants(input)?,
AstKind::Transform {
ref pattern,
Expand Down Expand Up @@ -230,27 +231,22 @@ impl<'a> Evaluator<'a> {
}
}
UnaryOp::ArrayConstructor(ref array) => {
let mut result = Value::array(
self.arena,
if node.cons_array {
ArrayFlags::CONS
} else {
ArrayFlags::empty()
},
);
for item in array.iter() {
let mut values = BumpVec::new_in(self.arena);
for item in array {
let value = self.evaluate(item, input, frame)?;
if let AstKind::Unary(UnaryOp::ArrayConstructor(..)) = item.kind {
result.push(value);
values.push(value);
} else {
result = fn_append_internal(
self.fn_context("append", node.char_index, input, frame),
result,
value,
);
fn_append_internal(&mut values, value);
}
}
Ok(result)

let flags = if node.cons_array {
ArrayFlags::CONS
} else {
ArrayFlags::empty()
};
Ok(Value::array_from(self.arena, values, flags))
}
UnaryOp::ObjectConstructor(ref object) => {
self.evaluate_group_expression(node.char_index, object, input, frame)
Expand Down Expand Up @@ -1103,13 +1099,8 @@ impl<'a> Evaluator<'a> {
Ok(result)
}

fn evaluate_wildcard(
&self,
node: &Ast,
input: &'a Value<'a>,
frame: &Frame<'a>,
) -> Result<&'a Value<'a>> {
let mut result = Value::array(self.arena, ArrayFlags::SEQUENCE);
fn evaluate_wildcard(&self, input: &'a Value<'a>) -> Result<&'a Value<'a>> {
let mut values = BumpVec::new_in(self.arena);

let input = if input.is_array() && input.has_flags(ArrayFlags::WRAPPED) && !input.is_empty()
{
Expand All @@ -1122,18 +1113,14 @@ impl<'a> Evaluator<'a> {
for (_key, value) in input.entries() {
if value.is_array() {
let value = value.flatten(self.arena);
result = fn_append_internal(
self.fn_context("append", node.char_index, input, frame),
result,
value,
);
fn_append_internal(&mut values, value);
} else {
result.push(value)
values.push(value)
}
}
}

Ok(result)
Ok(Value::array_from(self.arena, values, ArrayFlags::SEQUENCE))
}

fn evaluate_descendants(&self, input: &'a Value<'a>) -> Result<&'a Value<'a>> {
Expand Down
45 changes: 12 additions & 33 deletions src/evaluator/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use base64::Engine;
use std::borrow::Borrow;

use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;

use crate::{Error, Result};
Expand Down Expand Up @@ -86,42 +87,20 @@ impl<'a, 'e> FunctionContext<'a, 'e> {
}
}

// Version of append that takes a mutable arg1 - this could probably be collapsed
pub fn fn_append_internal<'a>(
context: FunctionContext<'a, '_>,
arg1: &'a mut Value<'a>,
arg2: &'a Value<'a>,
) -> &'a mut Value<'a> {
if arg2.is_undefined() {
return arg1;
}

let arg1_len = if arg1.is_array() { arg1.len() } else { 1 };
let arg2_len = if arg2.is_array() { arg2.len() } else { 1 };

let result = Value::array_with_capacity(
context.arena,
arg1_len + arg2_len,
if arg1.is_array() {
arg1.get_flags()
} else {
ArrayFlags::SEQUENCE
},
);

if arg1.is_array() {
arg1.members().for_each(|m| result.push(m));
} else {
result.push(&*arg1);
/// Extend the given values with value.
///
/// If the value is a single value then, append the value as is.
/// If the value is an array, extends values with the value's members.
pub fn fn_append_internal<'a>(values: &mut BumpVec<&'a Value<'a>>, value: &'a Value<'a>) {
if value.is_undefined() {
return;
}

if arg2.is_array() {
arg2.members().for_each(|m| result.push(m));
} else {
result.push(arg2);
match value {
Value::Array(a, _) => values.extend_from_slice(a),
Value::Range(_) => values.extend(value.members()),
_ => values.push(value),
}

result
}

pub fn fn_append<'a>(
Expand Down
35 changes: 14 additions & 21 deletions src/evaluator/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::borrow::Cow;
use bitflags::bitflags;
use bumpalo::boxed::Box;
use bumpalo::collections::String as BumpString;
use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::HashMap;
Expand Down Expand Up @@ -45,9 +46,9 @@ pub enum Value<'a> {
Null,
Number(f64),
Bool(bool),
String(bumpalo::collections::String<'a>),
Array(bumpalo::collections::Vec<'a, &'a Value<'a>>, ArrayFlags),
Object(HashMap<bumpalo::collections::String<'a>, &'a Value<'a>, DefaultHashBuilder, &'a Bump>),
String(BumpString<'a>),
Array(BumpVec<'a, &'a Value<'a>>, ArrayFlags),
Object(HashMap<BumpString<'a>, &'a Value<'a>, DefaultHashBuilder, &'a Bump>),
Range(Range<'a>),
Lambda {
ast: Box<'a, Ast>,
Expand Down Expand Up @@ -87,31 +88,25 @@ impl<'a> Value<'a> {
}

pub fn string(arena: &'a Bump, value: &str) -> &'a mut Value<'a> {
arena.alloc(Value::String(bumpalo::collections::String::from_str_in(
value, arena,
)))
arena.alloc(Value::String(BumpString::from_str_in(value, arena)))
}

pub fn array(arena: &Bump, flags: ArrayFlags) -> &mut Value {
let v = bumpalo::collections::Vec::new_in(arena);
let v = BumpVec::new_in(arena);
arena.alloc(Value::Array(v, flags))
}

pub fn array_from(
arr: &'a [&Value<'a>],
arena: &'a Bump,
arr: BumpVec<'a, &'a Value<'a>>,
flags: ArrayFlags,
) -> &'a mut Value<'a> {
let result = Value::array_with_capacity(arena, arr.len(), flags);
if let Value::Array(a, _) = result {
a.extend(arr);
}
result
arena.alloc(Value::Array(arr, flags))
}

pub fn array_with_capacity(arena: &Bump, capacity: usize, flags: ArrayFlags) -> &mut Value {
arena.alloc(Value::Array(
bumpalo::collections::Vec::with_capacity_in(capacity, arena),
BumpVec::with_capacity_in(capacity, arena),
flags,
))
}
Expand Down Expand Up @@ -328,9 +323,7 @@ impl<'a> Value<'a> {
}
}

pub fn entries(
&self,
) -> hashbrown::hash_map::Iter<'_, bumpalo::collections::String<'a>, &'a Value> {
pub fn entries(&self) -> hashbrown::hash_map::Iter<'_, BumpString<'a>, &'a Value> {
match self {
Value::Object(map) => map.iter(),
_ => panic!("Not an object"),
Expand Down Expand Up @@ -505,10 +498,10 @@ impl<'a> Value<'a> {
Self::Number(n) => Value::number(arena, *n),
Self::Bool(b) => Value::bool(arena, *b),
// TODO: clean up
Self::String(s) => arena.alloc(Value::String(
bumpalo::collections::String::from_str_in(s.as_str(), arena),
)),
Self::Array(a, f) => Value::array_from(a, arena, *f),
Self::String(s) => {
arena.alloc(Value::String(BumpString::from_str_in(s.as_str(), arena)))
}
Self::Array(a, f) => Value::array_from(arena, a.clone(), *f),
Self::Object(o) => Value::object_from(o, arena),
Self::Lambda { ast, input, frame } => Value::lambda(arena, ast, input, frame.clone()),
Self::NativeFn { name, arity, func } => Value::nativefn(arena, name, *arity, *func),
Expand Down