Skip to main content

mir_codebase/
storage.rs

1use std::sync::Arc;
2
3use indexmap::IndexMap;
4use mir_types::{Location, Name, Type};
5use rustc_hash::FxHashMap;
6use serde::{Deserialize, Serialize};
7
8// ---------------------------------------------------------------------------
9// Interned common types for deduplication
10// ---------------------------------------------------------------------------
11
12/// Interned Type types for common parameter/property types.
13/// Deduplicates allocations when thousands of parameters share types like `string`, `int`, etc.
14mod interned_types {
15    use super::*;
16    use std::sync::OnceLock;
17
18    fn intern_string() -> Arc<Type> {
19        Arc::new(Type::string())
20    }
21
22    fn intern_int() -> Arc<Type> {
23        Arc::new(Type::int())
24    }
25
26    fn intern_float() -> Arc<Type> {
27        Arc::new(Type::float())
28    }
29
30    fn intern_bool() -> Arc<Type> {
31        Arc::new(Type::bool())
32    }
33
34    fn intern_mixed() -> Arc<Type> {
35        Arc::new(Type::mixed())
36    }
37
38    fn intern_null() -> Arc<Type> {
39        Arc::new(Type::null())
40    }
41
42    fn intern_void() -> Arc<Type> {
43        Arc::new(Type::void())
44    }
45
46    static STRING: OnceLock<Arc<Type>> = OnceLock::new();
47    static INT: OnceLock<Arc<Type>> = OnceLock::new();
48    static FLOAT: OnceLock<Arc<Type>> = OnceLock::new();
49    static BOOL: OnceLock<Arc<Type>> = OnceLock::new();
50    static MIXED: OnceLock<Arc<Type>> = OnceLock::new();
51    static NULL: OnceLock<Arc<Type>> = OnceLock::new();
52    static VOID: OnceLock<Arc<Type>> = OnceLock::new();
53
54    pub fn string() -> Arc<Type> {
55        STRING.get_or_init(intern_string).clone()
56    }
57
58    pub fn int() -> Arc<Type> {
59        INT.get_or_init(intern_int).clone()
60    }
61
62    pub fn float() -> Arc<Type> {
63        FLOAT.get_or_init(intern_float).clone()
64    }
65
66    pub fn bool() -> Arc<Type> {
67        BOOL.get_or_init(intern_bool).clone()
68    }
69
70    pub fn mixed() -> Arc<Type> {
71        MIXED.get_or_init(intern_mixed).clone()
72    }
73
74    pub fn null() -> Arc<Type> {
75        NULL.get_or_init(intern_null).clone()
76    }
77
78    pub fn void() -> Arc<Type> {
79        VOID.get_or_init(intern_void).clone()
80    }
81
82    /// Global content-keyed `Arc<Type>` interner. Any structurally-identical
83    /// Type is shared as a single Arc across the session.
84    ///
85    /// Why: PHP codebases re-declare a small set of type shapes thousands of
86    /// times — `string|null` return types, `int` params, `array<string, mixed>`
87    /// property types. Without interning, each declaration allocates its own
88    /// `Arc<Type>` plus the inline `SmallVec<[Atomic; 2]>` and any boxed
89    /// `Atomic` payloads. With interning, only the first occurrence allocates.
90    ///
91    /// Trade-off: every `intern_or_wrap` call hashes + does one DashMap lookup.
92    /// Hashing a `Type` is cheap (SmallVec, small atomics) — measured cost is
93    /// well below the alloc-savings benefit on real workloads.
94    static GLOBAL_UNION_INTERN: std::sync::OnceLock<dashmap::DashMap<Type, Arc<Type>>> =
95        std::sync::OnceLock::new();
96
97    fn global_intern_table() -> &'static dashmap::DashMap<Type, Arc<Type>> {
98        GLOBAL_UNION_INTERN.get_or_init(dashmap::DashMap::default)
99    }
100
101    /// Try to intern a Type if it matches a common type, otherwise wrap in Arc.
102    pub fn intern_or_wrap(union: Type) -> Arc<Type> {
103        // Fast path 1: single-atomic scalar — covered by `OnceLock` constants.
104        // Avoids any DashMap traffic for the most common case.
105        if union.types.len() == 1 && !union.possibly_undefined && !union.from_docblock {
106            match &union.types[0] {
107                mir_types::Atomic::TString => return string(),
108                mir_types::Atomic::TInt => return int(),
109                mir_types::Atomic::TFloat => return float(),
110                mir_types::Atomic::TBool => return bool(),
111                mir_types::Atomic::TMixed => return mixed(),
112                mir_types::Atomic::TNull => return null(),
113                mir_types::Atomic::TVoid => return void(),
114                _ => {}
115            }
116        }
117        // Fast path 2: empty Type — also a common case (e.g. unresolved
118        // return type). Don't pollute the intern table with these.
119        if union.types.is_empty() {
120            return Arc::new(union);
121        }
122        // Global path: dedup against any previously-seen identical Type.
123        let table = global_intern_table();
124        if let Some(existing) = table.get(&union) {
125            return Arc::clone(existing.value());
126        }
127        let arc = Arc::new(union.clone());
128        // `insert` semantics: if a parallel thread beat us, its Arc wins.
129        // The lookup-before-insert race is benign — both Arcs are content-
130        // equal — but we still want to share the canonical one going forward.
131        match table.entry(union) {
132            dashmap::mapref::entry::Entry::Occupied(o) => Arc::clone(o.get()),
133            dashmap::mapref::entry::Entry::Vacant(v) => {
134                v.insert(Arc::clone(&arc));
135                arc
136            }
137        }
138    }
139}
140
141// ---------------------------------------------------------------------------
142// Shared primitives
143// ---------------------------------------------------------------------------
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
146pub enum Visibility {
147    Public,
148    Protected,
149    Private,
150}
151
152impl Visibility {
153    pub fn is_at_least(&self, required: Visibility) -> bool {
154        *self <= required
155    }
156}
157
158impl std::fmt::Display for Visibility {
159    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160        match self {
161            Visibility::Public => write!(f, "public"),
162            Visibility::Protected => write!(f, "protected"),
163            Visibility::Private => write!(f, "private"),
164        }
165    }
166}
167
168#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
169pub struct TemplateParam {
170    pub name: Name,
171    pub bound: Option<Type>,
172    /// The entity (class or function FQN) that declared this template param.
173    pub defining_entity: Name,
174    pub variance: mir_types::Variance,
175}
176
177#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
178pub struct FnParam {
179    pub name: Name,
180    /// Parameter type. Stored as `Option<Arc<Type>>` to enable deduplication of
181    /// common types across parameters. Many parameters share types like `string`,
182    /// `int`, `bool`, etc., so interning via Arc saves allocations.
183    #[serde(
184        deserialize_with = "deserialize_param_type",
185        serialize_with = "serialize_param_type"
186    )]
187    pub ty: Option<Arc<Type>>,
188    /// Whether this parameter has a default value. During analysis, defaults are
189    /// never used for their value — only for marking parameters as optional.
190    pub has_default: bool,
191    pub is_variadic: bool,
192    pub is_byref: bool,
193    pub is_optional: bool,
194}
195
196impl std::hash::Hash for FnParam {
197    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
198        self.name.hash(state);
199        self.has_default.hash(state);
200        self.is_variadic.hash(state);
201        self.is_byref.hash(state);
202        self.is_optional.hash(state);
203        // Hash the type value (not the Arc pointer) so that two FnParams with
204        // equal types (PartialEq) always produce the same hash, even when they
205        // are backed by different Arc allocations.
206        self.ty.as_deref().hash(state);
207    }
208}
209
210// Serde helpers to transparently convert between Option<Type> and Option<Arc<Type>>
211fn deserialize_param_type<'de, D>(deserializer: D) -> Result<Option<Arc<Type>>, D::Error>
212where
213    D: serde::Deserializer<'de>,
214{
215    Option::<Type>::deserialize(deserializer).map(|opt| opt.map(interned_types::intern_or_wrap))
216}
217
218fn serialize_param_type<S>(value: &Option<Arc<Type>>, serializer: S) -> Result<S::Ok, S::Error>
219where
220    S: serde::Serializer,
221{
222    let opt = value.as_ref().map(|arc| (**arc).clone());
223    opt.serialize(serializer)
224}
225
226fn deserialize_return_type<'de, D>(deserializer: D) -> Result<Option<Arc<Type>>, D::Error>
227where
228    D: serde::Deserializer<'de>,
229{
230    Option::<Type>::deserialize(deserializer).map(|opt| opt.map(interned_types::intern_or_wrap))
231}
232
233fn serialize_return_type<S>(value: &Option<Arc<Type>>, serializer: S) -> Result<S::Ok, S::Error>
234where
235    S: serde::Serializer,
236{
237    let opt = value.as_ref().map(|arc| (**arc).clone());
238    opt.serialize(serializer)
239}
240
241fn deserialize_params<'de, D>(deserializer: D) -> Result<Arc<[FnParam]>, D::Error>
242where
243    D: serde::Deserializer<'de>,
244{
245    Vec::<FnParam>::deserialize(deserializer).map(|v| Arc::from(v.into_boxed_slice()))
246}
247
248fn default_imports() -> Arc<FxHashMap<Name, Name>> {
249    Arc::new(FxHashMap::default())
250}
251
252/// Deserialize imports map. Supports both new (Name-keyed) and legacy
253/// (String-keyed) on-disk formats — older `cache.bin` files have plain
254/// `HashMap<String, String>`. Either way, we intern at load time so the
255/// in-memory representation is always `Arc<FxHashMap<Name, Name>>`.
256fn deserialize_imports<'de, D>(deserializer: D) -> Result<Arc<FxHashMap<Name, Name>>, D::Error>
257where
258    D: serde::Deserializer<'de>,
259{
260    let raw = FxHashMap::<String, String>::deserialize(deserializer)?;
261    let mut out: FxHashMap<Name, Name> =
262        FxHashMap::with_capacity_and_hasher(raw.len(), Default::default());
263    for (k, v) in raw {
264        out.insert(Name::new(&k), Name::new(&v));
265    }
266    Ok(Arc::new(out))
267}
268
269/// Serialize imports as the legacy `HashMap<String, String>` shape so disk
270/// caches written by this version remain compatible with readers that haven't
271/// been recompiled yet (and vice-versa).
272fn serialize_imports<S>(
273    value: &Arc<FxHashMap<Name, Name>>,
274    serializer: S,
275) -> Result<S::Ok, S::Error>
276where
277    S: serde::Serializer,
278{
279    use serde::ser::SerializeMap;
280    let mut map = serializer.serialize_map(Some(value.len()))?;
281    for (k, v) in value.iter() {
282        map.serialize_entry(k.as_str(), v.as_str())?;
283    }
284    map.end()
285}
286
287fn serialize_params<S>(value: &Arc<[FnParam]>, serializer: S) -> Result<S::Ok, S::Error>
288where
289    S: serde::Serializer,
290{
291    value.as_ref().serialize(serializer)
292}
293
294/// Helper to wrap Option<Type> in interned Arc<Type>.
295pub fn wrap_param_type(ty: Option<Type>) -> Option<Arc<Type>> {
296    ty.map(interned_types::intern_or_wrap)
297}
298
299/// Helper to wrap return type Option<Type> in interned Arc<Type>.
300pub fn wrap_return_type(ty: Option<Type>) -> Option<Arc<Type>> {
301    ty.map(interned_types::intern_or_wrap)
302}
303
304// ---------------------------------------------------------------------------
305// Assertion — `@psalm-assert`, `@psalm-assert-if-true`, etc.
306// ---------------------------------------------------------------------------
307
308#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
309pub enum AssertionKind {
310    Assert,
311    AssertIfTrue,
312    AssertIfFalse,
313}
314
315#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
316pub struct Assertion {
317    pub kind: AssertionKind,
318    pub param: Arc<str>,
319    pub ty: Type,
320}
321
322// ---------------------------------------------------------------------------
323// MethodDef
324// ---------------------------------------------------------------------------
325
326#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
327pub struct MethodDef {
328    pub name: Arc<str>,
329    pub fqcn: Arc<str>,
330    #[serde(
331        deserialize_with = "deserialize_params",
332        serialize_with = "serialize_params"
333    )]
334    pub params: Arc<[FnParam]>,
335    /// Type from annotation (`@return` / native type hint). `None` means unannotated.
336    /// Stored as `Option<Arc<Type>>` to enable deduplication of common return types
337    /// (e.g., `void`, `string`, `mixed`, `bool`) across thousands of methods.
338    #[serde(
339        deserialize_with = "deserialize_return_type",
340        serialize_with = "serialize_return_type"
341    )]
342    pub return_type: Option<Arc<Type>>,
343    /// Type inferred from body analysis. Stored as `Option<Arc<Type>>` (8 B) rather
344    /// than inline `Option<Type>` (176 B, no niche) — inference is now demand-driven
345    /// via salsa (`inferred_*_return_type_demand`), so this field is a rarely/never
346    /// populated fallback; shrinking it saves ~168 B on every MethodDef.
347    #[serde(
348        deserialize_with = "deserialize_return_type",
349        serialize_with = "serialize_return_type"
350    )]
351    pub inferred_return_type: Option<Arc<Type>>,
352    pub visibility: Visibility,
353    pub is_static: bool,
354    pub is_abstract: bool,
355    pub is_final: bool,
356    pub is_constructor: bool,
357    pub template_params: Vec<TemplateParam>,
358    pub assertions: Vec<Assertion>,
359    pub throws: Vec<Arc<str>>,
360    pub deprecated: Option<Arc<str>>,
361    pub is_internal: bool,
362    pub is_pure: bool,
363    /// True when the method has the `#[Override]` PHP attribute.
364    #[serde(default)]
365    pub is_override: bool,
366    pub location: Option<Location>,
367    /// Plain-text description from the docblock (text before `@tag` lines).
368    /// Used for hover info.
369    #[serde(default)]
370    pub docstring: Option<Arc<str>>,
371    /// True for methods added via `@method` docblock annotations. Virtual
372    /// methods must not be required as concrete interface implementations.
373    #[serde(default)]
374    pub is_virtual: bool,
375}
376
377impl MethodDef {
378    pub fn effective_return_type(&self) -> Option<&Type> {
379        self.return_type
380            .as_deref()
381            .or(self.inferred_return_type.as_deref())
382    }
383}
384
385// ---------------------------------------------------------------------------
386// PropertyDef
387// ---------------------------------------------------------------------------
388
389#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
390pub struct PropertyDef {
391    pub name: Arc<str>,
392    pub ty: Option<Type>,
393    pub inferred_ty: Option<Type>,
394    pub visibility: Visibility,
395    pub is_static: bool,
396    pub is_readonly: bool,
397    pub default: Option<Type>,
398    pub location: Option<Location>,
399    /// `@deprecated` docblock annotation, if present.
400    #[serde(default)]
401    pub deprecated: Option<Arc<str>>,
402}
403
404// ---------------------------------------------------------------------------
405// ConstantDef
406// ---------------------------------------------------------------------------
407
408#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
409pub struct ConstantDef {
410    pub name: Arc<str>,
411    pub ty: Type,
412    pub visibility: Option<Visibility>,
413    #[serde(default)]
414    pub is_final: bool,
415    pub location: Option<Location>,
416    /// `@deprecated` docblock annotation, if present.
417    #[serde(default)]
418    pub deprecated: Option<Arc<str>>,
419}
420
421// ---------------------------------------------------------------------------
422// ClassDef
423// ---------------------------------------------------------------------------
424
425#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
426pub struct ClassDef {
427    pub fqcn: Arc<str>,
428    pub short_name: Arc<str>,
429    pub parent: Option<Arc<str>>,
430    pub interfaces: Vec<Arc<str>>,
431    pub traits: Vec<Arc<str>>,
432    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
433    pub own_properties: IndexMap<Arc<str>, PropertyDef>,
434    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
435    #[serde(default)]
436    pub mixins: Vec<Arc<str>>,
437    pub template_params: Vec<TemplateParam>,
438    /// Type arguments from `@extends ParentClass<T1, T2>` — maps parent's template params to concrete types.
439    pub extends_type_args: Vec<Type>,
440    /// Type arguments from `@implements Interface<T1, T2>`.
441    #[serde(default)]
442    pub implements_type_args: Vec<(Arc<str>, Vec<Type>)>,
443    pub is_abstract: bool,
444    pub is_final: bool,
445    pub is_readonly: bool,
446    pub deprecated: Option<Arc<str>>,
447    pub is_internal: bool,
448    pub location: Option<Location>,
449    /// Per-`use` statement locations for each used trait: `(fqcn, location)` in
450    /// declaration order, parallel to `traits`.  Absent from older serialized
451    /// slices; defaults to empty.
452    #[serde(default)]
453    pub trait_use_locations: Vec<(Arc<str>, Location)>,
454    /// Type aliases declared on this class via `@psalm-type` / `@phpstan-type`.
455    #[serde(default)]
456    pub type_aliases: FxHashMap<Arc<str>, Type>,
457    /// Raw import-type declarations (`(local_name, original_name, from_class)`) — resolved during finalization.
458    #[serde(default)]
459    pub pending_import_types: Vec<(Arc<str>, Arc<str>, Arc<str>)>,
460}
461
462impl ClassDef {
463    pub fn get_method(&self, name: &str) -> Option<&MethodDef> {
464        // PHP method names are case-insensitive; caller should pass lowercase name.
465        // Only searches own_methods — inherited method resolution is done by
466        // `db::lookup_method_in_chain`.
467        self.own_methods.get(name).map(Arc::as_ref).or_else(|| {
468            self.own_methods
469                .iter()
470                .find(|(k, _)| k.as_ref().eq_ignore_ascii_case(name))
471                .map(|(_, v)| v.as_ref())
472        })
473    }
474
475    pub fn get_property(&self, name: &str) -> Option<&PropertyDef> {
476        self.own_properties.get(name)
477    }
478}
479
480// ---------------------------------------------------------------------------
481// InterfaceDef
482// ---------------------------------------------------------------------------
483
484#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
485pub struct InterfaceDef {
486    pub fqcn: Arc<str>,
487    pub short_name: Arc<str>,
488    pub extends: Vec<Arc<str>>,
489    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
490    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
491    pub template_params: Vec<TemplateParam>,
492    pub location: Option<Location>,
493    /// `@deprecated` docblock annotation, if present.
494    #[serde(default)]
495    pub deprecated: Option<Arc<str>>,
496}
497
498// ---------------------------------------------------------------------------
499// TraitDef
500// ---------------------------------------------------------------------------
501
502#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
503pub struct TraitDef {
504    pub fqcn: Arc<str>,
505    pub short_name: Arc<str>,
506    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
507    pub own_properties: IndexMap<Arc<str>, PropertyDef>,
508    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
509    pub template_params: Vec<TemplateParam>,
510    /// Traits used by this trait (`use OtherTrait;` inside a trait body).
511    pub traits: Vec<Arc<str>>,
512    pub location: Option<Location>,
513    /// `@psalm-require-extends` / `@phpstan-require-extends` — FQCNs that using classes must extend.
514    #[serde(default)]
515    pub require_extends: Vec<Arc<str>>,
516    /// `@psalm-require-implements` / `@phpstan-require-implements` — FQCNs that using classes must implement.
517    #[serde(default)]
518    pub require_implements: Vec<Arc<str>>,
519    /// `@deprecated` docblock annotation, if present.
520    #[serde(default)]
521    pub deprecated: Option<Arc<str>>,
522}
523
524// ---------------------------------------------------------------------------
525// EnumDef
526// ---------------------------------------------------------------------------
527
528#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
529pub struct EnumCaseDef {
530    pub name: Arc<str>,
531    pub value: Option<Type>,
532    pub location: Option<Location>,
533    /// `@deprecated` docblock annotation, if present.
534    #[serde(default)]
535    pub deprecated: Option<Arc<str>>,
536}
537
538#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
539pub struct EnumDef {
540    pub fqcn: Arc<str>,
541    pub short_name: Arc<str>,
542    pub scalar_type: Option<Type>,
543    pub interfaces: Vec<Arc<str>>,
544    pub cases: IndexMap<Arc<str>, EnumCaseDef>,
545    pub own_methods: IndexMap<Arc<str>, Arc<MethodDef>>,
546    pub own_constants: IndexMap<Arc<str>, ConstantDef>,
547    pub location: Option<Location>,
548}
549
550// ---------------------------------------------------------------------------
551// FunctionDef
552// ---------------------------------------------------------------------------
553
554#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
555pub struct FunctionDef {
556    pub fqn: Arc<str>,
557    pub short_name: Arc<str>,
558    #[serde(
559        deserialize_with = "deserialize_params",
560        serialize_with = "serialize_params"
561    )]
562    pub params: Arc<[FnParam]>,
563    /// Type from annotation (`@return` / native type hint). `None` means unannotated.
564    /// Stored as `Option<Arc<Type>>` to enable deduplication of common return types.
565    #[serde(
566        deserialize_with = "deserialize_return_type",
567        serialize_with = "serialize_return_type"
568    )]
569    pub return_type: Option<Arc<Type>>,
570    /// See `MethodDef::inferred_return_type` — `Option<Arc<Type>>` (8 B) for the
571    /// same demand-driven-inference reason.
572    #[serde(
573        deserialize_with = "deserialize_return_type",
574        serialize_with = "serialize_return_type"
575    )]
576    pub inferred_return_type: Option<Arc<Type>>,
577    pub template_params: Vec<TemplateParam>,
578    pub assertions: Vec<Assertion>,
579    pub throws: Vec<Arc<str>>,
580    pub deprecated: Option<Arc<str>>,
581    pub is_pure: bool,
582    pub location: Option<Location>,
583    /// Plain-text description from the docblock (text before `@tag` lines).
584    /// Used for hover info.
585    #[serde(default)]
586    pub docstring: Option<Arc<str>>,
587}
588
589impl FunctionDef {
590    pub fn effective_return_type(&self) -> Option<&Type> {
591        self.return_type
592            .as_deref()
593            .or(self.inferred_return_type.as_deref())
594    }
595}
596
597// ---------------------------------------------------------------------------
598// StubSlice — serializable bundle of definitions from one extension's stubs
599// ---------------------------------------------------------------------------
600
601/// A snapshot of all PHP definitions contributed by a single stub file set.
602///
603/// Produced by `mir-stubs-gen` at code-generation time and deserialized at
604/// runtime to ingest definitions into the salsa db via
605/// `MirDatabase::ingest_stub_slice`.
606#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
607pub struct StubSlice {
608    pub classes: Vec<Arc<ClassDef>>,
609    pub interfaces: Vec<Arc<InterfaceDef>>,
610    pub traits: Vec<Arc<TraitDef>>,
611    pub enums: Vec<Arc<EnumDef>>,
612    pub functions: Vec<Arc<FunctionDef>>,
613    #[serde(default)]
614    pub constants: Vec<(Arc<str>, Type)>,
615    /// Source file this slice was collected from. `None` for bundled stub slices
616    /// that were pre-computed and are not tied to a specific on-disk file.
617    #[serde(default)]
618    pub file: Option<Arc<str>>,
619    /// Types of `@var`-annotated global variables collected from this file.
620    /// Populated by `DefinitionCollector`; ingested into the salsa db's
621    /// `global_vars` table by `ingest_stub_slice` when `file` is `Some`.
622    #[serde(default)]
623    pub global_vars: Vec<(Arc<str>, Type)>,
624    /// The first namespace declared in this file (e.g. `"App\\Service"`).
625    /// Populated by `DefinitionCollector`; ingested into the salsa db's
626    /// `file_namespaces` table by `ingest_stub_slice` when `file` is `Some`.
627    #[serde(default)]
628    pub namespace: Option<Arc<str>>,
629    /// `use` alias map for this file: alias → FQCN.
630    ///
631    /// Stored as `Arc<FxHashMap<Name, Name>>` so that `file_imports()`
632    /// returns a cheap Arc clone instead of deep-cloning the map on every
633    /// `resolve_name` call (which fires once per symbol reference in
634    /// Pass 2). `Name` keys/values shrink each entry from ~108 bytes
635    /// (two `String` headers + two heap allocs averaging ~30 chars) to
636    /// 16 bytes (two `Ustr` u64 handles); the global ustr interner holds
637    /// one copy of each unique alias / FQCN string for the whole session.
638    #[serde(
639        deserialize_with = "deserialize_imports",
640        serialize_with = "serialize_imports"
641    )]
642    #[serde(default = "default_imports")]
643    pub imports: Arc<FxHashMap<Name, Name>>,
644    /// Set to `true` after `deduplicate_params_in_slice` has run on this slice.
645    /// `ingest_stub_slice` skips the clone+re-dedup when this flag is set.
646    #[serde(skip)]
647    pub is_deduped: bool,
648}
649
650// ---------------------------------------------------------------------------
651// Param list deduplication
652// ---------------------------------------------------------------------------
653
654use std::sync::Mutex;
655
656type ParamCache = Mutex<FxHashMap<Vec<FnParam>, Arc<[FnParam]>>>;
657
658/// Global cache of canonical Arc<[FnParam]> instances for deduplication.
659/// Shared across all StubSlices to deduplicate vendor code with millions of
660/// methods that often have identical parameter lists.
661static PARAM_DEDUP_CACHE: std::sync::OnceLock<ParamCache> = std::sync::OnceLock::new();
662
663/// Deduplicate parameter lists across all methods and functions in a StubSlice.
664/// Many PHP framework methods share identical parameter lists (e.g., thousands
665/// of `(string $arg, array $opts)` signatures). This function groups identical
666/// param lists globally (across all slices processed so far) and replaces them
667/// with Arc<[FnParam]> pointers to shared allocations.
668///
669/// Expected memory savings: 100–150 MiB on cold start (vendor collection).
670pub fn deduplicate_params_in_slice(slice: &mut StubSlice) {
671    let cache: &ParamCache = PARAM_DEDUP_CACHE.get_or_init(|| Mutex::new(FxHashMap::default()));
672    let mut canonical_params = cache.lock().unwrap();
673
674    let mut deduplicate = |params: &mut Arc<[FnParam]>| {
675        if let Some(existing) = canonical_params.get(params.as_ref()) {
676            *params = existing.clone();
677        } else {
678            canonical_params.insert(params.as_ref().to_vec(), params.clone());
679        }
680    };
681
682    // Deduplicate method params in all classes
683    for cls in &mut slice.classes {
684        for method in Arc::make_mut(cls).own_methods.values_mut() {
685            deduplicate(&mut Arc::make_mut(method).params);
686        }
687    }
688
689    // Deduplicate method params in all interfaces
690    for iface in &mut slice.interfaces {
691        for method in Arc::make_mut(iface).own_methods.values_mut() {
692            deduplicate(&mut Arc::make_mut(method).params);
693        }
694    }
695
696    // Deduplicate method params in all traits
697    for tr in &mut slice.traits {
698        for method in Arc::make_mut(tr).own_methods.values_mut() {
699            deduplicate(&mut Arc::make_mut(method).params);
700        }
701    }
702
703    // Deduplicate method params in all enums
704    for en in &mut slice.enums {
705        for method in Arc::make_mut(en).own_methods.values_mut() {
706            deduplicate(&mut Arc::make_mut(method).params);
707        }
708    }
709
710    // Deduplicate function params
711    for func in &mut slice.functions {
712        deduplicate(&mut Arc::make_mut(func).params);
713    }
714    slice.is_deduped = true;
715}