Improve query cache efficiency

This commit is contained in:
oxalica 2023-02-14 16:04:45 +08:00
parent 4dd931f460
commit d62d4a4dd4
5 changed files with 22 additions and 15 deletions

View file

@ -180,7 +180,7 @@ mod tests {
let (db, f) = TestDB::from_fixture(fixture).unwrap();
assert_eq!(f.files().len(), 1);
let file = f.files()[0];
assert_eq!(db.module(file).diagnostics(), Vec::new(), "Lower error");
assert_eq!(db.source_map(file).diagnostics(), Vec::new(), "Lower error");
let expect = f.markers().iter().map(|p| p.pos).collect::<Vec<_>>();
let mut got = db
.liveness_check(file)

View file

@ -26,7 +26,6 @@ pub(super) fn lower(
names: Arena::new(),
// Placeholder.
entry_expr: ExprId::from_raw(0.into()),
diagnostics: Vec::new(),
},
source_map: ModuleSourceMap::default(),
};
@ -60,7 +59,7 @@ impl LowerCtx<'_> {
}
fn diagnostic(&mut self, diag: Diagnostic) {
self.module.diagnostics.push(diag);
self.source_map.diagnostics.push(diag);
}
fn lower_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
@ -603,11 +602,12 @@ mod tests {
fn check_lower(src: &str, expect: Expect) {
let (db, file_id) = TestDB::single_file(src).unwrap();
let module = db.module(file_id);
let source_map = db.source_map(file_id);
let mut got = String::new();
for diag in module.diagnostics() {
for diag in source_map.diagnostics() {
writeln!(got, "{}", diag.debug_display()).unwrap();
}
if !module.diagnostics.is_empty() {
if !source_map.diagnostics.is_empty() {
writeln!(got).unwrap();
}
for (i, e) in module.exprs.iter() {
@ -640,9 +640,8 @@ mod tests {
#[track_caller]
fn check_error(src: &str, expect: Expect) {
let (db, file_id) = TestDB::single_file(src).unwrap();
let module = db.module(file_id);
let mut got = String::new();
for diag in module.diagnostics() {
for diag in db.source_map(file_id).diagnostics() {
writeln!(got, "{}", diag.debug_display()).unwrap();
}
expect.assert_eq(&got);

View file

@ -108,7 +108,6 @@ pub struct Module {
exprs: Arena<Expr>,
names: Arena<Name>,
entry_expr: ExprId,
diagnostics: Vec<Diagnostic>,
}
pub type ExprId = Idx<Expr>;
@ -132,17 +131,12 @@ impl Module {
pub fn shrink_to_fit(&mut self) {
self.exprs.shrink_to_fit();
self.names.shrink_to_fit();
self.diagnostics.shrink_to_fit();
}
pub fn entry_expr(&self) -> ExprId {
self.entry_expr
}
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &'_ Expr)> + ExactSizeIterator + '_ {
self.exprs.iter()
}
@ -184,6 +178,9 @@ pub struct ModuleSourceMap {
expr_map_rev: HashMap<ExprId, AstPtr>,
name_map: HashMap<AstPtr, NameId>,
name_map_rev: ArenaMap<NameId, Vec<AstPtr>>,
// This contains locations, thus is quite volatile.
diagnostics: Vec<Diagnostic>,
}
impl ModuleSourceMap {
@ -192,6 +189,7 @@ impl ModuleSourceMap {
self.expr_map_rev.shrink_to_fit();
self.name_map.shrink_to_fit();
self.name_map_rev.shrink_to_fit();
self.diagnostics.shrink_to_fit();
}
pub fn expr_for_node(&self, node: AstPtr) -> Option<ExprId> {
@ -213,6 +211,10 @@ impl ModuleSourceMap {
.flatten()
.cloned()
}
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
}
#[derive(Debug, Clone, PartialEq, Eq)]

View file

@ -8,8 +8,8 @@ pub(crate) fn diagnostics(db: &dyn DefDatabase, file: FileId) -> Vec<Diagnostic>
diags.extend(parse.errors().iter().map(|&err| Diagnostic::from(err)));
// Lowering.
let module = db.module(file);
diags.extend(module.diagnostics().iter().cloned());
let source_map = db.source_map(file);
diags.extend(source_map.diagnostics().iter().cloned());
// Name resolution.
diags.extend(db.name_resolution(file).to_diagnostics(db, file));

View file

@ -33,6 +33,8 @@ pub use rename::RenameResult;
pub use symbol_hierarchy::SymbolTree;
pub use syntax_highlighting::{HlAttrField, HlKeyword, HlOperator, HlPunct, HlRange, HlTag};
pub const DEFAULT_LRU_CAP: usize = 128;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NavigationTarget {
pub file_id: FileId,
@ -72,6 +74,10 @@ impl Default for RootDatabase {
let mut db = Self {
storage: salsa::Storage::default(),
};
crate::def::ParseQuery
.in_db_mut(&mut db)
.set_lru_capacity(DEFAULT_LRU_CAP);
db.set_flake_graph_with_durability(Default::default(), Durability::MEDIUM);
db
}