From ef6d53521f07caa9c524749116d5fe53e1e8408d Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sat, 16 May 2020 18:06:23 +0200 Subject: Shrink ra_parser::Event from 32 bytes to 16 bytes This boxes the Error variant with the assumption that it is rarely constructed --- crates/ra_parser/src/lib.rs | 2 +- crates/ra_parser/src/parser.rs | 2 +- crates/ra_syntax/src/syntax_node.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index e08ad4dae..eeb8ad66b 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs @@ -25,7 +25,7 @@ pub(crate) use token_set::TokenSet; pub use syntax_kind::SyntaxKind; #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ParseError(pub String); +pub struct ParseError(pub Box); /// `TokenSource` abstracts the source of the tokens parser operates on. /// diff --git a/crates/ra_parser/src/parser.rs b/crates/ra_parser/src/parser.rs index faa63d53f..4f59b0a23 100644 --- a/crates/ra_parser/src/parser.rs +++ b/crates/ra_parser/src/parser.rs @@ -192,7 +192,7 @@ impl<'t> Parser<'t> { /// structured errors with spans and notes, like rustc /// does. pub(crate) fn error>(&mut self, message: T) { - let msg = ParseError(message.into()); + let msg = ParseError(Box::new(message.into())); self.push_event(Event::Error { msg }) } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index f9d379abf..e566af7e8 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -70,6 +70,6 @@ impl SyntaxTreeBuilder { } pub fn error(&mut self, error: ra_parser::ParseError, text_pos: TextSize) { - self.errors.push(SyntaxError::new_at_offset(error.0, text_pos)) + self.errors.push(SyntaxError::new_at_offset(*error.0, text_pos)) } } -- cgit v1.2.3 From b606399095cc5357a93f40fb0d695eab8530bc98 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sat, 16 May 2020 18:24:17 +0200 Subject: Reduce reallocations in ra_tt::buffer::TokenBuffer::new_inner --- crates/ra_tt/src/buffer.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_tt/src/buffer.rs b/crates/ra_tt/src/buffer.rs index 14b3f707d..5967f44cd 100644 --- a/crates/ra_tt/src/buffer.rs +++ b/crates/ra_tt/src/buffer.rs @@ -42,7 +42,9 @@ impl<'t> TokenBuffer<'t> { buffers: &mut Vec]>>, next: Option, ) -> usize { - let mut entries = vec![]; + // Must contain everything in tokens and then the Entry::End + let start_capacity = tokens.len() + 1; + let mut entries = Vec::with_capacity(start_capacity); let mut children = vec![]; for (idx, tt) in tokens.iter().enumerate() { -- cgit v1.2.3 From 1e9172d70cf714dc006c1cbc4a029ae6e848d35b Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sat, 16 May 2020 21:56:08 +0200 Subject: Reuse Vec allocations --- crates/ra_mbe/src/mbe_expander/transcriber.rs | 33 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index 4b173edd3..7c9bb4d00 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs @@ -1,4 +1,4 @@ -//! Transcraber takes a template, like `fn $ident() {}`, a set of bindings like +//! Transcriber takes a template, like `fn $ident() {}`, a set of bindings like //! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}` use ra_syntax::SmolStr; @@ -53,7 +53,8 @@ impl Bindings { pub(super) fn transcribe(template: &tt::Subtree, bindings: &Bindings) -> ExpandResult { assert!(template.delimiter == None); let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new() }; - expand_subtree(&mut ctx, template) + let mut arena: Vec = Vec::new(); + expand_subtree(&mut ctx, template, &mut arena) } #[derive(Debug)] @@ -73,8 +74,13 @@ struct ExpandCtx<'a> { nesting: Vec, } -fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> ExpandResult { - let mut buf: Vec = Vec::new(); +fn expand_subtree( + ctx: &mut ExpandCtx, + template: &tt::Subtree, + arena: &mut Vec, +) -> ExpandResult { + // remember how many elements are in the arena now - when returning, we want to drain exactly how many elements we added. This way, the recursive uses of the arena get their own "view" of the arena, but will reuse the allocation + let start_elements = arena.len(); let mut err = None; for op in parse_template(template) { let op = match op { @@ -85,25 +91,27 @@ fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> ExpandResult buf.push(tt.clone()), + Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()), Op::TokenTree(tt::TokenTree::Subtree(tt)) => { - let ExpandResult(tt, e) = expand_subtree(ctx, tt); + let ExpandResult(tt, e) = expand_subtree(ctx, tt, arena); err = err.or(e); - buf.push(tt.into()); + arena.push(tt.into()); } Op::Var { name, kind: _ } => { let ExpandResult(fragment, e) = expand_var(ctx, name); err = err.or(e); - push_fragment(&mut buf, fragment); + push_fragment(arena, fragment); } Op::Repeat { subtree, kind, separator } => { - let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator); + let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator, arena); err = err.or(e); - push_fragment(&mut buf, fragment) + push_fragment(arena, fragment) } } } - ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: buf }, err) + // drain the elements added in this instance of expand_subtree + let tts = arena.drain(start_elements..arena.len()).collect(); + ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err) } fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult { @@ -155,6 +163,7 @@ fn expand_repeat( template: &tt::Subtree, kind: RepeatKind, separator: Option, + arena: &mut Vec, ) -> ExpandResult { let mut buf: Vec = Vec::new(); ctx.nesting.push(NestingState { idx: 0, at_end: false, hit: false }); @@ -165,7 +174,7 @@ fn expand_repeat( let mut counter = 0; loop { - let ExpandResult(mut t, e) = expand_subtree(ctx, template); + let ExpandResult(mut t, e) = expand_subtree(ctx, template, arena); let nesting_state = ctx.nesting.last_mut().unwrap(); if nesting_state.at_end || !nesting_state.hit { break; -- cgit v1.2.3