Skip to main content

mir_analyzer/batch/
run.rs

1use super::*;
2
3impl AnalysisSession {
4    /// Run the full batch analysis pipeline on a set of file paths.
5    pub fn analyze_paths(&self, paths: &[PathBuf], opts: &BatchOptions) -> AnalysisResult {
6        let php_version = self.batch_php_version(opts);
7        let mut all_issues = Vec::new();
8        let _t0 = std::time::Instant::now();
9
10        // ---- Load PHP built-in stubs (before definition collection so user code can override)
11        self.load_batch_stubs(php_version);
12        // Index vendor autoload.files (global function/constant helpers such as
13        // Laravel's `confirm()`, `select()`, etc.) before body analysis so
14        // calls to these functions resolve rather than emitting UndefinedFunction.
15        self.ensure_vendor_eager_functions();
16        let _t_stubs = _t0.elapsed();
17
18        // ---- Read files in parallel ----------------------------------
19        let parsed_files: Vec<ParsedProjectFile> = paths
20            .par_iter()
21            .filter_map(|path| match std::fs::read_to_string(path) {
22                Ok(src) => {
23                    let file = Arc::from(path.to_string_lossy().as_ref());
24                    Some(ParsedProjectFile::new(file, Arc::from(src)))
25                }
26                Err(e) => {
27                    eprintln!("Cannot read {}: {}", path.display(), e);
28                    None
29                }
30            })
31            .collect();
32        let _t_read = _t0.elapsed();
33
34        let file_data: Vec<(Arc<str>, Arc<str>)> = parsed_files
35            .iter()
36            .map(|parsed| (parsed.file.clone(), parsed.source.clone()))
37            .collect();
38
39        // ---- Detect files deleted since the last run ------------------------
40        // A file analyzed previously but now gone from disk leaves dependents
41        // holding results that assume its definitions still exist. (A file
42        // merely absent from this run's path set but still on disk is NOT a
43        // deletion — checking disk existence avoids evicting during partial-path
44        // analysis.) Drop their own entries here; dependent eviction happens
45        // below, once per-file surface fingerprints are known.
46        //
47        // `topology_changed` tracks whether this run touched the cache (any file
48        // changed, added, or removed). When nothing changed, the reverse-dep
49        // graph loaded from disk is still accurate, so the rebuild + full
50        // cache.bin rewrite at the end of this pass is skipped.
51        let mut topology_changed = false;
52        let mut removed_files: Vec<String> = Vec::new();
53        if let Some(cache) = &self.cache {
54            let current: rustc_hash::FxHashSet<&str> =
55                file_data.iter().map(|(f, _)| f.as_ref()).collect();
56            removed_files = cache
57                .cached_files()
58                .into_iter()
59                .filter(|f| !current.contains(f.as_str()) && !std::path::Path::new(f).exists())
60                .collect();
61            for f in &removed_files {
62                cache.evict(f);
63            }
64            topology_changed = !removed_files.is_empty();
65        }
66
67        // ---- Register Salsa source inputs for incremental follow-up calls ----
68        {
69            let mut guard = self.db.salsa.write();
70            for parsed in &parsed_files {
71                guard.upsert_source_file(parsed.file.clone(), parsed.source.clone());
72            }
73        }
74        let _t_salsa_reg = _t0.elapsed();
75
76        // ---- Definition collection from the already-parsed AST -------
77        // Returns (FileDefinitions, content_hash, has_hard_parse_errors) so we
78        // can prime the parse cache before the pre-warm loop below.
79        type Pass1Entry = (FileDefinitions, [u8; 32], bool, String);
80        let file_defs: Vec<Pass1Entry> = parsed_files
81            .par_iter()
82            .map(|parsed| {
83                let content_hash = hash_source(parsed.source());
84                let has_hard_parse_errors = parsed
85                    .errors()
86                    .iter()
87                    .any(crate::parser::is_hard_parse_error);
88                let mut all_issues: Vec<Issue> = parsed
89                    .errors()
90                    .iter()
91                    .filter(|err| !crate::parser::is_spurious_reserved_class_error(err))
92                    .map(|err| {
93                        crate::parser::parse_error_to_issue(
94                            err,
95                            &parsed.file,
96                            parsed.source(),
97                            parsed.source_map(),
98                        )
99                    })
100                    .collect();
101                let collector = crate::collector::DefinitionCollector::new_for_slice(
102                    parsed.file.clone(),
103                    parsed.source(),
104                    parsed.source_map(),
105                );
106                let (mut slice, collector_issues) = collector.collect_slice(parsed.owned());
107                all_issues.extend(collector_issues);
108                mir_codebase::definitions::deduplicate_params_in_slice(&mut slice);
109                let defs = FileDefinitions {
110                    slice: Arc::new(slice),
111                    issues: Arc::new(all_issues),
112                };
113                let surface_hash = surface_fingerprint(parsed.source(), parsed.owned());
114                (defs, content_hash, has_hard_parse_errors, surface_hash)
115            })
116            .collect();
117        let _t_collect_defs = _t0.elapsed();
118
119        // Pair each file with its cross-file surface fingerprint (par_iter().map
120        // preserves order, so file_defs aligns with parsed_files).
121        let surface_hashes: HashMap<Arc<str>, String> = parsed_files
122            .iter()
123            .zip(file_defs.iter())
124            .map(|(parsed, (_defs, _h, _e, surface))| (parsed.file.clone(), surface.clone()))
125            .collect();
126
127        // Hex content hashes derived from the pass-1 digests, so the
128        // content-changed pass and the body pass below don't re-hash every
129        // source file (BLAKE3 over all bytes, twice).
130        let content_hexes: HashMap<Arc<str>, String> = parsed_files
131            .iter()
132            .zip(file_defs.iter())
133            .map(|(parsed, (_defs, hash, _e, _surface))| {
134                (
135                    parsed.file.clone(),
136                    blake3::Hash::from(*hash).to_hex().to_string(),
137                )
138            })
139            .collect();
140
141        // ---- Cross-file invalidation: evict dependents whose surface changed --
142        // A file whose content changed re-analyzes itself regardless (its body
143        // pass misses the cache below). It cascades to dependents only when its
144        // declaration-level surface changed: a body-only edit to a declared-
145        // return callable leaves every dependent's result intact. A missing or
146        // pre-firewall (empty) stored surface is treated as unknown and cascades.
147        if let Some(cache) = &self.cache {
148            let content_changed: Vec<String> = file_data
149                .iter()
150                .filter_map(|(f, _src)| {
151                    let valid = content_hexes.get(f).is_some_and(|h| cache.is_valid(f, h));
152                    if valid {
153                        None
154                    } else {
155                        Some(f.to_string())
156                    }
157                })
158                .collect();
159            if !content_changed.is_empty() {
160                topology_changed = true;
161            }
162            let mut seeds: Vec<String> = content_changed
163                .into_iter()
164                .filter(|f| {
165                    let new_surface = surface_hashes
166                        .get(f.as_str())
167                        .map(String::as_str)
168                        .unwrap_or("");
169                    // Keep (cascade) unless the stored surface is known and equal.
170                    !matches!(
171                        cache.surface_hash(f),
172                        Some(stored) if !stored.is_empty() && stored == new_surface
173                    )
174                })
175                .collect();
176            seeds.extend(removed_files.iter().cloned());
177            if !seeds.is_empty() {
178                cache.evict_with_dependents(&seeds);
179            }
180        }
181
182        // Prime the in-process parse cache so the pre-warm loop below avoids
183        // re-parsing every project file through collect_file_definitions.
184        {
185            let guard = self.db.salsa.read();
186            let php_v = php_version.cache_byte();
187            for (defs, hash, has_hard_parse_errors, _surface) in &file_defs {
188                if !*has_hard_parse_errors {
189                    guard.prime_parse_cache(*hash, php_v, Arc::clone(&defs.slice));
190                }
191            }
192        }
193
194        let mut files_with_parse_errors: HashSet<Arc<str>> = HashSet::default();
195        {
196            // Commit subtype-index class edges alongside issue collection —
197            // parity with `ingest_file`'s single-file path, so goto-implementation
198            // sees implementors from a batch/vendor run without waiting for
199            // each file to be individually touched by an on-demand commit path.
200            let guard = self.db.salsa.read();
201            for (parsed, (defs, _hash, _hard_err, _surface)) in parsed_files.iter().zip(file_defs) {
202                for issue in defs.issues.iter() {
203                    if matches!(issue.kind, mir_issues::IssueKind::ParseError { .. })
204                        && issue.severity == mir_issues::Severity::Error
205                    {
206                        files_with_parse_errors.insert(issue.location.file.clone());
207                    }
208                }
209                let entries = crate::db::subtype_index::entries_from_slice(&defs.slice);
210                guard.set_file_class_edges(&parsed.file, entries);
211                all_issues.extend(Arc::unwrap_or_clone(defs.issues));
212            }
213        }
214        let _t_ingest = _t0.elapsed();
215
216        // ---- Pre-warm collect_file_definitions for project files -------------
217        {
218            let db_prewarm = {
219                let guard = self.db.salsa.read();
220                (**guard).clone()
221            };
222            let project_source_files: Vec<SourceFile> = {
223                let guard = self.db.salsa.read();
224                parsed_files
225                    .iter()
226                    .filter_map(|p| (**guard).lookup_source_file(&p.file))
227                    .collect()
228            };
229            project_source_files
230                .into_par_iter()
231                .for_each_with(db_prewarm, |db, sf| {
232                    let _ = collect_file_definitions(db as &dyn MirDatabase, sf);
233                });
234        }
235        let _t_prewarm_ms = (_t0.elapsed() - _t_ingest).as_secs_f64() * 1000.0;
236
237        // Fold the freshly-registered project files into the workspace symbol
238        // index singleton. The singleton may have been built from vendor before
239        // this run (CLI indexes vendor before analyze_paths); since adding files
240        // no longer nulls it, project classes would otherwise be invisible to
241        // find_class_like and reported as false UndefinedClass.
242        self.refresh_workspace_index();
243
244        // ---- Lazy-load unknown classes via PSR-4 ----------------------------
245        let _t_before_lazy = _t0.elapsed();
246        if let Some(psr4) = self.psr4.clone() {
247            self.lazy_load_missing_classes(psr4, php_version, &mut all_issues);
248        }
249        let _t_lazyload_ms = (_t0.elapsed() - _t_before_lazy).as_secs_f64() * 1000.0;
250
251        // ---- Class-level checks ---------------------------------------------
252        let analyzed_file_set: HashSet<Arc<str>> =
253            file_data.iter().map(|(f, _)| f.clone()).collect();
254        let _t_class_analyzer = std::time::Instant::now();
255        {
256            let class_db = {
257                let guard = self.db.salsa.read();
258                (**guard).clone()
259            };
260            let class_issues = crate::class::ClassAnalyzer::with_files(
261                &class_db,
262                analyzed_file_set.clone(),
263                &file_data,
264            )
265            .analyze_all();
266            all_issues.extend(class_issues);
267        }
268        let _t_class_analyzer_ms = _t_class_analyzer.elapsed().as_secs_f64() * 1000.0;
269
270        let _t_class_checks = _t0.elapsed();
271
272        let mut db_main = {
273            let guard = self.db.salsa.read();
274            (**guard).clone()
275        };
276        // All index mutation for the body pass is done (lazy_load_missing_classes
277        // + refresh ran above; lazy_load_from_body_issues runs *after* this pass
278        // on a separate db). Freeze the index on this ephemeral clone so each
279        // find_class_like borrows it instead of cloning the singleton's three
280        // Arcs per call — the per-worker `map_with` clone bumps the refcount once.
281        db_main.freeze_workspace_index();
282
283        // ---- Body analysis: function/method bodies in parallel --------------
284        type BodyResult = (
285            Arc<str>,
286            Vec<Issue>,
287            Vec<crate::symbol::ResolvedSymbol>,
288            Vec<RefLoc>,
289        );
290        let body_results: Vec<BodyResult> = parsed_files
291            .par_iter()
292            .filter(|parsed| !files_with_parse_errors.contains(&parsed.file))
293            .map_with(db_main, |db, parsed| {
294                let mut driver = BodyAnalyzer::new(&*db as &dyn MirDatabase, php_version);
295                // Diagnostics-only consumers never read the symbol vecs —
296                // don't build them (a Type clone per reference) at all.
297                driver.collect_symbols = !opts.skip_symbols;
298                let (issues, symbols) = if let Some(cache) = &self.cache {
299                    let h = content_hexes
300                        .get(parsed.file.as_ref())
301                        .cloned()
302                        .unwrap_or_else(|| hash_content(parsed.source()));
303                    if let Some((cached_issues, ref_locs)) = cache.get(&parsed.file, &h) {
304                        // Cache replay: rebuild the file's complete reference
305                        // set straight from the cached tuples — no pending-
306                        // buffer detour. Symbol keys are Arc-shared with the
307                        // cache entry, so this allocates no strings.
308                        let locs: Vec<RefLoc> = ref_locs
309                            .iter()
310                            .map(|(symbol, line, col_start, col_end)| RefLoc {
311                                symbol_key: Arc::clone(symbol),
312                                file: parsed.file.clone(),
313                                line: *line,
314                                col_start: *col_start,
315                                col_end: *col_end,
316                            })
317                            .collect();
318                        return (
319                            parsed.file.clone(),
320                            cached_issues.to_vec(),
321                            Vec::new(),
322                            locs,
323                        );
324                    }
325                    let (issues, symbols) = driver.analyze_bodies(
326                        parsed.owned(),
327                        parsed.file.clone(),
328                        parsed.source(),
329                        parsed.source_map(),
330                    );
331                    let pending = db.take_pending_ref_locs();
332                    let cache_locs: Arc<[crate::cache::CachedRefLoc]> = pending
333                        .iter()
334                        .map(|r| (Arc::clone(&r.symbol_key), r.line, r.col_start, r.col_end))
335                        .collect();
336                    let surface = surface_hashes
337                        .get(parsed.file.as_ref())
338                        .cloned()
339                        .unwrap_or_default();
340                    cache.put(
341                        &parsed.file,
342                        h,
343                        surface,
344                        issues.as_slice().into(),
345                        cache_locs,
346                    );
347                    if let Some(cb) = &opts.on_file_done {
348                        cb();
349                    }
350                    let symbols = if opts.skip_symbols {
351                        Vec::new()
352                    } else {
353                        symbols
354                    };
355                    return (parsed.file.clone(), issues, symbols, pending);
356                } else {
357                    driver.analyze_bodies(
358                        parsed.owned(),
359                        parsed.file.clone(),
360                        parsed.source(),
361                        parsed.source_map(),
362                    )
363                };
364                let pending = db.take_pending_ref_locs();
365                if let Some(cb) = &opts.on_file_done {
366                    cb();
367                }
368                // Drop the per-file symbol vec inside the worker when the
369                // consumer opted out — the orchestrator never accumulates.
370                let symbols = if opts.skip_symbols {
371                    Vec::new()
372                } else {
373                    symbols
374                };
375                (parsed.file.clone(), issues, symbols, pending)
376            })
377            .collect();
378
379        let _t_body_analysis = _t0.elapsed();
380
381        // Serial commit with replace semantics: each file's output (or cache
382        // replay) is its complete reference set, so stale entries from a
383        // prior run cannot survive an append.
384        let mut all_symbols = Vec::new();
385        {
386            let guard = self.db.salsa.read();
387            for (file, issues, symbols, ref_locs) in body_results {
388                all_issues.extend(issues);
389                all_symbols.extend(symbols);
390                guard.set_file_reference_locations(file.as_ref(), ref_locs);
391            }
392        }
393
394        // ---- Post-analysis lazy loading: FQCNs used without `use` imports ------
395        if let Some(psr4) = self.psr4.clone() {
396            self.lazy_load_from_body_issues(
397                psr4,
398                php_version,
399                &file_data,
400                &files_with_parse_errors,
401                &mut all_issues,
402                &mut all_symbols,
403                opts.skip_symbols,
404            );
405        }
406
407        // ---- Build reverse dep graph and persist it for the next run ---------
408        // Must run AFTER `commit_reference_locations_batch` (above): the graph's
409        // call-site / instantiation / inferred-return edges are derived from the
410        // committed reference-location map. Built any earlier (the salsa db is
411        // fresh each session) that map is empty, so only structural edges
412        // (parent/interface/trait/declared types) survive — and any dependent
413        // reachable only through a call site or inferred type goes stale.
414        if topology_changed {
415            if let Some(cache) = &self.cache {
416                let db_snapshot = {
417                    let guard = self.db.salsa.read();
418                    (**guard).clone()
419                };
420                let rev = build_reverse_deps(&db_snapshot);
421                cache.set_reverse_deps(rev);
422            }
423        }
424
425        // Persist cache hits/misses to disk
426        if let Some(cache) = &self.cache {
427            cache.flush();
428        }
429
430        // ---- Dead-code detection -------------------------------------------
431        if opts.should_run_dead_code() {
432            let salsa = self.snapshot_db();
433            let _t_dead_code = std::time::Instant::now();
434            let dead_code_issues =
435                crate::dead_code::DeadCodeAnalyzer::with_files(&salsa, analyzed_file_set.clone())
436                    .analyze();
437            all_issues.extend(dead_code_issues);
438            if std::env::var("MIR_TIMING").is_ok() {
439                eprintln!(
440                    "[timing] dead_code_analyzer={:.0}ms",
441                    _t_dead_code.elapsed().as_secs_f64() * 1000.0
442                );
443            }
444        }
445
446        let _t_total = _t0.elapsed();
447        if std::env::var("MIR_TIMING").is_ok() {
448            eprintln!(
449                "[timing] stubs={:.0}ms read={:.0}ms salsa_reg={:.0}ms collect_defs={:.0}ms ingest={:.0}ms class_checks={:.0}ms (prewarm={:.0}ms lazy_load={:.0}ms class_analyzer={:.0}ms) body_analysis={:.0}ms total={:.0}ms",
450                _t_stubs.as_secs_f64() * 1000.0,
451                (_t_read - _t_stubs).as_secs_f64() * 1000.0,
452                (_t_salsa_reg - _t_read).as_secs_f64() * 1000.0,
453                (_t_collect_defs - _t_salsa_reg).as_secs_f64() * 1000.0,
454                (_t_ingest - _t_collect_defs).as_secs_f64() * 1000.0,
455                (_t_class_checks - _t_ingest).as_secs_f64() * 1000.0,
456                _t_prewarm_ms,
457                _t_lazyload_ms,
458                _t_class_analyzer_ms,
459                (_t_body_analysis - _t_class_checks).as_secs_f64() * 1000.0,
460                _t_total.as_secs_f64() * 1000.0,
461            );
462        }
463
464        opts.apply(&mut all_issues);
465        let analyzed_files_vec: Vec<Arc<str>> = analyzed_file_set.iter().cloned().collect();
466        self.apply_suppressions_and_emit_unused(&mut all_issues, &analyzed_files_vec);
467        if let Some(dump) = crate::metrics::dump() {
468            eprintln!("{dump}");
469        }
470
471        // ---- Build workspace symbol index singleton -------------------------
472        {
473            let mut guard = self.db.salsa.write();
474            guard.rebuild_workspace_symbol_index();
475        }
476
477        AnalysisResult::build(all_issues, rustc_hash::FxHashMap::default(), all_symbols)
478    }
479    /// Re-analyze a single file (definition collection + body analysis) within the batch context.
480    ///
481    /// Mirrors the old `ProjectAnalyzer::re_analyze_file` cache-aware path.
482    /// Use [`Self::reanalyze_dependents`] for LSP-style per-file flows that
483    /// don't need batch options.
484    pub fn re_analyze_file(
485        &self,
486        file_path: &str,
487        new_content: &str,
488        opts: &BatchOptions,
489    ) -> AnalysisResult {
490        let php_version = self.batch_php_version(opts);
491
492        // Fast path: content unchanged and cache has a valid entry.
493        if let Some(cache) = &self.cache {
494            let h = hash_content(new_content);
495            if let Some((cached_issues, ref_locs)) = cache.get(file_path, &h) {
496                let mut issues = cached_issues.to_vec();
497                let file: Arc<str> = Arc::from(file_path);
498                // Replace semantics: the cached set is the file's complete
499                // reference set, so stale entries from a prior version are
500                // cleared rather than appended over.
501                let locs: Vec<RefLoc> = ref_locs
502                    .iter()
503                    .map(|(symbol, line, col_start, col_end)| RefLoc {
504                        symbol_key: Arc::clone(symbol),
505                        file: file.clone(),
506                        line: *line,
507                        col_start: *col_start,
508                        col_end: *col_end,
509                    })
510                    .collect();
511                let guard = self.db.salsa.read();
512                guard.set_file_reference_locations(file_path, locs);
513                drop(guard);
514                opts.apply(&mut issues);
515                self.apply_suppressions_and_emit_unused(&mut issues, std::slice::from_ref(&file));
516                return AnalysisResult::build(issues, HashMap::default(), Vec::new());
517            }
518        }
519
520        let file: Arc<str> = Arc::from(file_path);
521
522        {
523            let mut guard = self.db.salsa.write();
524            guard.remove_file_definitions(file_path);
525        }
526
527        let file_defs = {
528            let mut guard = self.db.salsa.write();
529            let salsa_file = guard.upsert_source_file(file.clone(), Arc::from(new_content));
530            collect_file_definitions(&**guard, salsa_file)
531        };
532
533        let mut all_issues: Vec<Issue> = Arc::unwrap_or_clone(file_defs.issues.clone());
534
535        {
536            let mut guard = self.db.salsa.write();
537            if guard.workspace_symbol_index_singleton().is_some() {
538                if let Some(sf) = guard.lookup_source_file(file.as_ref()) {
539                    if guard.file_declarations_changed(sf) {
540                        guard.rebuild_workspace_symbol_index();
541                    }
542                }
543            }
544        }
545
546        let (symbols, surface_hash) = {
547            let guard = self.db.salsa.write();
548
549            let parsed = php_rs_parser::parse(new_content);
550            let surface_hash = surface_fingerprint(new_content, &parsed.program);
551
552            let has_hard_errors = parsed.errors.iter().any(crate::parser::is_hard_parse_error);
553            let symbols = if !has_hard_errors {
554                let db_ref: &dyn MirDatabase = &**guard;
555                let driver = BodyAnalyzer::new(db_ref, php_version);
556                let (body_issues, symbols) = driver.analyze_bodies(
557                    &parsed.program,
558                    file.clone(),
559                    new_content,
560                    &parsed.source_map,
561                );
562                all_issues.extend(body_issues);
563                let pending = guard.take_pending_ref_locs();
564                guard.set_file_reference_locations(file.as_ref(), pending);
565                symbols
566            } else {
567                Vec::new()
568            };
569            (symbols, surface_hash)
570        };
571
572        // Bake inline-suppression marks in *before* caching: suppression is a
573        // pure function of file content (and the cache key hashes content), so
574        // the cached issues should already carry their marks. The cache-hit
575        // branch above replays this file's source without re-registering the
576        // `SourceFile` input, so the db-backed post-filter cannot recompute
577        // marks there — caching the canonical result is what keeps a fresh
578        // process honoring `@mir-ignore` on an unchanged file.
579        mark_suppressed(
580            &mut all_issues,
581            &crate::suppression::SuppressionMap::from_source(new_content),
582        );
583
584        if let Some(cache) = &self.cache {
585            let h = hash_content(new_content);
586            // Cascade to dependents only when this file's cross-file surface
587            // changed; a body-only edit to a declared-return callable leaves
588            // their results valid. Unknown (absent/empty) stored surface cascades.
589            let surface_changed = match cache.surface_hash(file_path) {
590                Some(stored) if !stored.is_empty() => stored != surface_hash,
591                _ => true,
592            };
593            if surface_changed {
594                cache.evict_with_dependents(&[file_path.to_string()]);
595            }
596            let db = self.snapshot_db();
597            let ref_locs = extract_reference_locations(&db, &file);
598            cache.put(
599                file_path,
600                h,
601                surface_hash,
602                all_issues.as_slice().into(),
603                ref_locs,
604            );
605        }
606
607        opts.apply(&mut all_issues);
608        // Emit `UnusedSuppress` for named suppressions that matched nothing —
609        // present on the cache-hit branch above and on `analyze_paths`, but
610        // missing here, so every non-cached re-analysis (the actual
611        // incremental/LSP-edit pipeline) silently dropped this diagnostic.
612        // `mark_suppressed` above already baked suppression marks into what
613        // gets cached; this only adds the (uncached, always-fresh) unused-
614        // suppression pass on top, mirroring the cache-hit branch's order.
615        self.apply_suppressions_and_emit_unused(&mut all_issues, std::slice::from_ref(&file));
616        AnalysisResult::build(all_issues, HashMap::default(), symbols)
617    }
618
619    /// Collect type definitions only from `paths` into the codebase
620    /// without analyzing method bodies or emitting issues. Used to load
621    /// vendor types.
622    ///
623    /// When a disk-backed cache is attached, per-file `StubSlice` results
624    /// from previous runs are reused on a content-hash match, eliminating
625    /// the parse + definition-collection step. Cache misses run the normal
626    /// pipeline and write back so subsequent runs hit.
627    pub fn collect_definitions(&self, paths: &[PathBuf]) {
628        let _timing = std::env::var("MIR_TIMING").is_ok();
629        let _t0 = std::time::Instant::now();
630
631        let php_v = self.php_version.cache_byte();
632
633        struct FileEntry {
634            file: Arc<str>,
635            src: Arc<str>,
636            hash: [u8; 32],
637            cached: Option<mir_codebase::definitions::StubSlice>,
638        }
639        let entries: Vec<FileEntry> = paths
640            .par_iter()
641            .filter_map(|path| {
642                let src = std::fs::read_to_string(path).ok()?;
643                let file: Arc<str> = Arc::from(path.to_string_lossy().as_ref());
644                let src: Arc<str> = Arc::from(src);
645                let hash = hash_source(&src);
646                let cached = self.db.stub_cache.as_ref().and_then(|c| {
647                    let mut slice = c.get(&file, &hash, php_v)?;
648                    prepare_for_ingest(&mut slice);
649                    Some(slice)
650                });
651                Some(FileEntry {
652                    file,
653                    src,
654                    hash,
655                    cached,
656                })
657            })
658            .collect();
659        let _t_read = _t0.elapsed();
660
661        let source_files: Vec<SourceFile> = {
662            let mut guard = self.db.salsa.write();
663            entries
664                .iter()
665                .map(|e| {
666                    guard.upsert_source_file_with_durability(
667                        e.file.clone(),
668                        e.src.clone(),
669                        salsa::Durability::HIGH,
670                    )
671                })
672                .collect()
673        };
674        let _t_reg = _t0.elapsed();
675
676        let db_pass1 = {
677            let guard = self.db.salsa.read();
678            (**guard).clone()
679        };
680        let stub_cache = self.db.stub_cache.clone();
681        let prepared: Vec<(Arc<str>, mir_codebase::definitions::StubSlice)> = entries
682            .into_par_iter()
683            .zip(source_files.into_par_iter())
684            .map_with(db_pass1, |db, (mut entry, salsa_file)| {
685                if let Some(slice) = entry.cached.take() {
686                    let slice_arc = Arc::new(slice);
687                    db.parse_cache()
688                        .insert(entry.hash, php_v, Arc::clone(&slice_arc));
689                    return (entry.file.clone(), (*slice_arc).clone());
690                }
691                let defs = collect_file_definitions(&*db, salsa_file);
692                if let Some(cache) = stub_cache.as_ref() {
693                    cache.put(&entry.file, &entry.hash, php_v, &defs.slice);
694                }
695                (entry.file.clone(), (*defs.slice).clone())
696            })
697            .collect();
698        let _t_collect = _t0.elapsed();
699        // Commit subtype-index class edges for the vendor tree: without this,
700        // goto-implementation can't surface implementors that live only in
701        // vendor/ (index_batch's project-file warm sweep has the same gap for
702        // project files, but no StubSlice is cheaply available there — see
703        // the persistence work tracked separately).
704        {
705            let guard = self.db.salsa.read();
706            for (file, slice) in &prepared {
707                let entries = crate::db::subtype_index::entries_from_slice(slice);
708                guard.set_file_class_edges(file, entries);
709            }
710        }
711        drop(prepared);
712        let _t_ingest = _t0.elapsed();
713
714        if _timing {
715            let (hits, misses) = self.stub_cache_stats();
716            eprintln!(
717                "[vendor] read={:.0}ms reg={:.0}ms collect={:.0}ms ingest={:.0}ms total={:.0}ms (cache hits={hits} misses={misses})",
718                _t_read.as_secs_f64() * 1000.0,
719                (_t_reg - _t_read).as_secs_f64() * 1000.0,
720                (_t_collect - _t_reg).as_secs_f64() * 1000.0,
721                (_t_ingest - _t_collect).as_secs_f64() * 1000.0,
722                _t_ingest.as_secs_f64() * 1000.0,
723            );
724        }
725
726        {
727            let mut guard = self.db.salsa.write();
728            guard.rebuild_workspace_symbol_index();
729        }
730
731        crate::collector::print_collector_stats();
732    }
733}