From 30d8bfd75c1a3d1988ac84b79ac386b5c9d12eca Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Wed, 17 Jan 2024 21:43:23 +1100 Subject: [PATCH] LSP Optimization: Wrap `programs_cache` in an `Arc` (#5472) ## Description We clone the `Engines` before each keystroke in the language server. We shouldn't need to clone anything other than a pointer for the `programs_cache`. Measured against the `benchmark` example in `sway-lsp` tests, this saves us `23.015ms`. related to #5445 ## Checklist - [x] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --- sway-core/src/query_engine/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/sway-core/src/query_engine/mod.rs b/sway-core/src/query_engine/mod.rs index d2397c17bda..b82ae516996 100644 --- a/sway-core/src/query_engine/mod.rs +++ b/sway-core/src/query_engine/mod.rs @@ -47,15 +47,15 @@ pub type ProgramsCacheMap = HashMap; #[derive(Debug, Default)] pub struct QueryEngine { - parse_module_cache: RwLock, - programs_cache: RwLock, + parse_module_cache: Arc>, + programs_cache: Arc>, } impl Clone for QueryEngine { fn clone(&self) -> Self { Self { - parse_module_cache: RwLock::new(self.parse_module_cache.read().unwrap().clone()), - programs_cache: RwLock::new(self.programs_cache.read().unwrap().clone()), + parse_module_cache: self.parse_module_cache.clone(), + programs_cache: self.programs_cache.clone(), } } } @@ -67,11 +67,10 @@ impl QueryEngine { } pub fn insert_parse_module_cache_entry(&self, entry: ModuleCacheEntry) { - let mut cache = self.parse_module_cache.write().unwrap(); let path = entry.path.clone(); let include_tests = entry.include_tests; - let key = ModuleCacheKey::new(path, include_tests); + let mut cache = self.parse_module_cache.write().unwrap(); cache.insert(key, entry); }