InstanceManager (public sealed partial class InstanceManager : SingletonBase<InstanceManager>, IReportable) is Nalix’s high-performance, Native AOT-safe dependency injection registry. It combines a 2-slot MRU [ThreadStatic] L1 cache with RuntimeTypeHandle-keyed concurrent storage and compile-time activators generated by InstanceGenerator.
2-Slot MRU [ThreadStatic] L1 Cache: GetExistingInstance<T>() checks two thread-local MRU slots (s_tsKey0/s_tsKey1) guarded by a monotonic invalidation counter (s_slotsInvalidated). After warm-up, resolving a service on the packet hot path requires zero allocations and zero locks.
Compile-Time Interface Mapping ([Injectable]): Interface registrations are explicit and AOT-safe. Annotating a class with [Injectable(typeof(IMyService))] causes InstanceGenerator to call InstanceManager.RegisterServiceMapping(typeof(MyService), typeof(IMyService)) in the assembly’s [ModuleInitializer]. When Register(new MyService()) or GetOrCreateInstance<MyService>() is called, all mapped service interfaces are populated automatically.
100% Native AOT Activators: Dynamic creation (GetOrCreateInstance, CreateInstance, CreateInstanceWithInjection) uses factory delegates pre-registered via InstanceManager.RegisterActivator by InstanceGenerator. Attempting to dynamically instantiate a type not decorated with [Injectable] throws InvalidOperationException.
Hard Cache Caps & Lockdown(): Both _instanceCache and _signatureInstanceCache enforce a cap of 4096 entries (MaxCachedInstances = 4096, MaxSignatureInstances = 4096). Calling Lockdown() after startup freezes the registry so subsequent Register<T>, RegisterForClassOnly<T>, RemoveInstance, Clear, GetOrCreateInstance(Type, args), or GetOrCreateInstance<T>(args) (when args.Length > 0) calls throw InvalidOperationException.
Safe Disposable & Reportable Lifecycle: Replaced or removed IDisposable instances are disposed once (only if no other type handle still references the same instance), and built-in CoreTelemetryTargetIReportable services (ITaskManager, IConnectionHub, IBufferPoolManager, IObjectPoolManager, ISessionService, etc.) are automatically registered with ReportRegistry.Instance (custom IReportable services can register explicitly via ReportRegistry.Instance.Register(key, instance)).
public void Register<T>(T instance) where T : class
Registers or atomically replaces instance under typeof(T) and any interfaces mapped via [Injectable(typeof(IService))]. Tracks IDisposable and built-in CoreTelemetryTargetIReportable instances.
RegisterForClassOnly<T>
public void RegisterForClassOnly<T>(T instance) where T : class
Alias for Register<T>(instance).
RegisterActivator
public static void RegisterActivator(Type type, Func<object?[], object> factory)
Registers a compile-time factory delegate (invoked automatically by InstanceGenerator).
RegisterServiceMapping
public static void RegisterServiceMapping(Type concreteType, Type serviceType)
Maps concreteType to serviceType (invoked automatically by InstanceGenerator).
Lockdown
public void Lockdown()
Freezes the manager state. Subsequent Register<T>, RegisterForClassOnly<T>, RemoveInstance, Clear(), GetOrCreateInstance(Type, args), or GetOrCreateInstance<T>(args) (when args.Length > 0) calls throw InvalidOperationException.
public T? GetExistingInstance<T>() where T : class
Lock-free hot-path lookup via the 2-slot MRU [ThreadStatic] L1 cache and _instanceCache. Returns null if not registered.
GetOrCreateInstance<T>
public T GetOrCreateInstance<T>(params object?[] args) where T : class
Returns the cached T instance or creates and caches a new one using its [Injectable] activator (when args.Length > 0, delegates to GetOrCreateInstance(typeof(T), args) and enforces Lockdown()).
GetOrCreateInstance
public object GetOrCreateInstance(Type type, params object?[] args)
Non-generic resolution/creation by Type (uses ActivatorKey signature caching when args are provided; throws InvalidOperationException if Lockdown() is active).
CreateInstance
public object CreateInstance(Type type, params object?[] args)
Creates a new instance via its [Injectable] activator without caching it.
CreateInstanceWithInjection<T>
public T CreateInstanceWithInjection<T>() where T : class
Creates a new uncached instance of T by selecting the greediest public constructor whose parameters can all be resolved from _instanceCache.
CreateInstanceWithInjection
public object CreateInstanceWithInjection(Type type)