From 4930c488226741b116f64afe2387c31da6c385b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20F=C3=B6rster?= Date: Tue, 3 Mar 2020 09:53:27 +0100 Subject: [PATCH] Paralellize creation of LaTeX symbol table --- Cargo.lock | 1 + Cargo.toml | 1 + src/syntax/latex/analysis.rs | 85 ++++++++++++++++++++++-------------- src/syntax/latex/ast.rs | 24 ++++------ 4 files changed, 64 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4892aea22..517a01579 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1398,6 +1398,7 @@ dependencies = [ "nom 5.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "once_cell 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "petgraph 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c558b3f75..fa6109005 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ lsp-types = { version = "0.61.0", features = ["proposed"] } nom = "5.1" once_cell = "1.3" petgraph = { version = "0.5", features = ["serde-1"] } +rayon = "1.3" relative-path = { version = "1.0", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/src/syntax/latex/analysis.rs b/src/syntax/latex/analysis.rs index 04f549067..6592484f1 100644 --- a/src/syntax/latex/analysis.rs +++ b/src/syntax/latex/analysis.rs @@ -63,49 +63,70 @@ impl SymbolTable { cwd, }; - let environments = Environment::parse(ctx); - let is_standalone = environments.iter().any(|env| env.is_root(&tree)); + let mut environments = None; + let mut includes = None; + let mut citations = None; + let mut command_definitions = None; + let mut glossary_entries = None; + let mut equations = None; + let mut inlines = None; + let mut math_operators = None; + let mut theorem_definitions = None; + let mut sections = None; + let mut labels = None; + let mut label_numberings = None; + let mut captions = None; + let mut items = None; + + rayon::scope(|s| { + s.spawn(|_| environments = Some(Environment::parse(ctx))); + s.spawn(|_| includes = Some(Include::parse(ctx))); + s.spawn(|_| citations = Some(Citation::parse(ctx))); + s.spawn(|_| command_definitions = Some(CommandDefinition::parse(ctx))); + s.spawn(|_| glossary_entries = Some(GlossaryEntry::parse(ctx))); + s.spawn(|_| equations = Some(Equation::parse(ctx))); + s.spawn(|_| inlines = Some(Inline::parse(ctx))); + s.spawn(|_| math_operators = Some(MathOperator::parse(ctx))); + s.spawn(|_| theorem_definitions = Some(TheoremDefinition::parse(ctx))); + s.spawn(|_| sections = Some(Section::parse(ctx))); + s.spawn(|_| labels = Some(Label::parse(ctx))); + s.spawn(|_| label_numberings = Some(LabelNumbering::parse(ctx))); + s.spawn(|_| captions = Some(Caption::parse(ctx))); + s.spawn(|_| items = Some(Item::parse(ctx))); + }); + + let is_standalone = environments + .as_ref() + .unwrap() + .iter() + .any(|env| env.is_root(&tree)); - let includes = Include::parse(ctx); let components = includes + .as_ref() + .unwrap() .iter() .flat_map(|include| include.components(&tree)) .collect(); - let citations = Citation::parse(ctx); - let command_definitions = CommandDefinition::parse(ctx); - let glossary_entries = GlossaryEntry::parse(ctx); - - let equations = Equation::parse(ctx); - let inlines = Inline::parse(ctx); - let math_operators = MathOperator::parse(ctx); - let theorem_definitions = TheoremDefinition::parse(ctx); - - let sections = Section::parse(ctx); - let labels = Label::parse(ctx); - let label_numberings = LabelNumbering::parse(ctx); - let captions = Caption::parse(ctx); - let items = Item::parse(ctx); - Self { tree, commands, - environments, + environments: environments.unwrap(), is_standalone, - includes, + includes: includes.unwrap(), components, - citations, - command_definitions, - glossary_entries, - equations, - inlines, - math_operators, - theorem_definitions, - sections, - labels, - label_numberings, - captions, - items, + citations: citations.unwrap(), + command_definitions: command_definitions.unwrap(), + glossary_entries: glossary_entries.unwrap(), + equations: equations.unwrap(), + inlines: inlines.unwrap(), + math_operators: math_operators.unwrap(), + theorem_definitions: theorem_definitions.unwrap(), + sections: sections.unwrap(), + labels: labels.unwrap(), + label_numberings: label_numberings.unwrap(), + captions: captions.unwrap(), + items: items.unwrap(), } } } diff --git a/src/syntax/latex/ast.rs b/src/syntax/latex/ast.rs index 366f340cc..97caf7284 100644 --- a/src/syntax/latex/ast.rs +++ b/src/syntax/latex/ast.rs @@ -163,7 +163,7 @@ impl Tree { pub fn children(&self, parent: NodeIndex) -> impl Iterator { self.graph .neighbors(parent) - .sorted_by_key(|child| self.graph.node_weight(*child).unwrap().start()) + .sorted_by_key(|child| self.graph[*child].start()) } pub fn walk(&self, visitor: &mut V, parent: NodeIndex) { @@ -179,7 +179,7 @@ impl Tree { } pub fn print(&self, node: NodeIndex) -> String { - let start_position = self.graph.node_weight(node).unwrap().start(); + let start_position = self.graph[node].start(); let mut printer = Printer::new(start_position); printer.visit(self, node); printer.output @@ -193,7 +193,7 @@ impl Tree { } pub fn as_group(&self, node: NodeIndex) -> Option<&Group> { - if let Node::Group(group) = self.graph.node_weight(node)? { + if let Node::Group(group) = &self.graph[node] { Some(group) } else { None @@ -201,7 +201,7 @@ impl Tree { } pub fn as_command(&self, node: NodeIndex) -> Option<&Command> { - if let Node::Command(cmd) = self.graph.node_weight(node)? { + if let Node::Command(cmd) = &self.graph[node] { Some(cmd) } else { None @@ -209,7 +209,7 @@ impl Tree { } pub fn as_text(&self, node: NodeIndex) -> Option<&Text> { - if let Node::Text(text) = self.graph.node_weight(node)? { + if let Node::Text(text) = &self.graph[node] { Some(text) } else { None @@ -217,7 +217,7 @@ impl Tree { } pub fn as_math(&self, node: NodeIndex) -> Option<&Math> { - if let Node::Math(math) = self.graph.node_weight(node)? { + if let Node::Math(math) = &self.graph[node] { Some(math) } else { None @@ -278,7 +278,7 @@ impl Tree { let group = self.extract_group(parent, group_kind, index)?; let mut words = Vec::new(); for child in self.children(group) { - match self.graph.node_weight(child)? { + match &self.graph[child] { Node::Root(_) | Node::Group(_) | Node::Command(_) | Node::Math(_) => return None, Node::Text(text) => { for word in &text.words { @@ -313,13 +313,7 @@ impl Finder { impl Visitor for Finder { fn visit(&mut self, tree: &Tree, node: NodeIndex) { - if tree - .graph - .node_weight(node) - .unwrap() - .range() - .contains(self.position) - { + if tree.graph[node].range().contains(self.position) { self.results.push(node); tree.walk(self, node); } @@ -365,7 +359,7 @@ impl Printer { impl Visitor for Printer { fn visit(&mut self, tree: &Tree, node: NodeIndex) { - match tree.graph.node_weight(node).unwrap() { + match &tree.graph[node] { Node::Root(_) => tree.walk(self, node), Node::Group(group) => { self.print_token(&group.left);