Skip to content

Instantly share code, notes, and snippets.

@eliben

eliben/a.patch Secret

Forked from utkarshkukreti/a.patch
Created May 31, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliben/a6a2a55a33e733e3104827ab03ebc720 to your computer and use it in GitHub Desktop.
Save eliben/a6a2a55a33e733e3104827ab03ebc720 to your computer and use it in GitHub Desktop.
eliben/code-for-blog:optimize-2022-rust-tablegen-lexer
commit 3fdd4fa036d925e1d2bba15f23db0790512729cf
Author: Utkarsh Kukreti <utkarshkukreti@gmail.com>
Date: Tue May 31 01:01:16 2022 +0530
optimize lexer/slice_all_push: ~4.2ms -> ~3.5ms (-18%)
`CharIndices` implements a cheap `Clone` operation which is much faster
than wrapping the whole iterator in `Peekable`, especially when the
`peek()` operation is rarely used.
diff --git a/2022/rust-tablegen-lexer/src/slice.rs b/2022/rust-tablegen-lexer/src/slice.rs
index f87e3c4..6069b2d 100644
--- a/2022/rust-tablegen-lexer/src/slice.rs
+++ b/2022/rust-tablegen-lexer/src/slice.rs
@@ -55,7 +55,7 @@ pub struct Token<'source> {
// the Iterator trait for a higher-level iterator API.
pub struct Lexer<'source> {
input: &'source str,
- iter: Peekable<CharIndices<'source>>,
+ iter: CharIndices<'source>,
// c is the last char taken from iter, and ci is its offset in the input.
c: char,
@@ -66,13 +66,12 @@ pub struct Lexer<'source> {
}
use core::str::CharIndices;
-use std::iter::Peekable;
impl<'source> Lexer<'source> {
pub fn new(input: &'source str) -> Self {
let mut lex = Self {
input,
- iter: input.char_indices().peekable(),
+ iter: input.char_indices(),
c: '\x00',
ci: 0,
error: false,
@@ -104,8 +103,8 @@ impl<'source> Lexer<'source> {
'-' => TokenValue::Minus,
'*' => TokenValue::Multiply,
'/' => {
- if let Some((_, chr)) = self.iter.peek() {
- if *chr == '/' {
+ if let Some((_, chr)) = self.iter.clone().next() {
+ if chr == '/' {
return self.scan_comment();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment