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        for (defs, _hash, _hard_err, _surface) in file_defs {
196            for issue in defs.issues.iter() {
197                if matches!(issue.kind, mir_issues::IssueKind::ParseError { .. })
198                    && issue.severity == mir_issues::Severity::Error
199                {
200                    files_with_parse_errors.insert(issue.location.file.clone());
201                }
202            }
203            all_issues.extend(Arc::unwrap_or_clone(defs.issues));
204        }
205        let _t_ingest = _t0.elapsed();
206
207        // ---- Pre-warm collect_file_definitions for project files -------------
208        {
209            let db_prewarm = {
210                let guard = self.db.salsa.read();
211                (**guard).clone()
212            };
213            let project_source_files: Vec<SourceFile> = {
214                let guard = self.db.salsa.read();
215                parsed_files
216                    .iter()
217                    .filter_map(|p| (**guard).lookup_source_file(&p.file))
218                    .collect()
219            };
220            project_source_files
221                .into_par_iter()
222                .for_each_with(db_prewarm, |db, sf| {
223                    let _ = collect_file_definitions(db as &dyn MirDatabase, sf);
224                });
225        }
226        let _t_prewarm_ms = (_t0.elapsed() - _t_ingest).as_secs_f64() * 1000.0;
227
228        // Fold the freshly-registered project files into the workspace symbol
229        // index singleton. The singleton may have been built from vendor before
230        // this run (CLI indexes vendor before analyze_paths); since adding files
231        // no longer nulls it, project classes would otherwise be invisible to
232        // find_class_like and reported as false UndefinedClass.
233        self.refresh_workspace_index();
234
235        // ---- Lazy-load unknown classes via PSR-4 ----------------------------
236        let _t_before_lazy = _t0.elapsed();
237        if let Some(psr4) = self.psr4.clone() {
238            self.lazy_load_missing_classes(psr4, php_version, &mut all_issues);
239        }
240        let _t_lazyload_ms = (_t0.elapsed() - _t_before_lazy).as_secs_f64() * 1000.0;
241
242        // ---- Class-level checks ---------------------------------------------
243        let analyzed_file_set: HashSet<Arc<str>> =
244            file_data.iter().map(|(f, _)| f.clone()).collect();
245        let _t_class_analyzer = std::time::Instant::now();
246        {
247            let class_db = {
248                let guard = self.db.salsa.read();
249                (**guard).clone()
250            };
251            let class_issues = crate::class::ClassAnalyzer::with_files(
252                &class_db,
253                analyzed_file_set.clone(),
254                &file_data,
255            )
256            .analyze_all();
257            all_issues.extend(class_issues);
258        }
259        let _t_class_analyzer_ms = _t_class_analyzer.elapsed().as_secs_f64() * 1000.0;
260
261        let _t_class_checks = _t0.elapsed();
262
263        let mut db_main = {
264            let guard = self.db.salsa.read();
265            (**guard).clone()
266        };
267        // All index mutation for the body pass is done (lazy_load_missing_classes
268        // + refresh ran above; lazy_load_from_body_issues runs *after* this pass
269        // on a separate db). Freeze the index on this ephemeral clone so each
270        // find_class_like borrows it instead of cloning the singleton's three
271        // Arcs per call — the per-worker `map_with` clone bumps the refcount once.
272        db_main.freeze_workspace_index();
273
274        // ---- Body analysis: function/method bodies in parallel --------------
275        type BodyResult = (
276            Arc<str>,
277            Vec<Issue>,
278            Vec<crate::symbol::ResolvedSymbol>,
279            Vec<RefLoc>,
280        );
281        let body_results: Vec<BodyResult> = parsed_files
282            .par_iter()
283            .filter(|parsed| !files_with_parse_errors.contains(&parsed.file))
284            .map_with(db_main, |db, parsed| {
285                let mut driver = BodyAnalyzer::new(&*db as &dyn MirDatabase, php_version);
286                // Diagnostics-only consumers never read the symbol vecs —
287                // don't build them (a Type clone per reference) at all.
288                driver.collect_symbols = !opts.skip_symbols;
289                let (issues, symbols) = if let Some(cache) = &self.cache {
290                    let h = content_hexes
291                        .get(parsed.file.as_ref())
292                        .cloned()
293                        .unwrap_or_else(|| hash_content(parsed.source()));
294                    if let Some((cached_issues, ref_locs)) = cache.get(&parsed.file, &h) {
295                        // Cache replay: rebuild the file's complete reference
296                        // set straight from the cached tuples — no pending-
297                        // buffer detour. Symbol keys are Arc-shared with the
298                        // cache entry, so this allocates no strings.
299                        let locs: Vec<RefLoc> = ref_locs
300                            .iter()
301                            .map(|(symbol, line, col_start, col_end)| RefLoc {
302                                symbol_key: Arc::clone(symbol),
303                                file: parsed.file.clone(),
304                                line: *line,
305                                col_start: *col_start,
306                                col_end: *col_end,
307                            })
308                            .collect();
309                        return (
310                            parsed.file.clone(),
311                            cached_issues.to_vec(),
312                            Vec::new(),
313                            locs,
314                        );
315                    }
316                    let (issues, symbols) = driver.analyze_bodies(
317                        parsed.owned(),
318                        parsed.file.clone(),
319                        parsed.source(),
320                        parsed.source_map(),
321                    );
322                    let pending = db.take_pending_ref_locs();
323                    let cache_locs: Arc<[crate::cache::CachedRefLoc]> = pending
324                        .iter()
325                        .map(|r| (Arc::clone(&r.symbol_key), r.line, r.col_start, r.col_end))
326                        .collect();
327                    let surface = surface_hashes
328                        .get(parsed.file.as_ref())
329                        .cloned()
330                        .unwrap_or_default();
331                    cache.put(
332                        &parsed.file,
333                        h,
334                        surface,
335                        issues.as_slice().into(),
336                        cache_locs,
337                    );
338                    if let Some(cb) = &opts.on_file_done {
339                        cb();
340                    }
341                    let symbols = if opts.skip_symbols {
342                        Vec::new()
343                    } else {
344                        symbols
345                    };
346                    return (parsed.file.clone(), issues, symbols, pending);
347                } else {
348                    driver.analyze_bodies(
349                        parsed.owned(),
350                        parsed.file.clone(),
351                        parsed.source(),
352                        parsed.source_map(),
353                    )
354                };
355                let pending = db.take_pending_ref_locs();
356                if let Some(cb) = &opts.on_file_done {
357                    cb();
358                }
359                // Drop the per-file symbol vec inside the worker when the
360                // consumer opted out — the orchestrator never accumulates.
361                let symbols = if opts.skip_symbols {
362                    Vec::new()
363                } else {
364                    symbols
365                };
366                (parsed.file.clone(), issues, symbols, pending)
367            })
368            .collect();
369
370        let _t_body_analysis = _t0.elapsed();
371
372        // Serial commit with replace semantics: each file's output (or cache
373        // replay) is its complete reference set, so stale entries from a
374        // prior run cannot survive an append.
375        let mut all_symbols = Vec::new();
376        {
377            let guard = self.db.salsa.read();
378            for (file, issues, symbols, ref_locs) in body_results {
379                all_issues.extend(issues);
380                all_symbols.extend(symbols);
381                guard.set_file_reference_locations(file.as_ref(), ref_locs);
382            }
383        }
384
385        // ---- Post-analysis lazy loading: FQCNs used without `use` imports ------
386        if let Some(psr4) = self.psr4.clone() {
387            self.lazy_load_from_body_issues(
388                psr4,
389                php_version,
390                &file_data,
391                &files_with_parse_errors,
392                &mut all_issues,
393                &mut all_symbols,
394                opts.skip_symbols,
395            );
396        }
397
398        // ---- Build reverse dep graph and persist it for the next run ---------
399        // Must run AFTER `commit_reference_locations_batch` (above): the graph's
400        // call-site / instantiation / inferred-return edges are derived from the
401        // committed reference-location map. Built any earlier (the salsa db is
402        // fresh each session) that map is empty, so only structural edges
403        // (parent/interface/trait/declared types) survive — and any dependent
404        // reachable only through a call site or inferred type goes stale.
405        if topology_changed {
406            if let Some(cache) = &self.cache {
407                let db_snapshot = {
408                    let guard = self.db.salsa.read();
409                    (**guard).clone()
410                };
411                let rev = build_reverse_deps(&db_snapshot);
412                cache.set_reverse_deps(rev);
413            }
414        }
415
416        // Persist cache hits/misses to disk
417        if let Some(cache) = &self.cache {
418            cache.flush();
419        }
420
421        // ---- Dead-code detection -------------------------------------------
422        if opts.should_run_dead_code() {
423            let salsa = self.snapshot_db();
424            let _t_dead_code = std::time::Instant::now();
425            let dead_code_issues =
426                crate::dead_code::DeadCodeAnalyzer::with_files(&salsa, analyzed_file_set.clone())
427                    .analyze();
428            all_issues.extend(dead_code_issues);
429            if std::env::var("MIR_TIMING").is_ok() {
430                eprintln!(
431                    "[timing] dead_code_analyzer={:.0}ms",
432                    _t_dead_code.elapsed().as_secs_f64() * 1000.0
433                );
434            }
435        }
436
437        let _t_total = _t0.elapsed();
438        if std::env::var("MIR_TIMING").is_ok() {
439            eprintln!(
440                "[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",
441                _t_stubs.as_secs_f64() * 1000.0,
442                (_t_read - _t_stubs).as_secs_f64() * 1000.0,
443                (_t_salsa_reg - _t_read).as_secs_f64() * 1000.0,
444                (_t_collect_defs - _t_salsa_reg).as_secs_f64() * 1000.0,
445                (_t_ingest - _t_collect_defs).as_secs_f64() * 1000.0,
446                (_t_class_checks - _t_ingest).as_secs_f64() * 1000.0,
447                _t_prewarm_ms,
448                _t_lazyload_ms,
449                _t_class_analyzer_ms,
450                (_t_body_analysis - _t_class_checks).as_secs_f64() * 1000.0,
451                _t_total.as_secs_f64() * 1000.0,
452            );
453        }
454
455        opts.apply(&mut all_issues);
456        let analyzed_files_vec: Vec<Arc<str>> = analyzed_file_set.iter().cloned().collect();
457        self.apply_suppressions_and_emit_unused(&mut all_issues, &analyzed_files_vec);
458        if let Some(dump) = crate::metrics::dump() {
459            eprintln!("{dump}");
460        }
461
462        // ---- Build workspace symbol index singleton -------------------------
463        {
464            let mut guard = self.db.salsa.write();
465            guard.rebuild_workspace_symbol_index();
466        }
467
468        AnalysisResult::build(all_issues, rustc_hash::FxHashMap::default(), all_symbols)
469    }
470    /// Re-analyze a single file (definition collection + body analysis) within the batch context.
471    ///
472    /// Mirrors the old `ProjectAnalyzer::re_analyze_file` cache-aware path.
473    /// Use [`Self::reanalyze_dependents`] for LSP-style per-file flows that
474    /// don't need batch options.
475    pub fn re_analyze_file(
476        &self,
477        file_path: &str,
478        new_content: &str,
479        opts: &BatchOptions,
480    ) -> AnalysisResult {
481        let php_version = self.batch_php_version(opts);
482
483        // Fast path: content unchanged and cache has a valid entry.
484        if let Some(cache) = &self.cache {
485            let h = hash_content(new_content);
486            if let Some((cached_issues, ref_locs)) = cache.get(file_path, &h) {
487                let mut issues = cached_issues.to_vec();
488                let file: Arc<str> = Arc::from(file_path);
489                // Replace semantics: the cached set is the file's complete
490                // reference set, so stale entries from a prior version are
491                // cleared rather than appended over.
492                let locs: Vec<RefLoc> = ref_locs
493                    .iter()
494                    .map(|(symbol, line, col_start, col_end)| RefLoc {
495                        symbol_key: Arc::clone(symbol),
496                        file: file.clone(),
497                        line: *line,
498                        col_start: *col_start,
499                        col_end: *col_end,
500                    })
501                    .collect();
502                let guard = self.db.salsa.read();
503                guard.set_file_reference_locations(file_path, locs);
504                drop(guard);
505                opts.apply(&mut issues);
506                self.apply_suppressions_and_emit_unused(&mut issues, std::slice::from_ref(&file));
507                return AnalysisResult::build(issues, HashMap::default(), Vec::new());
508            }
509        }
510
511        let file: Arc<str> = Arc::from(file_path);
512
513        {
514            let mut guard = self.db.salsa.write();
515            guard.remove_file_definitions(file_path);
516        }
517
518        let file_defs = {
519            let mut guard = self.db.salsa.write();
520            let salsa_file = guard.upsert_source_file(file.clone(), Arc::from(new_content));
521            collect_file_definitions(&**guard, salsa_file)
522        };
523
524        let mut all_issues: Vec<Issue> = Arc::unwrap_or_clone(file_defs.issues.clone());
525
526        {
527            let mut guard = self.db.salsa.write();
528            if guard.workspace_symbol_index_singleton().is_some() {
529                if let Some(sf) = guard.lookup_source_file(file.as_ref()) {
530                    if guard.file_declarations_changed(sf) {
531                        guard.rebuild_workspace_symbol_index();
532                    }
533                }
534            }
535        }
536
537        let (symbols, surface_hash) = {
538            let guard = self.db.salsa.write();
539
540            let parsed = php_rs_parser::parse(new_content);
541            let surface_hash = surface_fingerprint(new_content, &parsed.program);
542
543            let has_hard_errors = parsed.errors.iter().any(crate::parser::is_hard_parse_error);
544            let symbols = if !has_hard_errors {
545                let db_ref: &dyn MirDatabase = &**guard;
546                let driver = BodyAnalyzer::new(db_ref, php_version);
547                let (body_issues, symbols) = driver.analyze_bodies(
548                    &parsed.program,
549                    file.clone(),
550                    new_content,
551                    &parsed.source_map,
552                );
553                all_issues.extend(body_issues);
554                let pending = guard.take_pending_ref_locs();
555                guard.set_file_reference_locations(file.as_ref(), pending);
556                symbols
557            } else {
558                Vec::new()
559            };
560            (symbols, surface_hash)
561        };
562
563        // Bake inline-suppression marks in *before* caching: suppression is a
564        // pure function of file content (and the cache key hashes content), so
565        // the cached issues should already carry their marks. The cache-hit
566        // branch above replays this file's source without re-registering the
567        // `SourceFile` input, so the db-backed post-filter cannot recompute
568        // marks there — caching the canonical result is what keeps a fresh
569        // process honoring `@mir-ignore` on an unchanged file.
570        mark_suppressed(
571            &mut all_issues,
572            &crate::suppression::SuppressionMap::from_source(new_content),
573        );
574
575        if let Some(cache) = &self.cache {
576            let h = hash_content(new_content);
577            // Cascade to dependents only when this file's cross-file surface
578            // changed; a body-only edit to a declared-return callable leaves
579            // their results valid. Unknown (absent/empty) stored surface cascades.
580            let surface_changed = match cache.surface_hash(file_path) {
581                Some(stored) if !stored.is_empty() => stored != surface_hash,
582                _ => true,
583            };
584            if surface_changed {
585                cache.evict_with_dependents(&[file_path.to_string()]);
586            }
587            let db = self.snapshot_db();
588            let ref_locs = extract_reference_locations(&db, &file);
589            cache.put(
590                file_path,
591                h,
592                surface_hash,
593                all_issues.as_slice().into(),
594                ref_locs,
595            );
596        }
597
598        opts.apply(&mut all_issues);
599        // Emit `UnusedSuppress` for named suppressions that matched nothing —
600        // present on the cache-hit branch above and on `analyze_paths`, but
601        // missing here, so every non-cached re-analysis (the actual
602        // incremental/LSP-edit pipeline) silently dropped this diagnostic.
603        // `mark_suppressed` above already baked suppression marks into what
604        // gets cached; this only adds the (uncached, always-fresh) unused-
605        // suppression pass on top, mirroring the cache-hit branch's order.
606        self.apply_suppressions_and_emit_unused(&mut all_issues, std::slice::from_ref(&file));
607        AnalysisResult::build(all_issues, HashMap::default(), symbols)
608    }
609
610    /// Collect type definitions only from `paths` into the codebase
611    /// without analyzing method bodies or emitting issues. Used to load
612    /// vendor types.
613    ///
614    /// When a disk-backed cache is attached, per-file `StubSlice` results
615    /// from previous runs are reused on a content-hash match, eliminating
616    /// the parse + definition-collection step. Cache misses run the normal
617    /// pipeline and write back so subsequent runs hit.
618    pub fn collect_definitions(&self, paths: &[PathBuf]) {
619        let _timing = std::env::var("MIR_TIMING").is_ok();
620        let _t0 = std::time::Instant::now();
621
622        let php_v = self.php_version.cache_byte();
623
624        struct FileEntry {
625            file: Arc<str>,
626            src: Arc<str>,
627            hash: [u8; 32],
628            cached: Option<mir_codebase::definitions::StubSlice>,
629        }
630        let entries: Vec<FileEntry> = paths
631            .par_iter()
632            .filter_map(|path| {
633                let src = std::fs::read_to_string(path).ok()?;
634                let file: Arc<str> = Arc::from(path.to_string_lossy().as_ref());
635                let src: Arc<str> = Arc::from(src);
636                let hash = hash_source(&src);
637                let cached = self.db.stub_cache.as_ref().and_then(|c| {
638                    let mut slice = c.get(&file, &hash, php_v)?;
639                    prepare_for_ingest(&mut slice);
640                    Some(slice)
641                });
642                Some(FileEntry {
643                    file,
644                    src,
645                    hash,
646                    cached,
647                })
648            })
649            .collect();
650        let _t_read = _t0.elapsed();
651
652        let source_files: Vec<SourceFile> = {
653            let mut guard = self.db.salsa.write();
654            entries
655                .iter()
656                .map(|e| {
657                    guard.upsert_source_file_with_durability(
658                        e.file.clone(),
659                        e.src.clone(),
660                        salsa::Durability::HIGH,
661                    )
662                })
663                .collect()
664        };
665        let _t_reg = _t0.elapsed();
666
667        let db_pass1 = {
668            let guard = self.db.salsa.read();
669            (**guard).clone()
670        };
671        let stub_cache = self.db.stub_cache.clone();
672        let prepared: Vec<mir_codebase::definitions::StubSlice> = entries
673            .into_par_iter()
674            .zip(source_files.into_par_iter())
675            .map_with(db_pass1, |db, (mut entry, salsa_file)| {
676                if let Some(slice) = entry.cached.take() {
677                    let slice_arc = Arc::new(slice);
678                    db.parse_cache()
679                        .insert(entry.hash, php_v, Arc::clone(&slice_arc));
680                    return (*slice_arc).clone();
681                }
682                let defs = collect_file_definitions(&*db, salsa_file);
683                if let Some(cache) = stub_cache.as_ref() {
684                    cache.put(&entry.file, &entry.hash, php_v, &defs.slice);
685                }
686                (*defs.slice).clone()
687            })
688            .collect();
689        let _t_collect = _t0.elapsed();
690        drop(prepared);
691        let _t_ingest = _t0.elapsed();
692
693        if _timing {
694            let (hits, misses) = self.stub_cache_stats();
695            eprintln!(
696                "[vendor] read={:.0}ms reg={:.0}ms collect={:.0}ms ingest={:.0}ms total={:.0}ms (cache hits={hits} misses={misses})",
697                _t_read.as_secs_f64() * 1000.0,
698                (_t_reg - _t_read).as_secs_f64() * 1000.0,
699                (_t_collect - _t_reg).as_secs_f64() * 1000.0,
700                (_t_ingest - _t_collect).as_secs_f64() * 1000.0,
701                _t_ingest.as_secs_f64() * 1000.0,
702            );
703        }
704
705        {
706            let mut guard = self.db.salsa.write();
707            guard.rebuild_workspace_symbol_index();
708        }
709
710        crate::collector::print_collector_stats();
711    }
712}