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

Improve performance of seq/map peeks #1206

Merged
merged 3 commits into from
Oct 19, 2024
Merged
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
74 changes: 35 additions & 39 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1930,30 +1930,26 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
seq: &mut SeqAccess<'a, R>,
) -> Result<bool> {
let peek = match tri!(seq.de.parse_whitespace()) {
Some(b']') => {
return Ok(false);
}
Some(b',') if !seq.first => {
seq.de.eat_char();
tri!(seq.de.parse_whitespace())
}
Some(b) => {
if seq.first {
seq.first = false;
Some(b)
} else {
return Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
}
}
Some(b) => b,
None => {
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
}
};

match peek {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
if peek == b']' {
Ok(false)
} else if seq.first {
seq.first = false;
Ok(true)
} else if peek == b',' {
seq.de.eat_char();
match tri!(seq.de.parse_whitespace()) {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
} else {
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
}
}

Expand Down Expand Up @@ -1985,31 +1981,31 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
{
fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
let peek = match tri!(map.de.parse_whitespace()) {
Some(b'}') => {
return Ok(false);
}
Some(b',') if !map.first => {
map.de.eat_char();
tri!(map.de.parse_whitespace())
}
Some(b) => {
if map.first {
map.first = false;
Some(b)
} else {
return Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
}
}
Some(b) => b,
None => {
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};

match peek {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
if peek == b'}' {
Ok(false)
} else if map.first {
map.first = false;
if peek == b'"' {
Ok(true)
} else {
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
}
} else if peek == b',' {
map.de.eat_char();
match tri!(map.de.parse_whitespace()) {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
} else {
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
}
}

Expand Down