diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a8542d --- /dev/null +++ b/.gitignore @@ -0,0 +1,362 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..9b8d58c --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,51 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "debug-build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/FastGenericNew/FastGenericNew.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary", + "-c", + "Debug" + ], + "group": "build", + "problemMatcher": "$msCompile" + }, + { + "label": "release-build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/FastGenericNew/FastGenericNew.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary", + "-c", + "Release" + ], + "group": "build", + "problemMatcher": "$msCompile" + }, + { + "label": "Run Benchmark", + "command": "dotnet", + "type": "process", + "args": [ + "run", + "--project", + "${workspaceFolder}/Benchmark/Benchmark.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary", + "-c", + "Release" + ], + "group": "build", + "problemMatcher": "$msCompile" + } + ] +} diff --git a/Benchmark/Benchmark.csproj b/Benchmark/Benchmark.csproj new file mode 100644 index 0000000..b847436 --- /dev/null +++ b/Benchmark/Benchmark.csproj @@ -0,0 +1,16 @@ + + + Exe + net5.0 + + + + + + + + + + + + diff --git a/Benchmark/FastNewBenchmark.cs b/Benchmark/FastNewBenchmark.cs new file mode 100644 index 0000000..95d6540 --- /dev/null +++ b/Benchmark/FastNewBenchmark.cs @@ -0,0 +1,36 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Order; +using FastGenericNew; +#pragma warning disable CA1822 // Member does not access instance data and can be marked as static + +namespace Benchmark +{ + [StopOnFirstError] + [MemoryDiagnoser] + [DisassemblyDiagnoser] + [BaselineColumn] + [Orderer(SummaryOrderPolicy.FastestToSlowest)] + public class FastNewBenchmark + { + [Benchmark] + public Example DirectNew() => + new Example(); + + [Benchmark(Baseline = true)] + public Example FastNewT() => + Test.FastNew(); + + [Benchmark] + public Example NewT() => + Test.New(); + } + + public static class Test where T : new() + { + public static T FastNew() => FastNew.CreateInstance(); + + public static T New() => new T(); + } + + public class Example { } +} \ No newline at end of file diff --git a/Benchmark/Program.cs b/Benchmark/Program.cs new file mode 100644 index 0000000..1a0df29 --- /dev/null +++ b/Benchmark/Program.cs @@ -0,0 +1,13 @@ +using BenchmarkDotNet.Running; + +namespace Benchmark +{ + class Program + { + static void Main() + { + BenchmarkRunner.Run(); + + } + } +} \ No newline at end of file diff --git a/FastGenericNew.sln b/FastGenericNew.sln new file mode 100644 index 0000000..5a467f8 --- /dev/null +++ b/FastGenericNew.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30626.31 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGenericNew", "FastGenericNew\FastGenericNew.csproj", "{42CB2629-2845-4EA9-A920-D5E27F34166C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "Benchmark\Benchmark.csproj", "{2E299ABC-67E9-4163-A111-65557046380E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {42CB2629-2845-4EA9-A920-D5E27F34166C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42CB2629-2845-4EA9-A920-D5E27F34166C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42CB2629-2845-4EA9-A920-D5E27F34166C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42CB2629-2845-4EA9-A920-D5E27F34166C}.Release|Any CPU.Build.0 = Release|Any CPU + {2E299ABC-67E9-4163-A111-65557046380E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2E299ABC-67E9-4163-A111-65557046380E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2E299ABC-67E9-4163-A111-65557046380E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2E299ABC-67E9-4163-A111-65557046380E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {75D760F6-62AB-467E-AA84-5208960EE4F5} + EndGlobalSection +EndGlobal diff --git a/FastGenericNew/ConstructorOf.cs b/FastGenericNew/ConstructorOf.cs new file mode 100644 index 0000000..f136c23 --- /dev/null +++ b/FastGenericNew/ConstructorOf.cs @@ -0,0 +1,128 @@ +using System; +using System.Reflection; + +namespace FastGenericNew +{ + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter) + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2) + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + typeof(TParameter3), + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + typeof(TParameter4), + typeof(TParameter5), + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + typeof(TParameter4), + typeof(TParameter5), + typeof(TParameter6), + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + typeof(TParameter4), + typeof(TParameter5), + typeof(TParameter6), + typeof(TParameter7), + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + typeof(TParameter4), + typeof(TParameter5), + typeof(TParameter6), + typeof(TParameter7), + typeof(TParameter8), + }, null); + } + + public static class ConstructorOf + { + public static readonly ConstructorInfo value = + typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] + { + typeof(TParameter), + typeof(TParameter2), + typeof(TParameter3), + typeof(TParameter4), + typeof(TParameter5), + typeof(TParameter6), + typeof(TParameter7), + typeof(TParameter8), + typeof(TParameter9), + }, null); + } +} diff --git a/FastGenericNew/FastGenericNew.csproj b/FastGenericNew/FastGenericNew.csproj new file mode 100644 index 0000000..d299200 --- /dev/null +++ b/FastGenericNew/FastGenericNew.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.0 + + + + + + + + + + + \ No newline at end of file diff --git a/FastGenericNew/FastNew.cs b/FastGenericNew/FastNew.cs new file mode 100644 index 0000000..34ad33d --- /dev/null +++ b/FastGenericNew/FastNew.cs @@ -0,0 +1,177 @@ +using System; +using System.Linq.Expressions; +#pragma warning disable HAA0101 // Array allocation for params parameter + +namespace FastGenericNew +{ + public static class FastNew + { + public static readonly Func CreateInstance + = Expression.Lambda>(Expression.New(ConstructorOf.value)).Compile(); + } + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1), + parameter1).Compile(); + } + } + + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2), + parameter1, parameter2).Compile(); + } + } + + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3), + parameter1, parameter2, parameter3).Compile(); + } + } + + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + ParameterExpression parameter4 = Expression.Parameter(typeof(TParameter4)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3, parameter4), + parameter1, parameter2, parameter3, parameter4).Compile(); + } + } + + public static class FastNew + { + public static readonly Func CreateInstance; + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + ParameterExpression parameter4 = Expression.Parameter(typeof(TParameter4)); + ParameterExpression parameter5 = Expression.Parameter(typeof(TParameter5)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3, parameter4, parameter5), + parameter1, parameter2, parameter3, parameter4, parameter5).Compile(); + } + } + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + ParameterExpression parameter4 = Expression.Parameter(typeof(TParameter4)); + ParameterExpression parameter5 = Expression.Parameter(typeof(TParameter5)); + ParameterExpression parameter6 = Expression.Parameter(typeof(TParameter6)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6), + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6).Compile(); + } + } + + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + ParameterExpression parameter4 = Expression.Parameter(typeof(TParameter4)); + ParameterExpression parameter5 = Expression.Parameter(typeof(TParameter5)); + ParameterExpression parameter6 = Expression.Parameter(typeof(TParameter6)); + ParameterExpression parameter7 = Expression.Parameter(typeof(TParameter7)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7), + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7).Compile(); + } + } + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + ParameterExpression parameter4 = Expression.Parameter(typeof(TParameter4)); + ParameterExpression parameter5 = Expression.Parameter(typeof(TParameter5)); + ParameterExpression parameter6 = Expression.Parameter(typeof(TParameter6)); + ParameterExpression parameter7 = Expression.Parameter(typeof(TParameter7)); + ParameterExpression parameter8 = Expression.Parameter(typeof(TParameter8)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7, parameter8), + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7, parameter8).Compile(); + } + } + + public static class FastNew + { + public static readonly Func CreateInstance; + + static FastNew() + { + ParameterExpression parameter1 = Expression.Parameter(typeof(TParameter)); + ParameterExpression parameter2 = Expression.Parameter(typeof(TParameter2)); + ParameterExpression parameter3 = Expression.Parameter(typeof(TParameter3)); + ParameterExpression parameter4 = Expression.Parameter(typeof(TParameter4)); + ParameterExpression parameter5 = Expression.Parameter(typeof(TParameter5)); + ParameterExpression parameter6 = Expression.Parameter(typeof(TParameter6)); + ParameterExpression parameter7 = Expression.Parameter(typeof(TParameter7)); + ParameterExpression parameter8 = Expression.Parameter(typeof(TParameter8)); + ParameterExpression parameter9 = Expression.Parameter(typeof(TParameter9)); + CreateInstance = Expression.Lambda>(Expression.New( + ConstructorOf.value, + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7, parameter8, parameter9), + parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7, parameter8, parameter9).Compile(); + } + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..89adf4c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Boring3 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..34de0ba --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# FastGenericNew ![GitHub](https://img.shields.io/github/license/BThree496/FastGenericNew?style=flat-square) + +FastGenericNew is 10x times faster than `new T()` / `Activator.CreateInstance()` +Also with parameters support + +## Examples + +Fast create instance of `T`: + +```cs +FastNew.CreateInstance(); +``` + +Fast create instance of `T` with parameter(s): + +```cs +FastNew.CreateInstance("parameter"); +FastNew.CreateInstance("parameter", 0); +``` + +## Benchmark + +```ini + +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-rc.2.20479.15 + [Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.47505, CoreFX 5.0.20.47505), X64 RyuJIT + DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.47505, CoreFX 5.0.20.47505), X64 RyuJIT + + +``` + +| Method | Mean | Error | StdDev | Ratio | RatioSD | Baseline | Gen 0 | Gen 1 | Gen 2 | Allocated | Code Size | +|---------- |----------:|----------:|----------:|------:|--------:|--------- |-------:|------:|------:|----------:|----------:| +| DirectNew | 1.793 ns | 0.0206 ns | 0.0193 ns | 0.79 | 0.01 | No | 0.0029 | - | - | 24 B | 25 B | +| FastNewT | 2.262 ns | 0.0076 ns | 0.0071 ns | 1.00 | 0.00 | Yes | 0.0029 | - | - | 24 B | 24 B | +| NewT | 33.332 ns | 0.3524 ns | 0.3296 ns | 14.74 | 0.16 | No | 0.0029 | - | - | 24 B | 88 B | + +## How it works + +Not like `Activator.CreateInstance()`. FastGenericNew will dynamic 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. So it cause more cost than direct new. diff --git a/meaninglessLogo.png b/meaninglessLogo.png new file mode 100644 index 0000000..109545e Binary files /dev/null and b/meaninglessLogo.png differ