Skip to content

Commit f45b422

Browse files
authored
Merge pull request #1206 from dtolnay/hasnext
Improve performance of seq/map peeks
2 parents 4cb90ce + f2082d2 commit f45b422

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

src/de.rs

+35-39
Original file line numberDiff line numberDiff line change
@@ -1930,30 +1930,26 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
19301930
seq: &mut SeqAccess<'a, R>,
19311931
) -> Result<bool> {
19321932
let peek = match tri!(seq.de.parse_whitespace()) {
1933-
Some(b']') => {
1934-
return Ok(false);
1935-
}
1936-
Some(b',') if !seq.first => {
1937-
seq.de.eat_char();
1938-
tri!(seq.de.parse_whitespace())
1939-
}
1940-
Some(b) => {
1941-
if seq.first {
1942-
seq.first = false;
1943-
Some(b)
1944-
} else {
1945-
return Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
1946-
}
1947-
}
1933+
Some(b) => b,
19481934
None => {
19491935
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
19501936
}
19511937
};
19521938

1953-
match peek {
1954-
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
1955-
Some(_) => Ok(true),
1956-
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
1939+
if peek == b']' {
1940+
Ok(false)
1941+
} else if seq.first {
1942+
seq.first = false;
1943+
Ok(true)
1944+
} else if peek == b',' {
1945+
seq.de.eat_char();
1946+
match tri!(seq.de.parse_whitespace()) {
1947+
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
1948+
Some(_) => Ok(true),
1949+
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
1950+
}
1951+
} else {
1952+
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
19571953
}
19581954
}
19591955

@@ -1985,31 +1981,31 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
19851981
{
19861982
fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
19871983
let peek = match tri!(map.de.parse_whitespace()) {
1988-
Some(b'}') => {
1989-
return Ok(false);
1990-
}
1991-
Some(b',') if !map.first => {
1992-
map.de.eat_char();
1993-
tri!(map.de.parse_whitespace())
1994-
}
1995-
Some(b) => {
1996-
if map.first {
1997-
map.first = false;
1998-
Some(b)
1999-
} else {
2000-
return Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
2001-
}
2002-
}
1984+
Some(b) => b,
20031985
None => {
20041986
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
20051987
}
20061988
};
20071989

2008-
match peek {
2009-
Some(b'"') => Ok(true),
2010-
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
2011-
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
2012-
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
1990+
if peek == b'}' {
1991+
Ok(false)
1992+
} else if map.first {
1993+
map.first = false;
1994+
if peek == b'"' {
1995+
Ok(true)
1996+
} else {
1997+
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
1998+
}
1999+
} else if peek == b',' {
2000+
map.de.eat_char();
2001+
match tri!(map.de.parse_whitespace()) {
2002+
Some(b'"') => Ok(true),
2003+
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
2004+
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
2005+
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
2006+
}
2007+
} else {
2008+
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
20132009
}
20142010
}
20152011

0 commit comments

Comments
 (0)