From 12ceb375fcebc887cc22866dfa3e676410a5e674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 6 Sep 2023 13:53:19 +0200 Subject: [PATCH] parser: Use a lookup table for Delimiter::from_byte. It's faster. --- src/parser.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 9c91f523..f91e3954 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -314,15 +314,21 @@ impl Delimiters { #[inline] fn from_byte(byte: Option) -> Delimiters { + const TABLE: [Delimiters; 256] = { + let mut table = [Delimiter::None; 256]; + table[b';' as usize] = Delimiter::Semicolon; + table[b'!' as usize] = Delimiter::Bang; + table[b',' as usize] = Delimiter::Comma; + table[b'{' as usize] = Delimiter::CurlyBracketBlock; + table[b'}' as usize] = ClosingDelimiter::CloseCurlyBracket; + table[b']' as usize] = ClosingDelimiter::CloseSquareBracket; + table[b')' as usize] = ClosingDelimiter::CloseParenthesis; + table + }; + match byte { - Some(b';') => Delimiter::Semicolon, - Some(b'!') => Delimiter::Bang, - Some(b',') => Delimiter::Comma, - Some(b'{') => Delimiter::CurlyBracketBlock, - Some(b'}') => ClosingDelimiter::CloseCurlyBracket, - Some(b']') => ClosingDelimiter::CloseSquareBracket, - Some(b')') => ClosingDelimiter::CloseParenthesis, - _ => Delimiter::None, + None => Delimiter::None, + Some(b) => TABLE[b as usize], } } }