TODO §71.1. On OpenTitan hw (4 058 files) every save cost ~160 ms of whole-index work, twice (the save and VS Code's watcher echo), and watcher events for files the index could not hold started whole rescans. - vlog_semantic: resolution records every table lookup per reading file, misses included (workspace/dependents.rs, references.rs, replace.rs); an update re-resolves the edited file and the readers of keys whose values changed in what resolution reads. An unchanged record merges nothing. When the readers hold more than an eighth of all references, old results are removed in one pass over the postings. - lucisv_server: watcher events are classified per file (state/watched.rs); the include graph keeps every include line as written and re-resolves the lines a named path could satisfy, in one pass per event; an event naming more paths than half the index takes the single rescan; an open buffer the index does not hold joins on its first save; completion tables are rebuilt only for the names a file declares. Every save still republishes every open buffer. The line-length lint follows the formatting style's width. - Fixed on the way: removing an included header left other files' include edges pointing at it. Typical saves: comment or watcher echo <0.3 ms, local signal 4 ms, package member 12 ms; warm startup +40..120 ms for the readers table. QA failed the first cut on four stale answers found with an independent fresh-server oracle; all four are fixed and are differential tests. Dev (three rounds), QA (two rounds) and lead review: docs/features/localized-reindex/README.md. TODO.md files lane 71.7 from what the QA rounds and an investigation found in earlier releases too. Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both package-lock.json spots); notes in CHANGELOG.md (## 0.65.6) and editors/vscode/CHANGELOG.md. Gates: tools/gates.sh PASS on this tree; vlog_semantic 435/0, lucisv_server 2011/0, lucisv_mcp 384/0. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
177 lines
7.5 KiB
Rust
177 lines
7.5 KiB
Rust
//! The declaration → use postings, built from every file's reference facts or
|
|
//! re-done for a few files: what an update calls once it knows which files
|
|
//! read a key it moved (`docs/features/localized-reindex/README.md`).
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use super::dependents::{recording, Reads};
|
|
use super::external::{resolve_fact, ExternalTarget};
|
|
use super::record::ReferenceFact;
|
|
use super::{DefinitionKey, FileId, ReferenceSite, WorkspaceIndex};
|
|
|
|
/// What one reference puts into the postings.
|
|
enum Posting {
|
|
/// One use of one declaration.
|
|
Site(DefinitionKey, ReferenceSite),
|
|
/// A use that could name any of these declarations equally.
|
|
Ambiguous(Vec<DefinitionKey>),
|
|
}
|
|
|
|
impl WorkspaceIndex {
|
|
/// Rebuild every posting, the ambiguity counts and the readers table from
|
|
/// every file's facts.
|
|
pub(super) fn merge_references(&mut self) {
|
|
self.reference_sites.clear();
|
|
self.ambiguous_references.clear();
|
|
self.ambiguous_by_file.clear();
|
|
self.dependents.clear();
|
|
let every: Vec<FileId> = (0..self.facts.len()).map(|id| FileId(id as u32)).collect();
|
|
self.resolve_files(&every);
|
|
}
|
|
|
|
/// Put `files`' postings in, resolved against the tables as they stand.
|
|
///
|
|
/// `files` is in id order, and each file's postings go in after every
|
|
/// posting of a lower-numbered file and before every higher one — the
|
|
/// order [`WorkspaceIndex::merge_references`] produces by visiting the
|
|
/// files in that order, so a few files re-done in place give the lists a
|
|
/// whole rebuild gives.
|
|
pub(super) fn resolve_files(&mut self, files: &[FileId]) {
|
|
for &file in files {
|
|
let (postings, reads) = self.postings_of(file);
|
|
for posting in postings {
|
|
match posting {
|
|
Posting::Site(key, site) => {
|
|
let sites = self.reference_sites.entry(key).or_default();
|
|
let at = sites.partition_point(|held| held.file <= file);
|
|
sites.insert(at, site);
|
|
}
|
|
Posting::Ambiguous(keys) => {
|
|
for key in keys {
|
|
*self.ambiguous_references.entry(key).or_default() += 1;
|
|
self.ambiguous_by_file.entry(file).or_default().push(key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.dependents.add(file, reads);
|
|
}
|
|
}
|
|
|
|
/// Take `files`' postings out again.
|
|
///
|
|
/// Re-resolving them against the tables as they stand finds exactly what
|
|
/// [`WorkspaceIndex::resolve_files`] put in, because those are the tables
|
|
/// it was put in against — every update keeps the postings equal to a
|
|
/// resolution of every file over the current tables, and resolution is a
|
|
/// pure function of the two. So nothing per file has to be remembered to
|
|
/// undo it.
|
|
pub(super) fn unresolve_files(&mut self, files: &[FileId]) {
|
|
let leaving: HashSet<FileId> = files.iter().copied().collect();
|
|
let mut touched: HashSet<DefinitionKey> = HashSet::new();
|
|
for &file in files {
|
|
let (postings, reads) = self.postings_of(file);
|
|
for posting in postings {
|
|
match posting {
|
|
Posting::Site(key, _) => {
|
|
touched.insert(key);
|
|
}
|
|
Posting::Ambiguous(keys) => {
|
|
keys.iter().for_each(|key| self.forget_ambiguity(key))
|
|
}
|
|
}
|
|
}
|
|
self.dependents.remove(file, &reads);
|
|
self.ambiguous_by_file.remove(&file);
|
|
}
|
|
for key in touched {
|
|
let Some(sites) = self.reference_sites.get_mut(&key) else { continue };
|
|
sites.retain(|site| !leaving.contains(&site.file));
|
|
if sites.is_empty() {
|
|
self.reference_sites.remove(&key);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// [`WorkspaceIndex::unresolve_files`] by one pass over the postings
|
|
/// rather than by re-resolving `files`: every site a file put in is
|
|
/// dropped wherever it is, its ambiguity taken back from what it is
|
|
/// recorded to have contributed, and its id struck from every reader
|
|
/// list. Linear in the postings, not in `files` — the cheaper way out when
|
|
/// `files` hold a large share of every reference (a package most of the
|
|
/// tree imports), where re-resolving them would be most of a whole merge.
|
|
pub(super) fn unresolve_by_sweep(&mut self, files: &[FileId]) {
|
|
let mut leaving = vec![false; self.facts.len()];
|
|
for file in files {
|
|
if let Some(slot) = leaving.get_mut(file.0 as usize) {
|
|
*slot = true;
|
|
}
|
|
}
|
|
let gone = |file: FileId| leaving.get(file.0 as usize).copied().unwrap_or(false);
|
|
self.reference_sites.retain(|_, sites| {
|
|
sites.retain(|site| !gone(site.file));
|
|
!sites.is_empty()
|
|
});
|
|
for file in files {
|
|
for key in self.ambiguous_by_file.remove(file).unwrap_or_default() {
|
|
self.forget_ambiguity(&key);
|
|
}
|
|
}
|
|
self.dependents.remove_all(&gone);
|
|
}
|
|
|
|
/// How many references `files` hold, against how many every file holds —
|
|
/// the two numbers that choose between the two ways out.
|
|
pub(super) fn reference_share(&self, files: &[FileId]) -> (usize, usize) {
|
|
let count =
|
|
|file: &FileId| self.facts.get(file.0 as usize).map_or(0, |f| f.references.len());
|
|
let total = self.facts.iter().map(|facts| facts.references.len()).sum();
|
|
(files.iter().map(count).sum(), total)
|
|
}
|
|
|
|
fn forget_ambiguity(&mut self, key: &DefinitionKey) {
|
|
let Some(count) = self.ambiguous_references.get_mut(key) else { return };
|
|
*count -= 1;
|
|
if *count == 0 {
|
|
self.ambiguous_references.remove(key);
|
|
}
|
|
}
|
|
|
|
/// One file's postings, and every table key resolving them read.
|
|
fn postings_of(&self, file: FileId) -> (Vec<Posting>, Reads) {
|
|
let Some(facts) = self.facts.get(file.0 as usize) else {
|
|
return (Vec::new(), Reads::default());
|
|
};
|
|
recording(|| {
|
|
let mut out = Vec::new();
|
|
for reference in &facts.references {
|
|
let site = ReferenceSite { file, name_span: reference.occurrence() };
|
|
match reference {
|
|
ReferenceFact::Local { definition, .. } => {
|
|
let key = DefinitionKey {
|
|
file,
|
|
name_start: definition.start,
|
|
name_end: definition.end,
|
|
};
|
|
out.push(Posting::Site(key, site));
|
|
}
|
|
ReferenceFact::External { name, base, paths, surface, .. } => {
|
|
match resolve_fact(self, name, base.as_deref(), paths, *surface) {
|
|
ExternalTarget::Resolved(def) => {
|
|
out.push(Posting::Site(DefinitionKey::of(&def), site));
|
|
}
|
|
ExternalTarget::Ambiguous(defs) => {
|
|
out.push(Posting::Ambiguous(
|
|
defs.iter().map(DefinitionKey::of).collect(),
|
|
));
|
|
}
|
|
ExternalTarget::StillUnresolved => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out
|
|
})
|
|
}
|
|
}
|