Skip to content

Commit 0e94d07

Browse files
bors[bot]lnicola
andcommitted
Merge #1638
1638: Avoid cloning a TtToken in SubtreeTokenSource::mk_token r=matklad a=lnicola According to `perf record`, this function is the hottest one while running `ra_cli analysis-stats`: Before: ``` 6.05% ra_cli ra_cli <ra_mbe::subtree_source::SubtreeTokenSource as ra_parser::TokenSource>::lookahead_nth 5.56% ra_cli ra_cli <rowan::green::GreenNode as core::hash::Hash>::hash 4.16% ra_cli libc-2.29.so _int_malloc 3.93% ra_cli ra_cli ra_mbe::subtree_source::SubtreeTokenSource::get Database loaded, 255 roots, 231.676882ms Crates in this dir: 27 Total modules found: 282 Total declarations: 9642 Total functions: 3287 Total expressions: 64616 Expressions of unknown type: 9111 (14%) Expressions of partially unknown type: 3417 (5%) Analysis: 24.012797561s, 0b allocated 0b resident target/release/ra_cli analysis-stats 24.60s user 0.56s system 100% cpu 25.125 total ``` After: ``` 5.85% ra_cli ra_cli <rowan::green::GreenNode as core::hash::Hash>::hash 4.55% ra_cli libc-2.29.so _int_malloc 4.31% ra_cli ra_cli ra_parser::parser::Parser::nth 3.61% ra_cli ra_cli <ra_syntax::parsing::text_token_source::TextTokenSource as ra_parser::TokenSource>::lookahead_nth 3.54% ra_cli ra_cli ra_syntax::syntax_node::SyntaxTreeBuilder::finish_node 3.46% ra_cli libc-2.29.so _int_free 3.12% ra_cli libc-2.29.so malloc 2.76% ra_cli ra_cli ra_parser::event::process 2.68% ra_cli ra_cli alloc::sync::Arc<T>::drop_slow 2.50% ra_cli ra_cli ra_mbe::subtree_source::SubtreeTokenSource::get 2.31% ra_cli ra_cli <smol_str::SmolStr as core::hash::Hash>::hash 2.04% ra_cli libc-2.29.so __memmove_avx_unaligned_erms 1.92% ra_cli ra_cli <ra_mbe::subtree_source::SubtreeTokenSource as ra_parser::TokenSource>::lookahead_nth Database loaded, 255 roots, 236.176803ms Crates in this dir: 27 Total modules found: 282 Total declarations: 9642 Total functions: 3287 Total expressions: 64620 Expressions of unknown type: 9107 (14%) Expressions of partially unknown type: 3425 (5%) Analysis: 22.562328486s, 0b allocated 0b resident target/release/ra_cli analysis-stats 23.12s user 0.57s system 100% cpu 23.659 total ``` r? @edwin0cheng, @matklad Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents c5bdd02 + f524373 commit 0e94d07

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

crates/ra_mbe/src/subtree_source.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ra_parser::{Token, TokenSource};
22
use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T};
3-
use std::cell::{Cell, RefCell};
3+
use std::cell::{Cell, Ref, RefCell};
44
use tt::buffer::{Cursor, TokenBuffer};
55

66
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -20,8 +20,8 @@ impl<'a> SubtreeTokenSource<'a> {
2020
// Helper function used in test
2121
#[cfg(test)]
2222
pub fn text(&self) -> SmolStr {
23-
match self.get(self.curr.1) {
24-
Some(tt) => tt.text,
23+
match *self.get(self.curr.1) {
24+
Some(ref tt) => tt.text.clone(),
2525
_ => SmolStr::new(""),
2626
}
2727
}
@@ -41,44 +41,46 @@ impl<'a> SubtreeTokenSource<'a> {
4141
}
4242

4343
fn mk_token(&self, pos: usize) -> Token {
44-
match self.get(pos) {
45-
Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next },
44+
match *self.get(pos) {
45+
Some(ref tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next },
4646
None => Token { kind: EOF, is_jointed_to_next: false },
4747
}
4848
}
4949

50-
fn get(&self, pos: usize) -> Option<TtToken> {
51-
let mut cached = self.cached.borrow_mut();
52-
if pos < cached.len() {
53-
return cached[pos].clone();
50+
fn get(&self, pos: usize) -> Ref<Option<TtToken>> {
51+
if pos < self.cached.borrow().len() {
52+
return Ref::map(self.cached.borrow(), |c| &c[pos]);
5453
}
5554

56-
while pos >= cached.len() {
57-
let cursor = self.cached_cursor.get();
58-
if cursor.eof() {
59-
cached.push(None);
60-
continue;
61-
}
62-
63-
match cursor.token_tree() {
64-
Some(tt::TokenTree::Leaf(leaf)) => {
65-
cached.push(Some(convert_leaf(&leaf)));
66-
self.cached_cursor.set(cursor.bump());
67-
}
68-
Some(tt::TokenTree::Subtree(subtree)) => {
69-
self.cached_cursor.set(cursor.subtree().unwrap());
70-
cached.push(Some(convert_delim(subtree.delimiter, false)));
55+
{
56+
let mut cached = self.cached.borrow_mut();
57+
while pos >= cached.len() {
58+
let cursor = self.cached_cursor.get();
59+
if cursor.eof() {
60+
cached.push(None);
61+
continue;
7162
}
72-
None => {
73-
if let Some(subtree) = cursor.end() {
74-
cached.push(Some(convert_delim(subtree.delimiter, true)));
63+
64+
match cursor.token_tree() {
65+
Some(tt::TokenTree::Leaf(leaf)) => {
66+
cached.push(Some(convert_leaf(&leaf)));
7567
self.cached_cursor.set(cursor.bump());
7668
}
69+
Some(tt::TokenTree::Subtree(subtree)) => {
70+
self.cached_cursor.set(cursor.subtree().unwrap());
71+
cached.push(Some(convert_delim(subtree.delimiter, false)));
72+
}
73+
None => {
74+
if let Some(subtree) = cursor.end() {
75+
cached.push(Some(convert_delim(subtree.delimiter, true)));
76+
self.cached_cursor.set(cursor.bump());
77+
}
78+
}
7779
}
7880
}
7981
}
8082

81-
cached[pos].clone()
83+
Ref::map(self.cached.borrow(), |c| &c[pos])
8284
}
8385
}
8486

@@ -103,8 +105,8 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
103105

104106
/// Is the current token a specified keyword?
105107
fn is_keyword(&self, kw: &str) -> bool {
106-
match self.get(self.curr.1) {
107-
Some(t) => t.text == *kw,
108+
match *self.get(self.curr.1) {
109+
Some(ref t) => t.text == *kw,
108110
_ => false,
109111
}
110112
}

0 commit comments

Comments
 (0)