mir_analyzer/session/ingest.rs
1use super::*;
2
3/// One file's disk-cache lookup result from `AnalysisSession::warm_start_files`'s
4/// parallel read phase — everything the sequential apply phase needs, so it
5/// never has to re-read anything.
6struct WarmStartHit {
7 file: Arc<str>,
8 sf: crate::db::SourceFile,
9 stored_text: Arc<str>,
10 /// `(reference locations, resolved)` from a cached `AnalysisCache` hit.
11 refs: Option<(Vec<RefLoc>, bool)>,
12 /// `(subtype edges, declarations)` from a cached stub-slice hit.
13 stub: Option<(
14 Vec<crate::db::subtype_index::SubtypeEntry>,
15 crate::db::FileDeclarations,
16 )>,
17}
18
19impl AnalysisSession {
20 /// Cheap clone of the salsa db for a read-only query. The lock is held
21 /// only for the duration of the clone, so concurrent readers never
22 /// serialize on each other or on writes for longer than the clone itself.
23 ///
24 /// **Internal API — exposes Salsa types.** Subject to change without
25 /// notice. Public consumers should use the typed query methods
26 /// ([`Self::definition_of`], [`Self::hover`], etc.) instead.
27 #[doc(hidden)]
28 pub fn snapshot_db(&self) -> MirDbStorage {
29 self.db.snapshot_db()
30 }
31
32 /// Register or update a [`crate::db::SourceFile`] salsa input and return its
33 /// handle, without running definition collection or reference recording.
34 ///
35 /// The write-path entry point for a host that drives this db's salsa inputs
36 /// directly (the LSP database-convergence path) and pulls definitions
37 /// lazily via tracked queries, rather than the eager [`Self::ingest_file`].
38 ///
39 /// **Internal API — exposes Salsa types.** Subject to change without notice.
40 #[doc(hidden)]
41 pub fn upsert_source_file(
42 &self,
43 path: Arc<str>,
44 text: Arc<str>,
45 durability: salsa::Durability,
46 ) -> crate::db::SourceFile {
47 self.db.upsert_source_file(path, text, durability)
48 }
49
50 /// Look up an existing [`crate::db::SourceFile`] handle by path.
51 ///
52 /// **Internal API — exposes Salsa types.** Subject to change without notice.
53 #[doc(hidden)]
54 pub fn lookup_source_file(&self, path: &str) -> Option<crate::db::SourceFile> {
55 self.db.lookup_source_file(path)
56 }
57
58 /// Mark a [`crate::db::SourceFile`] as removed from the workspace.
59 ///
60 /// **Internal API — exposes Salsa types.** Subject to change without notice.
61 #[doc(hidden)]
62 pub fn remove_source_file_input(&self, path: &str) {
63 self.db.remove_source_file(path);
64 }
65
66 /// Run `f` with exclusive `&mut` access to the shared salsa db, for a host
67 /// that owns additional salsa ingredients (inputs/tracked fns) on this db
68 /// and needs to create or mutate them. Held under the db write lock, so it
69 /// serialises with all other writers.
70 ///
71 /// **Internal API — exposes Salsa types.** Subject to change without notice.
72 #[doc(hidden)]
73 pub fn with_db_mut<R>(&self, f: impl FnOnce(&mut MirDbStorage) -> R) -> R {
74 let mut guard = self.db.salsa.write();
75 f(&mut guard)
76 }
77
78 /// Run `f` with shared access to the canonical (non-snapshot) salsa db,
79 /// under the read lock. For host-owned reads of off-salsa state that must
80 /// observe the live db rather than a clone.
81 ///
82 /// `f` MUST NOT run salsa queries/input reads (tracked fns, `X.field(db)`):
83 /// the shared handle has one `ZalsaLocal` query stack, so doing so races any
84 /// concurrent salsa read on this handle and aborts the process. Use
85 /// [`Self::snapshot_db`] for salsa queries.
86 ///
87 /// **Internal API — exposes Salsa types.** Subject to change without notice.
88 #[doc(hidden)]
89 pub fn with_db_ref<R>(&self, f: impl FnOnce(&MirDbStorage) -> R) -> R {
90 let guard = self.db.salsa.read();
91 f(&guard)
92 }
93
94 /// Replace `file`'s reference postings with `locs` (its complete set from
95 /// a fresh single-file analysis) and mark freshness against `text` and
96 /// `generation` — both captured before the analysis, so a concurrent
97 /// edit or file add leaves the mark stale, which is the safe direction.
98 /// `resolved` follows [`Self::mark_ref_committed`]'s contract.
99 pub(crate) fn commit_file_refs(
100 &self,
101 file: &Arc<str>,
102 text: Option<Arc<str>>,
103 locs: Vec<RefLoc>,
104 generation: u64,
105 resolved: bool,
106 ) {
107 {
108 let guard = self.db.salsa.read();
109 guard.set_file_reference_locations(file.as_ref(), locs);
110 }
111 if let Some(text) = text {
112 // No memoized output on the imperative path — the empty weak
113 // handle makes the next re-analysis sweep recommit once (and
114 // record the real memo), which is the safe direction.
115 self.mark_ref_committed(file, &text, None, generation, resolved);
116 }
117 }
118
119 /// Run a closure with read access to a database snapshot.
120 ///
121 /// **Internal API — exposes Salsa types.** Subject to change without
122 /// notice.
123 #[doc(hidden)]
124 pub fn read<R>(&self, f: impl FnOnce(&dyn MirDatabase) -> R) -> R {
125 let db = self.snapshot_db();
126 f(&db)
127 }
128
129 /// definition-collection ingestion. Updates the file's source text in the salsa db,
130 /// runs definition collection, and ingests the resulting stub slice.
131 /// Triggers stub loading on first call. Also updates the cache's reverse-
132 /// dependency graph for `file` so cross-file invalidation stays correct
133 /// across incremental edits — without rebuilding the graph from scratch.
134 ///
135 /// If `file` was previously ingested, its old definitions and reference
136 /// locations are removed first so renames / deletions don't leave stale
137 /// state in the codebase. (Without this, long-running sessions would
138 /// accumulate dead reference-location entries indefinitely.)
139 pub fn ingest_file(&self, file: Arc<str>, source: Arc<str>) {
140 self.ensure_all_stubs();
141
142 // The symbols this file defined as of its last ingest. Read from the
143 // explicit `last_ingested_symbols` map rather than re-deriving via
144 // `file_defined_symbols` (a salsa query on the `SourceFile` input):
145 // when a host drives the db directly it may have already updated that
146 // input to the new text, which would make a re-derived "old" set equal
147 // the new set and silently drop deletions.
148 let old_symbols: HashSet<Arc<str>> = self
149 .last_ingested_symbols
150 .read()
151 .get(file.as_ref())
152 .cloned()
153 .unwrap_or_default();
154
155 {
156 let mut guard = self.db.salsa.write();
157 guard.remove_file_definitions(file.as_ref());
158 }
159 let file_defs =
160 self.db
161 .collect_and_ingest_file(file.clone(), source.as_ref(), self.php_version);
162
163 // Derive this file's defined symbols from the `FileDefinitions` just
164 // computed above — do NOT re-read them via a salsa query on the shared
165 // `.salsa.read()` handle. That query (`collect_file_definitions`) borrows
166 // the handle's single `ZalsaLocal` query stack, so two concurrent
167 // `ingest_file` calls doing it would race and abort the process under
168 // debug assertions. Reusing `file_defs` needs no db access at all.
169 let new_symbols: HashSet<Arc<str>> = file_defs.defined_symbols();
170 self.last_ingested_symbols
171 .write()
172 .insert(file.as_ref().to_string(), new_symbols.clone());
173
174 // Symbols removed from this file must be tracked so dependency_graph()
175 // can still produce edges to files referencing the now-gone symbols.
176 let deleted: Vec<Arc<str>> = old_symbols.difference(&new_symbols).cloned().collect();
177 let re_added: Vec<Arc<str>> = new_symbols.difference(&old_symbols).cloned().collect();
178 if !deleted.is_empty() {
179 // A deleted symbol may unshadow a lazy-loadable one (e.g. a vendor
180 // class with the same FQCN); prepared files must re-run warm-up.
181 self.bump_prepare_generation();
182 }
183 if !deleted.is_empty() || !re_added.is_empty() {
184 let mut stale = self.stale_defined_symbols.write();
185 let entry = stale.entry(file.as_ref().to_string()).or_default();
186 for sym in deleted {
187 entry.insert(sym);
188 }
189 for sym in &re_added {
190 entry.remove(sym);
191 }
192 if entry.is_empty() {
193 stale.remove(file.as_ref());
194 }
195 }
196 if !re_added.is_empty() {
197 // A newly-defined symbol may resolve references other files'
198 // commits left unresolved; advance the workspace generation so
199 // their freshness passes re-verify. New-file registration bumps
200 // on its own — this covers definitions appearing in an
201 // already-registered file (edits, `set_file_text` lazy loads).
202 self.db.salsa.write().bump_workspace_revision();
203 }
204
205 self.update_reverse_deps_for(&file);
206 // Evict cached analysis results for files that depend on this one so
207 // that the next re_analyze_file call re-analyses them rather than
208 // replaying a stale cache entry. Mirrors the eviction in
209 // `re_analyze_file` (batch.rs) but applies to the ingest path used by
210 // LSP servers that edit a single file without re-analysing it.
211 if let Some(cache) = self.cache.as_deref() {
212 cache.evict_with_dependents(&[file.to_string()]);
213 }
214 // Only evict cache entries whose resolver-mapped path equals this
215 // file. FQCNs the resolver can't map (psr4 miss) stay cached — no
216 // ingest could change their fate. Avoids the per-keystroke storm
217 // where wholesale clearing forces every unresolved FQCN to re-hit
218 // the resolver on the next FileAnalyzer iteration.
219 self.evict_unresolvable_for_file(&file);
220
221 // If the workspace symbol index singleton has already been built, keep
222 // it consistent with this edit *incrementally*: subtract the file's old
223 // declarations and add its new ones (tier-aware). Body-only edits are a
224 // no-op inside `update_workspace_index_for_file` (name-only
225 // FileDeclarations equality → no singleton write → the HIGH-durability
226 // dep does not invalidate body-analysis memos). Only the rare ambiguous
227 // case (a removed name still declared by another file, where this file
228 // owned the winning entry) falls back to a full O(N) rebuild.
229 {
230 let mut guard = self.db.salsa.write();
231 if guard.workspace_symbol_index_singleton().is_some() {
232 if let Some(sf) = guard.lookup_source_file(file.as_ref()) {
233 if !guard.update_workspace_index_for_file(sf) {
234 guard.rebuild_workspace_symbol_index();
235 }
236 guard.clear_index_pending(file.as_ref());
237 }
238 }
239 }
240
241 // Keep the inverted indexes in step with the edit. Class edges come
242 // straight from the definitions just collected; reference postings
243 // for the new text are recomputed lazily (analysis has not run yet),
244 // so the file's freshness mark is dropped rather than updated.
245 {
246 let entries = crate::db::subtype_index::entries_from_slice(&file_defs.slice);
247 let guard = self.db.salsa.read();
248 guard.set_file_class_edges(&file, entries);
249 }
250 // Freshness is keyed on the Arc actually stored on the input (the
251 // upsert keeps the prior Arc when content is equal), so read it back.
252 let stored_text = {
253 let db = self.snapshot_db();
254 db.lookup_source_file(file.as_ref())
255 .map(|sf| sf.text(&db as &dyn MirDatabase).clone())
256 };
257 if let Some(text) = stored_text {
258 self.mark_defs_committed(&file, &text);
259 }
260 // `remove_file_definitions` above cleared the file's postings, so the
261 // freshness mark must drop unconditionally — even for unchanged text —
262 // or a query would trust the now-empty posting lists.
263 self.forget_ref_committed(file.as_ref());
264 }
265
266 /// [`Self::ingest_file`] followed by the file's Phase-1 warm-up
267 /// ([`Self::prepare_file_for_analysis`]): its direct class references are
268 /// resolved and lazy-loaded *now*, at write time, instead of serially at
269 /// the front of the next references / re-analysis read.
270 ///
271 /// This is the host edit-path entry point (rust-analyzer's discipline:
272 /// mutation happens only when text changes; requests are pure reads).
273 /// Lazy loads triggered by the warm-up go through plain
274 /// [`Self::ingest_file`], so faulting in a dependency never cascades into
275 /// preparing *its* dependencies — the load frontier stays one file wide.
276 pub fn ingest_file_prepared(&self, file: Arc<str>, source: Arc<str>) {
277 self.ingest_file(file.clone(), source);
278 self.prepare_file_for_analysis(&file);
279 }
280
281 /// Register `source` as the text of `file` in the salsa input layer **without**
282 /// parsing or running definition collection.
283 ///
284 /// This is the LSP-friendly bulk-population entry point: after a workspace
285 /// scan, callers can feed every discovered file's text to the session
286 /// cheaply (an Arc clone plus a HashMap insert per file). Name resolution
287 /// then happens on demand via [`Self::load_class`], which reads
288 /// the file from disk through the configured [`crate::ClassResolver`] and
289 /// runs definition collection lazily when a class FQCN actually needs to resolve.
290 ///
291 /// Contrast with [`Self::ingest_file`], which eagerly parses, runs definition collection,
292 /// and populates the symbol index. Use `ingest_file` for files the user is
293 /// actively editing (where in-memory text diverges from disk); use
294 /// `set_file_text` for files known only through the workspace scan.
295 ///
296 /// Clears the negative cache: a previously-unresolvable FQCN may now
297 /// resolve if its defining file is among the newly-registered set.
298 pub fn set_file_text(&self, file: Arc<str>, source: Arc<str>) {
299 {
300 let mut guard = self.db.salsa.write();
301 guard.upsert_source_file(file.clone(), source);
302 }
303 self.evict_unresolvable_for_file(&file);
304 }
305
306 /// Bulk-register vendor / library files with HIGH salsa durability.
307 ///
308 /// HIGH-durability files are not expected to change during the session.
309 /// When a LOW-durability project file is edited, salsa can skip O(N)
310 /// dependency verification for every HIGH-durability file, reducing
311 /// `workspace_symbol_index` re-verification cost to O(project files only).
312 ///
313 /// Definition collection runs lazily on first symbol access; no parsing at call time.
314 pub fn set_vendor_files<I>(&self, files: I)
315 where
316 I: IntoIterator<Item = (Arc<str>, Arc<str>)>,
317 {
318 let mut guard = self.db.salsa.write();
319 for (file, source) in files {
320 guard.upsert_source_file_with_durability(file, source, salsa::Durability::HIGH);
321 }
322 }
323
324 /// Build or refresh the `WorkspaceSymbolIndexSingleton` from all currently
325 /// registered files.
326 ///
327 /// After this call, `find_class_like`, `find_function`, and
328 /// `find_global_constant` read `singleton.index(db)` — a single
329 /// `Durability::HIGH` tracked dep — instead of recomputing the full
330 /// O(N_files) dep list via `workspace_symbol_index`. On subsequent
331 /// LOW-durability (project-file) body edits the dep short-circuits in O(1).
332 ///
333 /// Call this once after all vendor + stub + project files have been
334 /// ingested (end of workspace warm-up). Also called automatically by
335 /// [`Self::ingest_file`] when a file's declared names change.
336 pub fn rebuild_workspace_symbol_index(&self) {
337 self.db.salsa.write().rebuild_workspace_symbol_index();
338 }
339
340 /// Bulk variant of [`Self::set_file_text`]. Acquires the salsa write lock
341 /// once for the entire batch instead of once per file.
342 ///
343 /// The intended LSP scan loop is:
344 /// ```text
345 /// let files: Vec<_> = walk_workspace()
346 /// .map(|path| (path, fs::read(&path).unwrap()))
347 /// .collect();
348 /// session.set_workspace_files(files);
349 /// ```
350 /// After this call, every file's source text is known to salsa. No
351 /// parsing has happened yet — Definition collection runs per file on the first
352 /// `load_class` that needs to consult it.
353 pub fn set_workspace_files<I>(&self, files: I)
354 where
355 I: IntoIterator<Item = (Arc<str>, Arc<str>)>,
356 {
357 let registered_paths: Vec<Arc<str>> = {
358 let mut guard = self.db.salsa.write();
359 files
360 .into_iter()
361 .map(|(file, source)| {
362 guard.upsert_source_file(file.clone(), source);
363 file
364 })
365 .collect()
366 };
367 if !registered_paths.is_empty() && self.resolver.is_some() {
368 self.evict_unresolvable_for_files(®istered_paths);
369 }
370 }
371
372 /// The workspace generation epoch — the rust-analyzer-style "are we up to
373 /// date" counter. Bumped whenever a file is added or removed. A consumer
374 /// records this alongside the diagnostics it publishes for a file; when the
375 /// value later advances (background indexing registered more files), those
376 /// files become candidates for re-analysis + re-publish.
377 pub fn index_generation(&self) -> u64 {
378 self.db.salsa.read().workspace_revision_value()
379 }
380
381 /// Index one bounded chunk of `(path, text)` files — the chunked background
382 /// indexing primitive.
383 ///
384 /// For each chunk this: (1) registers the files as `Durability::HIGH` salsa
385 /// inputs in one short write window, (2) parses them to prime the in-process
386 /// and on-disk declaration caches (in parallel when `parallelism ==
387 /// `[`IndexParallelism::Rayon`]; sequentially for wasm / single-thread
388 /// consumers), and (3) merges their declarations into the workspace symbol
389 /// index singleton **incrementally** (no full rebuild) so partially-indexed
390 /// symbols resolve immediately.
391 ///
392 /// The library spawns no thread: the consumer pumps chunks from its own
393 /// driver (LSP worker thread, or one chunk per wasm event-loop tick),
394 /// re-checking higher-priority work between calls. `cancel` is honoured at
395 /// chunk boundaries so an edit can abandon queued indexing cheaply.
396 ///
397 /// **Contract:** index the workspace *incrementally* through this method;
398 /// don't bulk-register the entire file set up front and then index — the
399 /// first call lazily seeds the singleton from the currently-registered set
400 /// (built-in stubs + this chunk), so keeping that initial set small keeps
401 /// the first call cheap. Call [`Self::finalize_index`] once after the last
402 /// chunk to reconcile authoritatively.
403 ///
404 /// **Responsiveness:** parsing / declaration collection happens off the
405 /// salsa write lock (on a snapshot); only the cheap symbol-map merge runs
406 /// under the lock, so the write window per chunk is short and an interactive
407 /// read on another thread blocks at most that long. Note that, per salsa's
408 /// snapshot model, a *cancellable query* in flight on another thread (e.g.
409 /// `hover`, `definition_of`, `FileAnalyzer::analyze`) when this batch takes
410 /// the write lock may unwind with `salsa::Cancelled`; a multi-threaded
411 /// consumer should catch that and retry the request (the rust-analyzer
412 /// pattern). A single-threaded consumer that interleaves requests *between*
413 /// `index_batch` calls never observes cancellation.
414 pub fn index_batch(
415 &self,
416 files: &[(Arc<str>, Arc<str>)],
417 parallelism: crate::IndexParallelism,
418 cancel: &crate::IndexCancel,
419 ) -> crate::IndexBatchOutcome {
420 if files.is_empty() || cancel.is_cancelled() {
421 return crate::IndexBatchOutcome {
422 registered: 0,
423 cancelled: cancel.is_cancelled(),
424 generation: self.index_generation(),
425 };
426 }
427 self.ensure_all_stubs();
428
429 // 1. Register the chunk as HIGH-durability inputs — one short write
430 // window, then release the lock so interactive requests interleave.
431 let sources: Vec<crate::db::SourceFile> = {
432 let mut guard = self.db.salsa.write();
433 files
434 .iter()
435 .map(|(file, source)| {
436 guard.upsert_source_file_with_durability(
437 file.clone(),
438 source.clone(),
439 salsa::Durability::HIGH,
440 )
441 })
442 .collect()
443 };
444 let registered = sources.len();
445
446 if cancel.is_cancelled() {
447 return crate::IndexBatchOutcome {
448 registered,
449 cancelled: true,
450 generation: self.index_generation(),
451 };
452 }
453
454 // Is this the seed chunk (no singleton yet)? If so we must collect decls
455 // for the whole currently-registered set (stubs + this chunk); otherwise
456 // just this chunk.
457 let seed = self
458 .db
459 .salsa
460 .read()
461 .workspace_symbol_index_singleton()
462 .is_none();
463 let snap = self.db.snapshot_db();
464 let to_collect: Vec<crate::db::SourceFile> = if seed {
465 snap.all_source_files()
466 } else {
467 sources.clone()
468 };
469
470 // 2. Collect per-file declarations OFF the write lock (on a snapshot).
471 // This is where parsing happens — crucially NOT while holding the
472 // write lock, so concurrent interactive reads are not blocked for the
473 // parse duration. Also primes the shared parse/disk caches.
474 let collect_one = |db: &crate::db::MirDbStorage, sf: crate::db::SourceFile| {
475 (sf, crate::db::collect_file_declarations(db, sf).clone())
476 };
477 let decls: Vec<(crate::db::SourceFile, crate::db::FileDeclarations)> =
478 if parallelism == crate::IndexParallelism::Rayon {
479 use rayon::prelude::*;
480 to_collect
481 .par_iter()
482 .map_with(snap.clone(), |db, &sf| collect_one(db, sf))
483 .collect()
484 } else {
485 to_collect
486 .iter()
487 .map(|&sf| collect_one(&snap, sf))
488 .collect()
489 };
490 drop(snap);
491
492 if cancel.is_cancelled() {
493 return crate::IndexBatchOutcome {
494 registered,
495 cancelled: true,
496 generation: self.index_generation(),
497 };
498 }
499
500 // 3. Apply to the singleton under a SHORT write window — only cheap map
501 // construction / merge runs here (no parse).
502 {
503 let mut guard = self.db.salsa.write();
504 if guard.workspace_symbol_index_singleton().is_none() {
505 guard.build_workspace_index_from_decls(decls);
506 } else {
507 guard.merge_precomputed_into_workspace_index(&decls);
508 }
509 }
510
511 crate::IndexBatchOutcome {
512 registered,
513 cancelled: cancel.is_cancelled(),
514 generation: self.index_generation(),
515 }
516 }
517
518 /// Authoritative full rebuild of the workspace symbol index. Call once
519 /// after the consumer has pumped every [`Self::index_batch`] chunk (end of
520 /// warm-up) to reconcile the incrementally-merged index against the full
521 /// registered set. Cheap after indexing — every file's declarations are
522 /// already cached.
523 pub fn finalize_index(&self) {
524 self.db.salsa.write().rebuild_workspace_symbol_index();
525 }
526
527 /// Replay disk-cached reference-location postings and subtype-index class
528 /// edges for `files`, so a returning session's find-references /
529 /// goto-implementation queries are answered from the index immediately
530 /// instead of paying the on-demand analysis sweep the first time each
531 /// file is queried (`indexed_references_to`/`indexed_subtype_classes`'s
532 /// freshness pass already handles a miss correctly — this only shortens
533 /// the common warm-start case).
534 ///
535 /// A no-op (per file) unless the disk cache from a *previous* run has an
536 /// entry whose content hash matches `files`' current text: [`Self::with_cache`]/
537 /// [`Self::with_cache_dir`] must be attached, and each file's reference
538 /// locations ([`AnalysisCache`], populated by the CLI batch pipeline) or
539 /// definitions ([`crate::stub_cache::StubSliceCache`], populated by
540 /// [`Self::ingest_file`]/vendor ingestion) must already be on disk from
541 /// some earlier run/tool invocation against this exact content. A first-
542 /// ever run (nothing cached yet) is unaffected — every file simply falls
543 /// through to the existing lazy on-demand paths, same as without this call.
544 ///
545 /// Registers `files` as `Durability::HIGH` salsa inputs (like
546 /// [`Self::index_batch`]) if not already registered. Safe to call
547 /// alongside `index_batch` in any order; both merge into the same
548 /// maintained indexes.
549 pub fn warm_start_files(&self, files: &[(Arc<str>, Arc<str>)]) {
550 let Some(cache) = self.cache.clone() else {
551 return;
552 };
553 let stub_cache = self.db.stub_cache.clone();
554 let php_v = self.php_version.cache_byte();
555
556 // Register the whole bundled stub set up front. A seeded symbol-index
557 // singleton is only sound when no stub registration arrives after the
558 // seed (`ensure_stubs_for_ast` upserts bypass index maintenance and
559 // would leave the new stubs invisible); with everything registered
560 // here, later lazy stub loads are no-ops. Same contract as
561 // `index_batch`.
562 self.ensure_all_stubs();
563
564 {
565 let mut guard = self.db.salsa.write();
566 for (file, text) in files {
567 guard.upsert_source_file_with_durability(
568 file.clone(),
569 text.clone(),
570 salsa::Durability::HIGH,
571 );
572 }
573 }
574
575 // Generation after registration: replayed postings reflect a *prior*
576 // session's workspace, so any later file/symbol add must re-verify
577 // them (the None-output mark below also disables resolved immunity).
578 let commit_gen = self.index_generation();
579
580 // Phase 1: read the disk-cache slices in parallel, off any lock — the
581 // actual I/O cost of a warm-start replay at scale (~0.8-0.9s of a
582 // 3.9s warm boot at 15.4K files serially; this is the `index_batch`
583 // pattern already used elsewhere in this file). Only cheap map
584 // construction/merge runs in phase 2, under a lock.
585 let snap = self.snapshot_db();
586 let hits: Vec<WarmStartHit> = {
587 use rayon::prelude::*;
588 files
589 .par_iter()
590 .map_with(snap.clone(), |db, (file, _)| {
591 // Freshness is keyed on the Arc actually stored on the
592 // input — an upsert against already-registered,
593 // content-equal text keeps the prior Arc (see
594 // `ingest_file`), so read back what's really there rather
595 // than assume identity with the file's own `text`.
596 let sf = db.lookup_source_file(file.as_ref())?;
597 let stored_text = sf.text(db).clone();
598
599 let hex = crate::cache::hash_content(&stored_text);
600 let refs = cache.get(file, &hex).map(|(issues, ref_locs)| {
601 let locs: Vec<RefLoc> = ref_locs
602 .iter()
603 .map(|(symbol, line, col_start, col_end)| RefLoc {
604 symbol_key: Arc::clone(symbol),
605 file: file.clone(),
606 line: *line,
607 col_start: *col_start,
608 col_end: *col_end,
609 })
610 .collect();
611 // Resolved from the cached issue set: a fully-resolved
612 // replay survives the registrations/lazy loads that
613 // follow warm-up instead of being invalidated by the
614 // first generation bump.
615 let resolved = !crate::db::issues_have_unresolved_names(&issues);
616 (locs, resolved)
617 });
618
619 let stub = stub_cache.as_ref().and_then(|stub_cache| {
620 let hash = crate::stub_cache::hash_source(&stored_text);
621 let mut slice = stub_cache.get(file, &hash, php_v)?;
622 crate::stub_cache::prepare_for_ingest(&mut slice);
623 let entries = crate::db::subtype_index::entries_from_slice(&slice);
624 let decls = crate::db::decls_from_slice(&slice, sf);
625 Some((entries, decls))
626 });
627
628 Some(WarmStartHit {
629 file: file.clone(),
630 sf,
631 stored_text,
632 refs,
633 stub,
634 })
635 })
636 .filter_map(|hit| hit)
637 .collect()
638 };
639 drop(snap);
640
641 // Phase 2: apply — only cheap salsa input writes and map merges run
642 // here, under the lock each one already required individually.
643 let mut seed_decls: Vec<(crate::db::SourceFile, crate::db::FileDeclarations)> =
644 Vec::with_capacity(hits.len());
645 for hit in hits {
646 let WarmStartHit {
647 file,
648 sf,
649 stored_text,
650 refs,
651 stub,
652 } = hit;
653 if let Some((locs, resolved)) = refs {
654 self.commit_file_refs(&file, Some(stored_text.clone()), locs, commit_gen, resolved);
655 }
656 if let Some((entries, decls)) = stub {
657 {
658 let guard = self.db.salsa.read();
659 guard.set_file_class_edges(&file, entries);
660 }
661 self.mark_defs_committed(&file, &stored_text);
662 seed_decls.push((sf, decls));
663 }
664 }
665
666 self.seed_workspace_index_from_warm_start(seed_decls);
667 }
668
669 /// Seed the workspace symbol index singleton from warm-start declaration
670 /// projections, so a returning session's first query answers from an O(1)
671 /// map instead of the tracked O(all-files) `workspace_symbol_index` walk
672 /// (~4s at 15K files: one `collect_file_definitions` slice-deserialization
673 /// per file, re-validated after every prepare-loop revision bump).
674 ///
675 /// `covered` holds decls projected from content-hash-valid disk slices.
676 /// Files without a valid slice (changed since last session, plus any stub
677 /// not yet slice-cached) are collected in parallel — real parses, so the
678 /// seed is skipped entirely when the gap is large (a first-ever boot,
679 /// where the parse bill belongs to the background sweep, not startup).
680 fn seed_workspace_index_from_warm_start(
681 &self,
682 covered: Vec<(crate::db::SourceFile, crate::db::FileDeclarations)>,
683 ) {
684 use rustc_hash::FxHashSet;
685 if covered.is_empty() {
686 return;
687 }
688
689 let snap = self.snapshot_db();
690 let all = snap.all_source_files();
691 let covered_set: FxHashSet<crate::db::SourceFile> =
692 covered.iter().map(|(sf, _)| *sf).collect();
693 let missing: Vec<crate::db::SourceFile> = all
694 .iter()
695 .copied()
696 .filter(|sf| !covered_set.contains(sf))
697 .collect();
698
699 // Gap ceiling: beyond this the "fill" is a workspace-scale parse and
700 // seeding stops being a warm start. 1024 absorbs a large changed set
701 // plus the bundled stubs; the quarter bound keeps tiny workspaces
702 // seedable even when most files changed.
703 let threshold = 1024usize.max(all.len() / 4);
704 if missing.len() > threshold {
705 return;
706 }
707
708 // Parse/collect the gap off the write lock, in parallel, on one
709 // snapshot — the `index_batch` pattern. `collect_file_declarations`
710 // is disk-slice-accelerated itself, so "missing" here often means a
711 // cheap deserialization rather than a parse.
712 let gap_decls: Vec<(crate::db::SourceFile, crate::db::FileDeclarations)> = {
713 use rayon::prelude::*;
714 missing
715 .par_iter()
716 .map_with(snap.clone(), |db, &sf| {
717 (sf, crate::db::collect_file_declarations(db, sf).clone())
718 })
719 .collect()
720 };
721 drop(snap);
722
723 let mut decls = covered;
724 decls.extend(gap_decls);
725
726 let mut guard = self.db.salsa.write();
727 if guard.workspace_symbol_index_singleton().is_none() {
728 guard.build_workspace_index_from_decls(decls);
729 } else {
730 // Something (e.g. a vendor-eager `index_batch`) seeded first; its
731 // singleton is already maintained incrementally. Merge only files
732 // it hasn't seen — `merge_precomputed_into_workspace_index` skips
733 // files already snapshotted.
734 guard.merge_precomputed_into_workspace_index(&decls);
735 }
736 }
737
738 /// Reconcile the symbol-index singleton with mirror-only text writes.
739 ///
740 /// Plain `upsert_source_file_with_durability` calls (an LSP host
741 /// mirroring watcher-driven external edits or new files) bypass
742 /// `ingest_file`'s incremental index maintenance; those files accumulate
743 /// in a pending set while a singleton exists. Query entry points call
744 /// this first so the singleton is never consulted stale. Declaration
745 /// memos are pre-warmed on a snapshot (parallel, off the write lock);
746 /// the per-file merge under the lock is then a memo hit.
747 pub fn settle_workspace_index(&self) {
748 if self.db.salsa.read().index_pending_is_empty() {
749 return;
750 }
751 let pending = self.db.salsa.read().take_index_pending();
752 if pending.is_empty() {
753 return;
754 }
755 let snap = self.snapshot_db();
756 let sfs: Vec<crate::db::SourceFile> = pending
757 .iter()
758 .filter_map(|p| snap.lookup_source_file(p.as_ref()))
759 .collect();
760 // Best-effort pre-warm; a concurrent write may cancel it, in which
761 // case the update below collects under the lock (single file, rare).
762 let _ = salsa::Cancelled::catch(std::panic::AssertUnwindSafe(|| {
763 use rayon::prelude::*;
764 sfs.par_iter().for_each_with(snap.clone(), |db, &sf| {
765 let _ = crate::db::collect_file_declarations(db, sf);
766 });
767 }));
768 drop(snap);
769 let mut guard = self.db.salsa.write();
770 // `update_workspace_index_for_file` clones the singleton maps per
771 // call; for a bulk arrival (branch switch) one full rebuild — memo
772 // validations plus a single map build, since the decls were just
773 // pre-warmed above — beats N clones under the write lock.
774 if sfs.len() > 32 {
775 guard.rebuild_workspace_symbol_index();
776 return;
777 }
778 for sf in sfs {
779 if !guard.update_workspace_index_for_file(sf) {
780 guard.rebuild_workspace_symbol_index();
781 }
782 }
783 }
784
785 /// Drop a file's contribution to the session: codebase definitions,
786 /// reference locations, salsa input handle, cache entry, and outgoing
787 /// reverse-dependency edges. Cache entries of *dependent* files are
788 /// also evicted (cross-file invalidation).
789 ///
790 /// Use this when a file is closed by the consumer, or before a re-ingest
791 /// of substantially changed content. (Plain re-ingest via
792 /// [`Self::ingest_file`] also drops old definitions, but does not
793 /// remove the salsa input handle — call this for full cleanup.)
794 pub fn invalidate_file(&self, file: &str) {
795 {
796 let mut guard = self.db.salsa.write();
797 guard.remove_file_definitions(file);
798 guard.remove_source_file(file);
799 guard.clear_file_class_edges(file);
800 }
801 self.forget_ref_committed(file);
802 self.forget_defs_committed(file);
803 // Outgoing structural edges disappear from the derived graph
804 // automatically: the file is no longer in `source_file_paths()`, so
805 // `dependency_graph()` stops iterating it.
806 // Clear stale symbol tracking for this file — it's fully gone.
807 self.stale_defined_symbols.write().remove(file);
808 self.last_ingested_symbols.write().remove(file);
809 // Declarations this file provided are gone; other prepared files may
810 // now need their warm-up re-run to lazy-load replacements.
811 self.forget_prepared(file);
812 self.bump_prepare_generation();
813 if let Some(cache) = &self.cache {
814 cache.update_reverse_deps_for_file(file, &HashSet::default());
815 cache.evict_with_dependents(&[file.to_string()]);
816 }
817 // The file is gone; cache entries that previously mapped to it stay
818 // unresolvable until the file (or another with matching symbols) is
819 // ingested again. Selective evict mirrors the ingest path.
820 self.evict_unresolvable_for_file(file);
821 // Vendor files are static in the eager-index model — closing a project
822 // buffer never evicts them (no per-file pinning). Memory is bounded by
823 // the LRU on `collect_file_definitions` and the parse cache instead.
824 }
825
826 /// Number of files currently tracked in this session's salsa input set.
827 /// Stable across reads; useful for diagnostics and memory bounds checks.
828 pub fn tracked_file_count(&self) -> usize {
829 let guard = self.db.salsa.read();
830 guard.source_file_count()
831 }
832
833 // -----------------------------------------------------------------------
834 // Read-only codebase queries
835 //
836 // All take a brief lock to clone the db, then run the lookup against the
837 // owned snapshot — concurrent edits proceed without blocking.
838 // -----------------------------------------------------------------------
839}