Skip to content

Commit

Permalink
LSP Optimization: Wrap programs_cache in an Arc (#5472)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
JoshuaBatty authored Jan 17, 2024
1 parent 813ef65 commit 30d8bfd
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions sway-core/src/query_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ pub type ProgramsCacheMap = HashMap<ModulePath, ProgramsCacheEntry>;

#[derive(Debug, Default)]
pub struct QueryEngine {
parse_module_cache: RwLock<ModuleCacheMap>,
programs_cache: RwLock<ProgramsCacheMap>,
parse_module_cache: Arc<RwLock<ModuleCacheMap>>,
programs_cache: Arc<RwLock<ProgramsCacheMap>>,
}

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(),
}
}
}
Expand All @@ -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);
}

Expand Down

0 comments on commit 30d8bfd

Please sign in to comment.