dependency-injector 1.0.0

High-performance, lock-free dependency injection container for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! High-performance storage for DI container
//!
//! Uses DashMap for lock-free concurrent access.
//! Optionally uses perfect hashing for locked containers (with `perfect-hash` feature).

#![allow(dead_code)]

use crate::factory::AnyFactory;
use ahash::RandomState;
use dashmap::DashMap;
use std::any::{Any, TypeId};
use std::sync::Arc;

#[cfg(feature = "perfect-hash")]
use std::hash::{Hash, Hasher};

// =============================================================================
// Unchecked Downcast (Phase 8 optimization)
// =============================================================================

/// Downcast an `Arc<dyn Any + Send + Sync>` to `Arc<T>` without runtime type checking.
///
/// This uses the same pattern as std's [`Arc::downcast`], but skips the `is::<T>()` check
/// because callers guarantee type correctness via `TypeId` lookup.
///
/// # Safety
///
/// The caller MUST guarantee that `arc` was originally created from an `Arc<T>`.
/// This is typically ensured by looking up via `TypeId::of::<T>()`.
///
/// # Implementation Notes
///
/// When casting `*const dyn Any` (fat pointer) to `*const T` (thin pointer), Rust
/// extracts just the data pointer portion, discarding the vtable metadata. This is
/// well-defined behavior and exactly how std's `Arc::downcast` works:
///
/// ```ignore
/// // From std::sync::Arc::downcast:
/// unsafe {
///     let ptr = Arc::into_raw(self);
///     Ok(Arc::from_raw(ptr as *const T))
/// }
/// ```
///
/// # Crate Invariants
///
/// In this crate, safety is guaranteed because:
/// - Factories are keyed by `TypeId::of::<T>()` at registration time
/// - Resolution looks up by the same `TypeId::of::<T>()`
/// - The factory stores the exact type that was registered
/// - TypeId comparison is the same check that `Any::is::<T>()` performs
#[inline]
pub(crate) unsafe fn downcast_arc_unchecked<T: Send + Sync + 'static>(
    arc: Arc<dyn Any + Send + Sync>,
) -> Arc<T> {
    // SAFETY: Caller guarantees arc contains type T (verified via TypeId lookup).
    // This is the same pattern used by std::sync::Arc::downcast.
    let ptr = Arc::into_raw(arc);
    // Fat-to-thin pointer cast extracts the data pointer, discarding vtable metadata.
    unsafe { Arc::from_raw(ptr as *const T) }
}

/// Thread-safe storage for service factories
///
/// Uses `DashMap` with `ahash` for maximum concurrent performance.
/// Supports hierarchical parent chain for deep scope resolution.
pub struct ServiceStorage {
    /// Map from TypeId to factory
    factories: DashMap<TypeId, AnyFactory, RandomState>,
    /// Optional parent storage for hierarchical resolution
    parent: Option<Arc<ServiceStorage>>,
}

impl ServiceStorage {
    /// Create new empty storage with optimized shard count.
    ///
    /// Uses 8 shards as a balance between:
    /// - Creation overhead (fewer shards = faster creation)
    /// - Concurrent read performance (more shards = less contention)
    ///
    /// Default DashMap uses num_cpus * 4 shards which is overkill for
    /// typical DI containers with <50 services.
    #[inline]
    pub fn new() -> Self {
        Self {
            factories: DashMap::with_capacity_and_hasher_and_shard_amount(
                0,
                RandomState::new(),
                8, // 8 shards balances creation speed vs concurrency
            ),
            parent: None,
        }
    }

    /// Create with pre-allocated capacity and optimized shards.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        // Scale shards based on expected capacity and concurrency needs
        let shard_amount = if capacity <= 16 {
            8
        } else if capacity <= 64 {
            16
        } else {
            32
        };
        Self {
            factories: DashMap::with_capacity_and_hasher_and_shard_amount(
                capacity,
                RandomState::new(),
                shard_amount,
            ),
            parent: None,
        }
    }

    /// Create a child storage with a parent reference for deep hierarchy resolution.
    ///
    /// Uses only 4 shards since child scopes typically have very few services
    /// (usually just request-specific data like RequestId). This reduces creation
    /// overhead while still supporting concurrent access.
    #[inline]
    pub fn with_parent(parent: Arc<ServiceStorage>) -> Self {
        Self {
            factories: DashMap::with_capacity_and_hasher_and_shard_amount(
                0,
                RandomState::new(),
                4, // 4 shards for child scopes (typically <5 services)
            ),
            parent: Some(parent),
        }
    }

    /// Insert a factory
    #[inline]
    pub(crate) fn insert(&self, type_id: TypeId, factory: AnyFactory) {
        self.factories.insert(type_id, factory);
    }

    /// Check if type exists
    #[inline]
    pub fn contains(&self, type_id: &TypeId) -> bool {
        self.factories.contains_key(type_id)
    }

    /// Resolve a service by TypeId
    #[inline]
    pub fn resolve(&self, type_id: &TypeId) -> Option<Arc<dyn Any + Send + Sync>> {
        self.factories.get(type_id).map(|f| f.resolve())
    }

    /// Try to resolve and downcast to T
    ///
    /// Uses unchecked downcast since we know the type from the TypeId lookup.
    #[inline]
    pub fn get<T: Send + Sync + 'static>(&self) -> Option<Arc<T>> {
        self.resolve(&TypeId::of::<T>()).map(|any| {
            // SAFETY: We looked up by TypeId::of::<T>(), so the factory
            // was registered with the same TypeId and stores type T.
            unsafe { downcast_arc_unchecked(any) }
        })
    }

    /// Resolve and return both the service and whether it's transient.
    ///
    /// This avoids a second DashMap lookup when checking if the service should be cached.
    /// Returns `Some((service, is_transient))` if found, `None` if not found.
    #[inline]
    pub fn get_with_transient_flag<T: Send + Sync + 'static>(&self) -> Option<(Arc<T>, bool)> {
        let type_id = TypeId::of::<T>();
        self.factories.get(&type_id).map(|factory| {
            let is_transient = factory.is_transient();
            let service = factory.resolve();
            // SAFETY: We looked up by TypeId::of::<T>(), so the factory stores type T.
            let typed = unsafe { downcast_arc_unchecked(service) };
            (typed, is_transient)
        })
    }

    /// Resolve a service by walking the full parent chain.
    ///
    /// Returns the service from the nearest scope that has it registered.
    #[inline]
    pub fn resolve_from_chain(&self, type_id: &TypeId) -> Option<Arc<dyn Any + Send + Sync>> {
        // Check current scope first
        if let Some(service) = self.resolve(type_id) {
            return Some(service);
        }

        // Walk parent chain
        let mut current = self.parent.as_ref();
        while let Some(storage) = current {
            if let Some(service) = storage.resolve(type_id) {
                return Some(service);
            }
            current = storage.parent.as_ref();
        }

        None
    }

    /// Check if a service exists in this storage or any parent.
    #[inline]
    pub fn contains_in_chain(&self, type_id: &TypeId) -> bool {
        // Check current scope first
        if self.contains(type_id) {
            return true;
        }

        // Walk parent chain
        let mut current = self.parent.as_ref();
        while let Some(storage) = current {
            if storage.contains(type_id) {
                return true;
            }
            current = storage.parent.as_ref();
        }

        false
    }

    /// Get reference to parent storage (if any)
    #[inline]
    pub fn parent(&self) -> Option<&Arc<ServiceStorage>> {
        self.parent.as_ref()
    }

    /// Create a child storage from this storage.
    ///
    /// This is more efficient than `with_parent` as it takes self by Arc reference.
    #[inline]
    pub fn child(self: &Arc<Self>) -> Self {
        Self {
            factories: DashMap::with_capacity_and_hasher_and_shard_amount(0, RandomState::new(), 8),
            parent: Some(Arc::clone(self)),
        }
    }

    /// Get number of registered services
    #[inline]
    pub fn len(&self) -> usize {
        self.factories.len()
    }

    /// Check if empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.factories.is_empty()
    }

    /// Clear all services (preserves parent reference)
    #[inline]
    pub fn clear(&self) {
        self.factories.clear();
    }

    /// Check if this storage has a parent
    #[inline]
    pub fn has_parent(&self) -> bool {
        self.parent.is_some()
    }

    /// Remove a service
    #[inline]
    pub fn remove(&self, type_id: &TypeId) -> bool {
        self.factories.remove(type_id).is_some()
    }

    /// Get all registered type IDs
    pub fn type_ids(&self) -> Vec<TypeId> {
        self.factories.iter().map(|r| *r.key()).collect()
    }

    /// Check if a service is transient
    #[inline]
    pub fn is_transient(&self, type_id: &TypeId) -> bool {
        self.factories
            .get(type_id)
            .map(|f| f.is_transient())
            .unwrap_or(false)
    }
}

impl Default for ServiceStorage {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ServiceStorage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ServiceStorage")
            .field("count", &self.len())
            .finish()
    }
}

// =============================================================================
// Perfect Hashing for Frozen Storage (Phase 10 optimization)
// =============================================================================

/// Wrapper for TypeId that implements Hash trait for boomphf
#[cfg(feature = "perfect-hash")]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct HashableTypeId(TypeId);

#[cfg(feature = "perfect-hash")]
impl Hash for HashableTypeId {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // TypeId is already a hash, just pass it through
        // Use the debug representation to get the internal value
        let type_id_bits: u64 = unsafe { std::mem::transmute_copy(&self.0) };
        type_id_bits.hash(state);
    }
}

/// Frozen storage with perfect hashing for O(1) lookups.
///
/// Created from a `ServiceStorage` after the container is locked.
/// Uses minimal perfect hashing (MPHF) for ~5ns faster resolution.
#[cfg(feature = "perfect-hash")]
pub struct FrozenStorage {
    /// The perfect hash function
    mphf: boomphf::Mphf<HashableTypeId>,
    /// Factories indexed by perfect hash
    factories: Vec<AnyFactory>,
    /// TypeIds for verification (optional, can be removed for speed)
    type_ids: Vec<TypeId>,
    /// Parent storage for hierarchical resolution
    parent: Option<Arc<FrozenStorage>>,
}

#[cfg(feature = "perfect-hash")]
impl FrozenStorage {
    /// Create a frozen storage from a ServiceStorage.
    ///
    /// This computes a minimal perfect hash function for all registered TypeIds,
    /// enabling O(1) lookups without hash collisions.
    pub fn from_storage(storage: &ServiceStorage) -> Self {
        // Collect all entries as owned values
        let entries: Vec<(TypeId, AnyFactory)> = storage
            .factories
            .iter()
            .map(|r| (*r.key(), r.value().clone()))
            .collect();

        let n = entries.len();
        if n == 0 {
            return Self {
                mphf: boomphf::Mphf::new(1.7, &[]),
                factories: Vec::new(),
                type_ids: Vec::new(),
                parent: storage
                    .parent
                    .as_ref()
                    .map(|p| Arc::new(Self::from_storage(p))),
            };
        }

        // Create hashable type IDs for MPHF
        let hashable_ids: Vec<HashableTypeId> =
            entries.iter().map(|(id, _)| HashableTypeId(*id)).collect();

        // Create MPHF with gamma=1.7 (good balance of speed vs memory)
        let mphf = boomphf::Mphf::new(1.7, &hashable_ids);

        // Create factory and type_id arrays indexed by perfect hash
        let mut factories: Vec<Option<AnyFactory>> = (0..n).map(|_| None).collect();
        let mut indexed_type_ids: Vec<Option<TypeId>> = (0..n).map(|_| None).collect();

        for (type_id, factory) in entries {
            let idx = mphf.hash(&HashableTypeId(type_id)) as usize;
            factories[idx] = Some(factory);
            indexed_type_ids[idx] = Some(type_id);
        }

        // Unwrap all Options (all slots should be filled)
        let factories: Vec<AnyFactory> = factories.into_iter().flatten().collect();
        let type_ids: Vec<TypeId> = indexed_type_ids.into_iter().flatten().collect();

        // Freeze parent if it exists
        let parent = storage
            .parent
            .as_ref()
            .map(|p| Arc::new(Self::from_storage(p)));

        Self {
            mphf,
            factories,
            type_ids,
            parent,
        }
    }

    /// Resolve a service by TypeId using perfect hashing.
    ///
    /// This is O(1) with no hash collisions.
    #[inline]
    pub fn resolve(&self, type_id: &TypeId) -> Option<Arc<dyn Any + Send + Sync>> {
        let hashable = HashableTypeId(*type_id);

        // Try to get hash - returns None if type_id wasn't in original set
        let idx = self.mphf.try_hash(&hashable)? as usize;

        // Bounds check (should always pass if MPHF is correct)
        if idx >= self.factories.len() {
            return None;
        }

        // Verify TypeId matches (MPHF can give false positives for unknown keys)
        if self.type_ids[idx] != *type_id {
            return None;
        }

        Some(self.factories[idx].resolve())
    }

    /// Check if a type exists in this frozen storage.
    #[inline]
    pub fn contains(&self, type_id: &TypeId) -> bool {
        let hashable = HashableTypeId(*type_id);

        if let Some(idx) = self.mphf.try_hash(&hashable) {
            let idx = idx as usize;
            idx < self.type_ids.len() && self.type_ids[idx] == *type_id
        } else {
            false
        }
    }

    /// Resolve from the full parent chain.
    #[inline]
    pub fn resolve_from_chain(&self, type_id: &TypeId) -> Option<Arc<dyn Any + Send + Sync>> {
        if let Some(service) = self.resolve(type_id) {
            return Some(service);
        }

        let mut current = self.parent.as_ref();
        while let Some(storage) = current {
            if let Some(service) = storage.resolve(type_id) {
                return Some(service);
            }
            current = storage.parent.as_ref();
        }

        None
    }

    /// Check if type exists in chain.
    #[inline]
    pub fn contains_in_chain(&self, type_id: &TypeId) -> bool {
        if self.contains(type_id) {
            return true;
        }

        let mut current = self.parent.as_ref();
        while let Some(storage) = current {
            if storage.contains(type_id) {
                return true;
            }
            current = storage.parent.as_ref();
        }

        false
    }

    /// Get the number of services.
    #[inline]
    pub fn len(&self) -> usize {
        self.factories.len()
    }

    /// Check if empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.factories.is_empty()
    }

    /// Check if a service is transient.
    #[inline]
    pub fn is_transient(&self, type_id: &TypeId) -> bool {
        let hashable = HashableTypeId(*type_id);

        if let Some(idx) = self.mphf.try_hash(&hashable) {
            let idx = idx as usize;
            if idx < self.factories.len() && self.type_ids[idx] == *type_id {
                return self.factories[idx].is_transient();
            }
        }
        false
    }
}

#[cfg(feature = "perfect-hash")]
impl std::fmt::Debug for FrozenStorage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FrozenStorage")
            .field("count", &self.len())
            .field("has_parent", &self.parent.is_some())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone)]
    struct TestService {
        value: i32,
    }

    #[cfg(feature = "perfect-hash")]
    #[test]
    fn test_frozen_storage() {
        let storage = ServiceStorage::new();
        storage.insert(
            TypeId::of::<TestService>(),
            AnyFactory::singleton(TestService { value: 42 }),
        );
        storage.insert(TypeId::of::<i32>(), AnyFactory::singleton(123i32));
        storage.insert(
            TypeId::of::<String>(),
            AnyFactory::singleton("hello".to_string()),
        );

        let frozen = FrozenStorage::from_storage(&storage);

        // Test contains
        assert!(frozen.contains(&TypeId::of::<TestService>()));
        assert!(frozen.contains(&TypeId::of::<i32>()));
        assert!(frozen.contains(&TypeId::of::<String>()));
        assert!(!frozen.contains(&TypeId::of::<bool>())); // Not registered

        // Test resolve
        let service = frozen.resolve(&TypeId::of::<TestService>()).unwrap();
        let typed: Arc<TestService> = unsafe { downcast_arc_unchecked(service) };
        assert_eq!(typed.value, 42);

        // Test len
        assert_eq!(frozen.len(), 3);
    }

    #[cfg(feature = "perfect-hash")]
    #[test]
    fn test_frozen_storage_empty() {
        let storage = ServiceStorage::new();
        let frozen = FrozenStorage::from_storage(&storage);

        assert!(frozen.is_empty());
        assert_eq!(frozen.len(), 0);
        assert!(!frozen.contains(&TypeId::of::<TestService>()));
    }

    #[test]
    fn test_storage_insert_and_get() {
        let storage = ServiceStorage::new();
        let type_id = TypeId::of::<TestService>();

        // Phase 2: Use new enum-based AnyFactory API
        storage.insert(type_id, AnyFactory::singleton(TestService { value: 42 }));

        let service = storage.get::<TestService>().unwrap();
        assert_eq!(service.value, 42);
    }

    #[test]
    fn test_storage_contains() {
        let storage = ServiceStorage::new();
        let type_id = TypeId::of::<TestService>();

        assert!(!storage.contains(&type_id));

        storage.insert(type_id, AnyFactory::singleton(TestService { value: 0 }));

        assert!(storage.contains(&type_id));
    }

    #[test]
    fn test_storage_remove() {
        let storage = ServiceStorage::new();
        let type_id = TypeId::of::<TestService>();

        storage.insert(type_id, AnyFactory::singleton(TestService { value: 0 }));
        assert!(storage.contains(&type_id));

        storage.remove(&type_id);
        assert!(!storage.contains(&type_id));
    }

    /// Verify that unchecked downcast correctly handles fat-to-thin pointer conversion.
    /// This test ensures the optimization is sound by checking pointer equality.
    #[test]
    fn test_unchecked_downcast_soundness() {
        use std::any::Any;

        // Create an Arc<T> and record its data pointer
        let original: Arc<TestService> = Arc::new(TestService { value: 42 });
        let original_ptr = Arc::as_ptr(&original);

        // Coerce to Arc<dyn Any + Send + Sync>
        let any_arc: Arc<dyn Any + Send + Sync> = original;

        // Downcast back using our unchecked function
        // SAFETY: We know the type is TestService
        let recovered: Arc<TestService> = unsafe { super::downcast_arc_unchecked(any_arc) };

        // Verify pointer equality (proves fat→thin cast is correct)
        assert_eq!(original_ptr, Arc::as_ptr(&recovered));

        // Verify value is intact
        assert_eq!(recovered.value, 42);
    }
}