-
Notifications
You must be signed in to change notification settings - Fork 4
⚡ Bolt: [performance improvement] Avoid String heap allocation in DelimTokenType #53
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
base: master
Are you sure you want to change the base?
Changes from all commits
43dd710
d559e0c
1fb75d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,16 +52,17 @@ | |
| } | ||
|
|
||
| impl DelimTokenType { | ||
| pub fn string(&self) -> String { | ||
| #[cfg(not(tarpaulin_include))] | ||
| pub fn as_str(&self) -> &'static str { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| use DelimTokenType::*; | ||
| match self { | ||
| OpenParen => "(".to_string(), | ||
| CloseParen => ")".to_string(), | ||
| OpenBracket => "[".to_string(), | ||
| CloseBracket => "]".to_string(), | ||
| OpenBrace => "{".to_string(), | ||
| CloseBrace => "}".to_string(), | ||
| Unknown => "??".to_string(), | ||
| OpenParen => "(", | ||
| CloseParen => ")", | ||
|
Comment on lines
+56
to
+60
|
||
| OpenBracket => "[", | ||
| CloseBracket => "]", | ||
| OpenBrace => "{", | ||
| CloseBrace => "}", | ||
| Unknown => "??", | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -86,7 +87,7 @@ | |
| pub fn check_op(token: Token, expected: &str) -> bool { | ||
| match token { | ||
| Token::Delim(op, _) => { | ||
| if op.string() == expected { | ||
| if op.as_str() == expected { | ||
| return true; | ||
| } | ||
| } | ||
|
Comment on lines
89
to
93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -187,7 +188,7 @@ | |
| Reference(val, _) => val.to_string(), | ||
| Function(val, _) => val.to_string(), | ||
| Semicolon(val, _) => val.to_string(), | ||
| Delim(ty, _) => ty.string(), | ||
| Delim(ty, _) => ty.as_str().to_string(), | ||
| EOF => "EOF".to_string(), | ||
| } | ||
| } | ||
|
|
@@ -213,7 +214,7 @@ | |
| Function(val, span) => write!(f, "Function Token: {}, {}", val, span), | ||
| String(val, span) => write!(f, "String Token: {}, {}", val, span), | ||
| Semicolon(val, span) => write!(f, "Semicolon Token: {}, {}", val, span), | ||
| Delim(ty, span) => write!(f, "Delim Token: {}, {}", ty.string(), span), | ||
| Delim(ty, span) => write!(f, "Delim Token: {}, {}", ty.as_str(), span), | ||
| EOF => write!(f, "EOF"), | ||
| } | ||
| } | ||
|
|
@@ -224,6 +225,18 @@ | |
| use super::{DelimTokenType, Span, Token}; | ||
| use rstest::rstest; | ||
|
|
||
| #[rstest] | ||
| #[case(DelimTokenType::OpenParen, "(")] | ||
| #[case(DelimTokenType::CloseParen, ")")] | ||
| #[case(DelimTokenType::OpenBracket, "[")] | ||
| #[case(DelimTokenType::CloseBracket, "]")] | ||
| #[case(DelimTokenType::OpenBrace, "{")] | ||
| #[case(DelimTokenType::CloseBrace, "}")] | ||
| #[case(DelimTokenType::Unknown, "??")] | ||
| fn test_delim_token_type_as_str(#[case] input: DelimTokenType, #[case] output: &str) { | ||
| assert_eq!(input.as_str(), output) | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case("(", DelimTokenType::OpenParen)] | ||
| #[case(")", DelimTokenType::CloseParen)] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,7 +145,7 @@ impl<'a> Tokenizer<'a> { | |
| self.next()?; | ||
| match token { | ||
| Token::Delim(bracket, _) => { | ||
| if bracket.string() == op { | ||
| if bracket.as_str() == op { | ||
| return Ok(()); | ||
| } | ||
| } | ||
|
Comment on lines
147
to
151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a logic bug here: if the token is a Token::Delim(bracket, _) if bracket.as_str() == op => return Ok(()), |
||
|
|
||
Check warning
Code scanning / clippy
unexpected cfg condition name: tarpaulin_include Warning