Skip to content

Commit 1227fdd

Browse files
committed
Reduce cloning of tokens
- Avoid cloning whitespace tokens in `peek_nth_token()` by using a &Token from `tokens.get()` instead of a cloned `Token` from `token_at()` - Similarly avoid cloning in `next_token_no_skip`, and clone the non-whitespace tokens in `next_token` instead. - Remove `token_at`, which was only used in `peek_token` and `peek_nth_token` - Fold `prev_token_no_skip()` into `prev_token()` and make `prev_token` return nothing, as the return value isn't used anyway.
1 parent ebb82b8 commit 1227fdd

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

src/sqlparser.rs

+16-41
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,13 @@ impl Parser {
567567
pub fn peek_nth_token(&self, mut n: usize) -> Option<Token> {
568568
let mut index = self.index;
569569
loop {
570-
match self.token_at(index) {
570+
match self.tokens.get(index) {
571571
Some(Token::Whitespace(_)) => {
572572
index += 1;
573573
}
574574
Some(token) => {
575575
if n == 0 {
576-
return Some(token);
576+
return Some(token.clone());
577577
}
578578
index += 1;
579579
n -= 1;
@@ -589,56 +589,32 @@ impl Parser {
589589
pub fn next_token(&mut self) -> Option<Token> {
590590
loop {
591591
match self.next_token_no_skip() {
592-
Some(Token::Whitespace(_)) => {
593-
continue;
594-
}
595-
token => {
596-
return token;
597-
}
592+
Some(Token::Whitespace(_)) => continue,
593+
token => return token.cloned(),
598594
}
599595
}
600596
}
601597

602-
/// see the token at this index
603-
fn token_at(&self, n: usize) -> Option<Token> {
604-
if let Some(token) = self.tokens.get(n) {
605-
Some(token.clone())
606-
} else {
607-
None
608-
}
609-
}
610-
611-
pub fn next_token_no_skip(&mut self) -> Option<Token> {
598+
pub fn next_token_no_skip(&mut self) -> Option<&Token> {
612599
if self.index < self.tokens.len() {
613600
self.index += 1;
614-
Some(self.tokens[self.index - 1].clone())
601+
Some(&self.tokens[self.index - 1])
615602
} else {
616603
None
617604
}
618605
}
619606

620607
/// Push back the last one non-whitespace token
621-
pub fn prev_token(&mut self) -> Option<Token> {
622-
// TODO: returned value is unused (available via peek_token)
608+
pub fn prev_token(&mut self) {
623609
loop {
624-
match self.prev_token_no_skip() {
625-
Some(Token::Whitespace(_)) => {
610+
assert!(self.index > 0);
611+
if self.index > 0 {
612+
self.index -= 1;
613+
if let Token::Whitespace(_) = &self.tokens[self.index] {
626614
continue;
627615
}
628-
token => {
629-
return token;
630-
}
631-
}
632-
}
633-
}
634-
635-
/// Get the previous token and decrement the token index
636-
fn prev_token_no_skip(&mut self) -> Option<Token> {
637-
if self.index > 0 {
638-
self.index -= 1;
639-
Some(self.tokens[self.index].clone())
640-
} else {
641-
None
616+
};
617+
return;
642618
}
643619
}
644620

@@ -1776,13 +1752,12 @@ mod tests {
17761752
fn test_prev_index() {
17771753
let sql = "SELECT version()";
17781754
all_dialects().run_parser_method(sql, |parser| {
1779-
assert_eq!(parser.prev_token(), None);
17801755
assert_eq!(parser.next_token(), Some(Token::make_keyword("SELECT")));
17811756
assert_eq!(parser.next_token(), Some(Token::make_word("version", None)));
1782-
assert_eq!(parser.prev_token(), Some(Token::make_word("version", None)));
1757+
parser.prev_token();
17831758
assert_eq!(parser.peek_token(), Some(Token::make_word("version", None)));
1784-
assert_eq!(parser.prev_token(), Some(Token::make_keyword("SELECT")));
1785-
assert_eq!(parser.prev_token(), None);
1759+
parser.prev_token();
1760+
assert_eq!(parser.peek_token(), Some(Token::make_keyword("SELECT")));
17861761
});
17871762
}
17881763
}

0 commit comments

Comments
 (0)