Boring3.FastGenericNew 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Boring3.FastGenericNew --version 1.1.0
NuGet\Install-Package Boring3.FastGenericNew -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Boring3.FastGenericNew" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Boring3.FastGenericNew --version 1.1.0
#r "nuget: Boring3.FastGenericNew, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Boring3.FastGenericNew as a Cake Addin
#addin nuget:?package=Boring3.FastGenericNew&version=1.1.0

// Install Boring3.FastGenericNew as a Cake Tool
#tool nuget:?package=Boring3.FastGenericNew&version=1.1.0

FastGenericNew GitHub Nuget Nuget

FastGenericNew is 10x times faster than Activator.CreateInstance<T>() / new T()

Install

DotNet CLI

dotnet add package Boring3.FastGenericNew --version 1.1.0

Package Reference

<PackageReference Include="Boring3.FastGenericNew" Version="1.1.0" />

Package Manager

Install-Package Boring3.FastGenericNew -Version 1.1.0

Features

  • Parameters Supported
  • Non-Public Constructor Supported
  • Zero box/unbox
  • ValueType Supported

Examples

Fast create instance of T:

FastNew<T>.CreateInstance();

Fast create instance of T with parameter(s):

FastNew<T, string>.CreateInstance("parameter");
FastNew<T, string, int>.CreateInstance("parameter", 0);

Benchmark

Environment


BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET Core SDK=5.0.100
  [Host]     : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT
  DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT

Reference Type (class)

Method Mean Error StdDev Ratio RatioSD Baseline Gen 0 Gen 1 Gen 2 Allocated Code Size
DirectNew 1.757 ns 0.0083 ns 0.0074 ns 0.79 0.01 No 0.0029 - - 24 B 25 B
FastNewT 2.223 ns 0.0121 ns 0.0108 ns 1.00 0.00 Yes 0.0029 - - 24 B 24 B
ActivatorCreate 32.653 ns 0.4308 ns 0.4030 ns 14.69 0.18 No 0.0029 - - 24 B 88 B
NewT 32.717 ns 0.6927 ns 0.7977 ns 14.70 0.37 No 0.0029 - - 24 B 88 B

Value Type (struct)

Method Mean Error StdDev Median Ratio RatioSD Baseline Gen 0 Gen 1 Gen 2 Allocated Code Size
DirectNew 0.0014 ns 0.0011 ns 0.0010 ns 0.0014 ns 0.004 0.00 No - - - - 3 B
NewT 0.0102 ns 0.0130 ns 0.0121 ns 0.0023 ns 0.020 0.03 No - - - - 3 B
FastNewT 0.4629 ns 0.0099 ns 0.0077 ns 0.4613 ns 1.000 0.00 Yes - - - - 24 B
ActivatorCreate 33.6680 ns 0.7328 ns 0.6855 ns 33.7002 ns 72.346 1.36 No 0.0029 - - 24 B 88 B

Note: JIT have two solutions for new T() compilation.
For Reference Types. new T() will equals Activator.CreateInstance<T>()
For Value Types. new T() will allocate it inline. So it fast than FastNew that unable to be inlined.

How it works

Not like Activator.CreateInstance<T>(). FastGenericNew will dynamically compile a method that return T. And cache it up by generic.

You can invoke this method by a delegate with no any box/unbox.

But there's still a little problem anyway.
.NET Runtime will not inline delegate in any case currently.
So it causes bit more costs than direct new.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 4,996 11/30/2020
1.1.0 1,263 11/9/2020