Skip to content

Commit

Permalink
try to avoid lots of mem moves
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Sep 10, 2024
1 parent 0b0350b commit ebd407e
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions crates/jiter/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,38 +307,40 @@ fn take_value_recursive<'j, 's>(

// now try to advance position in the current array or object
peek = loop {
current_recursion = match current_recursion {
RecursedValue::Array(mut array) => {
array.push(value);
match &mut current_recursion {
RecursedValue::Array(array) => {
if let Some(next_peek) = parser.array_step()? {
array.push(value);
// array continuing
current_recursion = RecursedValue::Array(array);
break next_peek;
} else if let Some(recursed) = recursion_stack.pop() {
// array finished, recursing
value = JsonValue::Array(Arc::new(array));
recursed
} else {
// no recursion left and array finished
return Ok(JsonValue::Array(Arc::new(array)));
}
}
RecursedValue::Object { mut partial, next_key } => {
partial.insert(next_key, value);
if let Some(next_key) = parser.object_step::<StringDecoder>(tape)?.map(create_cow) {
RecursedValue::Object { partial, next_key } => {
if let Some(yet_another_key) = parser.object_step::<StringDecoder>(tape)?.map(create_cow) {
partial.insert(std::mem::replace(next_key, yet_another_key), value);
// object continuing
current_recursion = RecursedValue::Object { partial, next_key };
break parser.peek()?;
} else if let Some(recursed) = recursion_stack.pop() {
// object finished, recursing
value = JsonValue::Object(Arc::new(LazyIndexMap::new()));
recursed
} else {
// no recursion left and object finished
return Ok(JsonValue::Object(Arc::new(partial)));
}
}
}

value = match current_recursion {
RecursedValue::Array(mut array) => {
array.push(value);
JsonValue::Array(Arc::new(array))
}
RecursedValue::Object { mut partial, next_key } => {
partial.insert(next_key, value);
JsonValue::Object(Arc::new(partial))
}
};

if let Some(r) = recursion_stack.pop() {
// value is the current array or object, next turn of the loop will insert it to the parent
current_recursion = r;
} else {
return Ok(value);
}
};
}
}
Expand Down

0 comments on commit ebd407e

Please sign in to comment.