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 {
fn default() -> RootDatabase {
RootDatabase::new(None)
}
}
impl RootDatabase {
pub fn new(lru_capacity: Option<usize>) -> 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
}
}

View File

@ -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<usize>) -> 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 {

View File

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

View File

@ -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::<Task>();

View File

@ -46,7 +46,11 @@ pub struct WorldSnapshot {
}
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 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,

View File

@ -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"
}
}
},

View File

@ -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;
}
}
}

View File

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