Skip to content

Commit

Permalink
perf(parser): Optimize for empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 30, 2024
1 parent 5a9ad6f commit e273be6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/toml_edit/src/parser/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use winnow::combinator::cut_err;
use winnow::combinator::delimited;
use winnow::combinator::opt;
use winnow::combinator::peek;
use winnow::combinator::separated;
use winnow::combinator::trace;

Expand Down Expand Up @@ -39,6 +40,11 @@ const ARRAY_SEP: u8 = b',';
// array-values = ws-comment-newline val ws-comment-newline array-sep array-values
// array-values =/ ws-comment-newline val ws-comment-newline [ array-sep ]
pub(crate) fn array_values(input: &mut Input<'_>) -> PResult<Array> {
if peek(opt(ARRAY_CLOSE)).parse_next(input)?.is_some() {
// Optimize for empty arrays, avoiding `value` from being expected to fail
return Ok(Array::new());
}

let array = separated(0.., array_value, ARRAY_SEP).parse_next(input)?;
let mut array = Array::with_vec(array);
if !array.is_empty() {
Expand Down

0 comments on commit e273be6

Please sign in to comment.