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