From b2e6ca46ca2ad3352ef13154b401e798e9a7d752 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 27 Nov 2020 18:00:03 +0200 Subject: Profile completions better --- Cargo.lock | 1 + crates/assists/src/utils.rs | 1 + crates/assists/src/utils/insert_use.rs | 1 + crates/completion/src/item.rs | 2 ++ crates/completion/src/render/enum_variant.rs | 1 + crates/completion/src/render/function.rs | 1 + crates/completion/src/render/macro_.rs | 1 + crates/syntax/Cargo.toml | 1 + crates/syntax/src/algo.rs | 13 +++++++++++++ 9 files changed, 22 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 051d9e734..633b99758 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1658,6 +1658,7 @@ dependencies = [ "itertools", "once_cell", "parser", + "profile", "rayon", "rowan", "rustc-ap-rustc_lexer", diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 66c0cdd5f..048746587 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -25,6 +25,7 @@ use crate::{ pub use insert_use::{insert_use, ImportScope, MergeBehaviour}; pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path { + let _p = profile::span("mod_path_to_ast"); let mut segments = Vec::new(); let mut is_abs = false; match path.kind { diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index 423782a0e..975a08a20 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs @@ -95,6 +95,7 @@ pub fn insert_use<'a>( path: ast::Path, merge: Option, ) -> SyntaxRewriter<'a> { + let _p = profile::span("mod_path_to_ast"); let mut rewriter = SyntaxRewriter::default(); let use_item = make::use_(make::use_tree(path.clone(), None, None, false)); // merge into existing imports if possible diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index b13c3f376..7b62c2c4e 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs @@ -278,6 +278,8 @@ pub(crate) struct Builder { impl Builder { pub(crate) fn build(self) -> CompletionItem { + let _p = profile::span("item::Builder::build"); + let mut label = self.label; let mut lookup = self.lookup; let mut insert_text = self.insert_text; diff --git a/crates/completion/src/render/enum_variant.rs b/crates/completion/src/render/enum_variant.rs index 6070e9b1d..64e742b77 100644 --- a/crates/completion/src/render/enum_variant.rs +++ b/crates/completion/src/render/enum_variant.rs @@ -17,6 +17,7 @@ pub(crate) fn render_enum_variant<'a>( variant: hir::EnumVariant, path: Option, ) -> CompletionItem { + let _p = profile::span("render_enum_variant"); EnumVariantRender::new(ctx, local_name, variant, path).render(import_data) } diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 9dd5cd18c..e8b726ad6 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -15,6 +15,7 @@ pub(crate) fn render_fn<'a>( local_name: Option, fn_: hir::Function, ) -> CompletionItem { + let _p = profile::span("render_fn"); FunctionRender::new(ctx, local_name, fn_).render(import_data) } diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index fead59e41..91055a296 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -16,6 +16,7 @@ pub(crate) fn render_macro<'a>( name: String, macro_: hir::MacroDef, ) -> Option { + let _p = profile::span("render_macro"); MacroRender::new(ctx, name, macro_).render(import_data) } diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index 1fe907753..ce62babc3 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -28,6 +28,7 @@ stdx = { path = "../stdx", version = "0.0.0" } text_edit = { path = "../text_edit", version = "0.0.0" } parser = { path = "../parser", version = "0.0.0" } test_utils = { path = "../test_utils", version = "0.0.0" } +profile = { path = "../profile", version = "0.0.0" } [dev-dependencies] walkdir = "2.3.1" diff --git a/crates/syntax/src/algo.rs b/crates/syntax/src/algo.rs index 320c430c9..ee89d9867 100644 --- a/crates/syntax/src/algo.rs +++ b/crates/syntax/src/algo.rs @@ -127,6 +127,8 @@ pub struct TreeDiff { impl TreeDiff { pub fn into_text_edit(&self, builder: &mut TextEditBuilder) { + let _p = profile::span("into_text_edit"); + for (anchor, to) in self.insertions.iter() { let offset = match anchor { TreeDiffInsertPos::After(it) => it.text_range().end(), @@ -154,6 +156,8 @@ impl TreeDiff { /// /// This function tries to find a fine-grained diff. pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff { + let _p = profile::span("diff"); + let mut diff = TreeDiff { replacements: FxHashMap::default(), insertions: FxIndexMap::default(), @@ -467,6 +471,8 @@ impl<'a> SyntaxRewriter<'a> { } pub fn rewrite(&self, node: &SyntaxNode) -> SyntaxNode { + let _p = profile::span("rewrite"); + if self.f.is_none() && self.replacements.is_empty() && self.insertions.is_empty() { return node.clone(); } @@ -483,6 +489,7 @@ impl<'a> SyntaxRewriter<'a> { /// /// Returns `None` when there are no replacements. pub fn rewrite_root(&self) -> Option { + let _p = profile::span("rewrite_root"); fn element_to_node_or_parent(element: &SyntaxElement) -> SyntaxNode { match element { SyntaxElement::Node(it) => it.clone(), @@ -517,6 +524,8 @@ impl<'a> SyntaxRewriter<'a> { } fn rewrite_children(&self, node: &SyntaxNode) -> SyntaxNode { + let _p = profile::span("rewrite_children"); + // FIXME: this could be made much faster. let mut new_children = Vec::new(); if let Some(elements) = self.insertions(&InsertPos::FirstChildOf(node.clone())) { @@ -533,6 +542,8 @@ impl<'a> SyntaxRewriter<'a> { acc: &mut Vec>, element: &SyntaxElement, ) { + let _p = profile::span("rewrite_self"); + if let Some(replacement) = self.replacement(&element) { match replacement { Replacement::Single(element) => acc.push(element_to_green(element)), @@ -588,6 +599,8 @@ fn with_children( parent: &SyntaxNode, new_children: Vec>, ) -> SyntaxNode { + let _p = profile::span("with_children"); + let len = new_children.iter().map(|it| it.text_len()).sum::(); let new_node = rowan::GreenNode::new(rowan::SyntaxKind(parent.kind() as u16), new_children); let new_root_node = parent.replace_with(new_node); -- cgit v1.2.3