make LRU cache configurable

This commit is contained in:
Aleksey Kladov 2019-06-07 20:49:29 +03:00
parent 15668119de
commit fed52706de
8 changed files with 48 additions and 11 deletions

View file

@ -41,6 +41,12 @@ impl salsa::Database for RootDatabase {
impl Default for RootDatabase { impl Default for RootDatabase {
fn default() -> RootDatabase { fn default() -> RootDatabase {
RootDatabase::new(None)
}
}
impl RootDatabase {
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
let mut db = RootDatabase { let mut db = RootDatabase {
runtime: salsa::Runtime::default(), runtime: salsa::Runtime::default(),
last_gc: time::Instant::now(), last_gc: time::Instant::now(),
@ -49,9 +55,9 @@ impl Default for RootDatabase {
db.set_crate_graph(Default::default()); db.set_crate_graph(Default::default());
db.set_local_roots(Default::default()); db.set_local_roots(Default::default());
db.set_library_roots(Default::default()); db.set_library_roots(Default::default());
let lru_cap = ra_db::DEFAULT_LRU_CAP; let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP);
db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_cap); db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity);
db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_cap); db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity);
db db
} }
} }

View file

@ -242,12 +242,21 @@ pub struct CallInfo {
} }
/// `AnalysisHost` stores the current state of the world. /// `AnalysisHost` stores the current state of the world.
#[derive(Debug, Default)] #[derive(Debug)]
pub struct AnalysisHost { pub struct AnalysisHost {
db: db::RootDatabase, db: db::RootDatabase,
} }
impl Default for AnalysisHost {
fn default() -> AnalysisHost {
AnalysisHost::new(None)
}
}
impl AnalysisHost { impl AnalysisHost {
pub fn new(lru_capcity: Option<usize>) -> AnalysisHost {
AnalysisHost { db: db::RootDatabase::new(lru_capcity) }
}
/// Returns a snapshot of the current state, which you can query for /// Returns a snapshot of the current state, which you can query for
/// semantic information. /// semantic information.
pub fn analysis(&self) -> Analysis { pub fn analysis(&self) -> Analysis {

View file

@ -17,11 +17,17 @@ pub struct InitializationOptions {
/// Defaults to `true` /// Defaults to `true`
#[serde(deserialize_with = "nullable_bool_true")] #[serde(deserialize_with = "nullable_bool_true")]
pub show_workspace_loaded: bool, pub show_workspace_loaded: bool,
pub lru_capacity: Option<usize>,
} }
impl Default for InitializationOptions { impl Default for InitializationOptions {
fn default() -> InitializationOptions { fn default() -> InitializationOptions {
InitializationOptions { publish_decorations: false, show_workspace_loaded: true } InitializationOptions {
publish_decorations: false,
show_workspace_loaded: true,
lru_capacity: None,
}
} }
} }
@ -54,7 +60,9 @@ mod test {
assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap()); assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap());
assert_eq!( assert_eq!(
default, default,
serde_json::from_str(r#"{"publishDecorations":null, "showWorkspaceLoaded":null}"#) serde_json::from_str(
r#"{"publishDecorations":null, "showWorkspaceLoaded":null, "lruCapacity":null}"#
)
.unwrap() .unwrap()
); );
} }

View file

@ -73,7 +73,7 @@ pub fn main_loop(
loaded_workspaces 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 pool = ThreadPool::new(THREADPOOL_SIZE);
let (task_sender, task_receiver) = unbounded::<Task>(); let (task_sender, task_receiver) = unbounded::<Task>();

View file

@ -46,7 +46,11 @@ pub struct WorldSnapshot {
} }
impl WorldState { impl WorldState {
pub fn new(folder_roots: Vec<PathBuf>, workspaces: Vec<ProjectWorkspace>) -> WorldState { pub fn new(
folder_roots: Vec<PathBuf>,
workspaces: Vec<ProjectWorkspace>,
lru_capacity: Option<usize>,
) -> WorldState {
let mut change = AnalysisChange::new(); let mut change = AnalysisChange::new();
let mut roots = Vec::new(); let mut roots = Vec::new();
@ -74,7 +78,7 @@ impl WorldState {
} }
change.set_crate_graph(crate_graph); 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); analysis_host.apply_change(change);
WorldState { WorldState {
roots_to_scan, roots_to_scan,

View file

@ -232,6 +232,11 @@
], ],
"default": "off", "default": "off",
"description": "Trace output of cargo-watch" "description": "Trace output of cargo-watch"
},
"rust-analyzer.lruCapacity": {
"type": "number",
"default": null,
"description": "Number of syntax trees rust-analyzer keeps in memory"
} }
} }
}, },

View file

@ -19,6 +19,7 @@ export class Config {
public enableEnhancedTyping = true; public enableEnhancedTyping = true;
public raLspServerPath = RA_LSP_DEBUG || 'ra_lsp_server'; public raLspServerPath = RA_LSP_DEBUG || 'ra_lsp_server';
public showWorkspaceLoadedNotification = true; public showWorkspaceLoadedNotification = true;
public lruCapacity: null | number = null;
public cargoWatchOptions: CargoWatchOptions = { public cargoWatchOptions: CargoWatchOptions = {
enableOnStartup: 'ask', enableOnStartup: 'ask',
trace: 'off', trace: 'off',
@ -109,5 +110,8 @@ export class Config {
'' ''
); );
} }
if (config.has('lruCapacity')) {
this.lruCapacity = config.get('lruCapacity') as number;
}
} }
} }

View file

@ -35,7 +35,8 @@ export class Server {
initializationOptions: { initializationOptions: {
publishDecorations: true, publishDecorations: true,
showWorkspaceLoaded: showWorkspaceLoaded:
Server.config.showWorkspaceLoadedNotification Server.config.showWorkspaceLoadedNotification,
lruCapacity: Server.config.lruCapacity
}, },
traceOutputChannel traceOutputChannel
}; };