From fed52706def9a9f5d33edc7dd9848a02ae475ba5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Jun 2019 20:49:29 +0300 Subject: make LRU cache configurable --- crates/ra_ide_api/src/db.rs | 12 +++++++++--- crates/ra_ide_api/src/lib.rs | 11 ++++++++++- crates/ra_lsp_server/src/init.rs | 14 +++++++++++--- crates/ra_lsp_server/src/main_loop.rs | 2 +- crates/ra_lsp_server/src/world.rs | 8 ++++++-- editors/code/package.json | 5 +++++ editors/code/src/config.ts | 4 ++++ editors/code/src/server.ts | 3 ++- 8 files changed, 48 insertions(+), 11 deletions(-) diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index cccf0ada4..b3f395502 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -41,6 +41,12 @@ impl salsa::Database for RootDatabase { impl Default for RootDatabase { fn default() -> RootDatabase { + RootDatabase::new(None) + } +} + +impl RootDatabase { + pub fn new(lru_capacity: Option) -> RootDatabase { let mut db = RootDatabase { runtime: salsa::Runtime::default(), last_gc: time::Instant::now(), @@ -49,9 +55,9 @@ impl Default for RootDatabase { db.set_crate_graph(Default::default()); db.set_local_roots(Default::default()); db.set_library_roots(Default::default()); - let lru_cap = ra_db::DEFAULT_LRU_CAP; - db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_cap); - db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_cap); + let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP); + db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity); + db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity); db } } diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index dbebf50a6..8741e736f 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -242,12 +242,21 @@ pub struct CallInfo { } /// `AnalysisHost` stores the current state of the world. -#[derive(Debug, Default)] +#[derive(Debug)] pub struct AnalysisHost { db: db::RootDatabase, } +impl Default for AnalysisHost { + fn default() -> AnalysisHost { + AnalysisHost::new(None) + } +} + impl AnalysisHost { + pub fn new(lru_capcity: Option) -> AnalysisHost { + AnalysisHost { db: db::RootDatabase::new(lru_capcity) } + } /// Returns a snapshot of the current state, which you can query for /// semantic information. pub fn analysis(&self) -> Analysis { diff --git a/crates/ra_lsp_server/src/init.rs b/crates/ra_lsp_server/src/init.rs index 1b77e0312..b894b449d 100644 --- a/crates/ra_lsp_server/src/init.rs +++ b/crates/ra_lsp_server/src/init.rs @@ -17,11 +17,17 @@ pub struct InitializationOptions { /// Defaults to `true` #[serde(deserialize_with = "nullable_bool_true")] pub show_workspace_loaded: bool, + + pub lru_capacity: Option, } impl Default for InitializationOptions { fn default() -> InitializationOptions { - InitializationOptions { publish_decorations: false, show_workspace_loaded: true } + InitializationOptions { + publish_decorations: false, + show_workspace_loaded: true, + lru_capacity: None, + } } } @@ -54,8 +60,10 @@ mod test { assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap()); assert_eq!( default, - serde_json::from_str(r#"{"publishDecorations":null, "showWorkspaceLoaded":null}"#) - .unwrap() + serde_json::from_str( + r#"{"publishDecorations":null, "showWorkspaceLoaded":null, "lruCapacity":null}"# + ) + .unwrap() ); } } diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 090fb9b1b..0790ea472 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -73,7 +73,7 @@ pub fn main_loop( loaded_workspaces }; - let mut state = WorldState::new(ws_roots, workspaces); + let mut state = WorldState::new(ws_roots, workspaces, options.lru_capacity); let pool = ThreadPool::new(THREADPOOL_SIZE); let (task_sender, task_receiver) = unbounded::(); diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index cd8df4fdb..f9ce570ca 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -46,7 +46,11 @@ pub struct WorldSnapshot { } impl WorldState { - pub fn new(folder_roots: Vec, workspaces: Vec) -> WorldState { + pub fn new( + folder_roots: Vec, + workspaces: Vec, + lru_capacity: Option, + ) -> WorldState { let mut change = AnalysisChange::new(); let mut roots = Vec::new(); @@ -74,7 +78,7 @@ impl WorldState { } change.set_crate_graph(crate_graph); - let mut analysis_host = AnalysisHost::default(); + let mut analysis_host = AnalysisHost::new(lru_capacity); analysis_host.apply_change(change); WorldState { roots_to_scan, diff --git a/editors/code/package.json b/editors/code/package.json index 05c808394..c2ed8d126 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -232,6 +232,11 @@ ], "default": "off", "description": "Trace output of cargo-watch" + }, + "rust-analyzer.lruCapacity": { + "type": "number", + "default": null, + "description": "Number of syntax trees rust-analyzer keeps in memory" } } }, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 8d73a6b34..3024546d2 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -19,6 +19,7 @@ export class Config { public enableEnhancedTyping = true; public raLspServerPath = RA_LSP_DEBUG || 'ra_lsp_server'; public showWorkspaceLoadedNotification = true; + public lruCapacity: null | number = null; public cargoWatchOptions: CargoWatchOptions = { enableOnStartup: 'ask', trace: 'off', @@ -109,5 +110,8 @@ export class Config { '' ); } + if (config.has('lruCapacity')) { + this.lruCapacity = config.get('lruCapacity') as number; + } } } diff --git a/editors/code/src/server.ts b/editors/code/src/server.ts index 81c2b3fff..7029142fd 100644 --- a/editors/code/src/server.ts +++ b/editors/code/src/server.ts @@ -35,7 +35,8 @@ export class Server { initializationOptions: { publishDecorations: true, showWorkspaceLoaded: - Server.config.showWorkspaceLoadedNotification + Server.config.showWorkspaceLoadedNotification, + lruCapacity: Server.config.lruCapacity }, traceOutputChannel }; -- cgit v1.2.3