RepositoryHTML
fahrizalfhrl

fahrizalfhrl/fast-generic-new

FastGenericNew: high-performance generic instantiation for .NET, replacing Activator.CreateInstance<

150stars
0forks
0issues
1views

⚡ Fast Generic Instantiation Without the Reflection Tax

Replace Activator.CreateInstance<T> with cached delegates that run at full .NET speed.

FastGenericNew is a drop-in .NET library that provides high-performance generic instantiation, replacing Activator.CreateInstance<T> and new T() with cached, strongly-typed delegates and compile-time code paths. It supports generic parameters, non-public constructors, and parameterless struct constructors without boxing or generic constraints, making it ideal for performance-sensitive codebases that need to construct generic types efficiently.

✨ Highlights

  • Drop-in Activator replacement: FastNew.CreateInstance<T>() resolves to a cached, strongly-typed delegate at first use; subsequent calls are a single indirect invocation with no reflection overhead.
  • Generic arity up to 16 arguments: FastNew.CreateInstance<T, TArg0>(...) through FastNew.CreateInstance<T, ..., TArg15>(...) avoids object[] argument packing.
  • Zero boxing on value types: constructed via typed delegates per closed generic, so T structs are returned without intermediate object allocation.
  • Try-pattern API: FastNew.TryCreateInstance<T>(out T result) and generic-arg overloads return bool without throwing on missing constructors.
  • Non-public constructor access: resolves private/internal/protected parameterless and parameterized constructors where the runtime permits.
  • No generic constraints required: usable with T lacking new(), struct, or class constraints; unconstrained call sites are supported.
  • Source generator variant: incremental IIncrementalGenerator emits construction delegates at compile time, trim-safe under AOT and linker analysis.
  • Multiple backend implementations: expression-tree, dynamic-method/IL, and delegate-based paths selected per runtime and type shape; the fastest viable backend is chosen automatically.

💡 Why it matters

The standard Activator.CreateInstance<T> path relies on reflection, which is slow, allocates, and breaks in trimmed/AOT scenarios. FastGenericNew eliminates that overhead by caching delegates per closed generic type and offering a compile-time source generator that emits construction code at build time. The result is near-native instantiation speed with zero runtime reflection, safe under PublishTrimmed, AOT, and WebAssembly.

The library also solves common pain points: it works with unconstrained generic types, avoids boxing on value types, and provides a non-throwing TryCreateInstance API. Whether you choose the runtime package or the source generator variant, the API surface is identical — so you can switch without touching your call sites.


#DotNet #CSharp #Performance #OpenSource #TiniX

Similar Projects

Discover other similar projects and AI models.

RepositoryC#
bitwarden/server

Bitwarden infrastructure/backend (API, database, Docker, etc).

20.8k
Dec 1, 2026
Repository
MoienTajik/AspNetCore-Developer-Roadmap

Roadmap to becoming an ASP.NET Core developer in 2026

19.7k
Jan 29, 2026
RepositoryC#
Nyrest/FastGenericNew

The ultimate fast alternative to Activator.CreateInstance<T> / new T()

280
Aug 25, 2026
RepositoryC#
jellyfin/jellyfin

The Free Software Media System - Server Backend & API

60.0k
Dec 1, 2026
RepositoryC#
KnifeLemon/Filee

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

33.6k
Dec 3, 2026
RepositoryC#
AvaloniaUI/Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The future of .NET UI

31.9k
Nov 27, 2026
RepositoryC#
BeyondDimension/SteamTools

🛠「Watt Toolkit」是一个开源跨平台的多功能 Steam 工具箱。

27.9k
Dec 1, 2026
RepositoryPowerShell
dotnet/core

.NET news, announcements, release notes, and more!

22.1k
Nov 26, 2026
Repository
quozd/awesome-dotnet

A collection of awesome .NET libraries, tools, frameworks and software

21.7k
Mar 26, 2026