Giter VIP home page Giter VIP logo

fastgenericnew's Introduction

✨ Features

  • ✔️ The best CreateInstance ever

    • Up to 50x faster than Activator.CreateInstance<T>
    • Generic Parameters Support
    • Zero boxing/unboxing
    • TryGetValue-like TryFastNew API
    • Link Mode PublishTrimmed Support
    • Non-Public Constructor Support
    • No Generic Constraints
    • Compatible with .NET Standard 2.0
    • Multiple backend implementations
    • Heavily tested on Win/Mac/Linux
  • 🪛 Modern Compiler Integration

    • Source Generator v2 (Incremental Generator)
    • Highly Configurable (Props)
    • Multi-threaded Generation
  • 🔥 Latest C#/.NET Features Support

🔧 Installation

You should only use one of them

Pre-Compiled Version

dotnet add package FastGenericNew --version 3.3.1
<ItemGroup>
  <PackageReference Include="FastGenericNew" Version="3.3.1" />
</ItemGroup>

SourceGenerator Version

dotnet add package FastGenericNew.SourceGenerator --version 3.3.1
<ItemGroup>
  <PackageReference Include="FastGenericNew.SourceGenerator" Version="3.3.1" />
</ItemGroup>

SourceGeneratorV2 requires

.NET Standard 2.0 or above
C# 8.0 or above
Roslyn 4.0.1 or above
Modern IDE (Optional) [VS2022, Rider, VSCode]

📖 Examples

using FastGenericNew;

// Simply replace 'Activator' to 'FastNew'
var obj = FastNew.CreateInstance<T>();

// With parameter(s)
var obj2 = FastNew.CreateInstance<T, string>("text");
var obj3 = FastNew.CreateInstance<T, string, int>("text", 0);

// Try pattern
// NOTE: The try pattern will only check if the constructor can be called.
//       It will not catch or handle any exceptions thrown in the constructor.
if (FastNew.TryCreateInstance<T, string>("arg0", out T result));
{
    // ...
}

Notes

With .NET Framework, Activator.CreateInstance<T>() invokes the parameterless constructor of ValueType if
the constraint is where T : new() but appears to ignore the parameterless constructor if the constraint is where T : struct.
But FastNew.CreateInstance<T>() will always invoke the parameterless constructor if it's available.

If you don't want to invoke the parameterless constructor of ValueType, consider using FastNew.NewOrDefault<T>() which will never invoke the parameterless constructor of ValueType

🚀 Benchmark

Check the full benchmark results here:
https://github.com/Nyrest/FastGenericNew/blob/main/benchmark_results.md

Environment

BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.3958/23H2/2023Update/SunValley3)
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK 9.0.100-preview.7.24407.12
  [Host]     : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2
  Job-GFVMQQ : .NET 5.0.17 (5.0.1722.21314), X64 RyuJIT AVX2
  Job-FGYWFO : .NET 6.0.33 (6.0.3324.36610), X64 RyuJIT AVX2
  Job-LODQQQ : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2
  Job-NXJWMD : .NET 9.0.0 (9.0.24.40507), X64 RyuJIT AVX2
  Job-VBBRLS : .NET Framework 4.8.1 (4.8.9256.0), X64 RyuJIT VectorSize=256

Reference Types

Benchmark Result of Reference Types

Value Types

Benchmark Result of Value Types

📜 License

FastGenericNew is licensed under the MIT license.

fastgenericnew's People

Contributors

dependabot[bot] avatar emik03 avatar imgbotapp avatar nyrest avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fastgenericnew's Issues

Allow `@FastGenericNew.FastNew` to be declared `internal`

Hi, thanks for creating this excellent source generator and taking the time to maintain and read this. I'd like to suggest a configuration option to make FastNew internal, as exposing the classes publicly both makes tree-shakers less efficient, but also causes namespace conflicts if a consumer also uses the source generator or library. When it comes to utility-based functions such as generic factory methods, I very much want to internalize all of those components because I don't want to force users of my library to have functions that they very likely don't need or intend on having. Thanks!

Help for implementation in a different setup.

Hi,
I have a specific setup where I'm using Activator.CreateInstance.
Because I have a memory leak I want to try your approach.

But I'm not an expert so I want to ask you help to understand how to do it.

Here my current code
I have an array of class name like this one:

 public static readonly string[] authTables = new string[]
        {
            typeof(User).FullName,
            typeof(UserInfo).FullName
        };

and I give it to my method (in tables):

   public static void CreateTablesInDB(SqliteConnection con, string[] tables)
        {
            foreach (var table in tables)
            {
                Type tableType = Type.GetType(table + ", MyAssemblyName");//* assembly name                
                var instanceTable = Activator.CreateInstance(tableType);
                // here do some stuff with my instanceTable....
                string strCreateTable = ClassToDDL(instanceTable, "create");
            }
        }

User and UserInfo are my classes.

My question is: Can I use your approach? In which way?

Thank you.

XML Comments

  • FastNew
  • FastNew.CreateInstance
  • FastNew.TryCreateInstance
  • FastNew.Constructor

Fix annoying CS1591 warning

[WIP] Wiki

RIP me

  • Introduction

    • Welcome
    • Why FastGenericNew
    • When to use FastGenericNew
    • How this works
  • Get started

    • Installation Instructions
      • .NET CLI
      • PackageReference
      • Package Manager
      • Script & Interactive
  • API Summary & Examples

    • FastNew.CreateInstance
    • FastNew.TryCreateInstance
    • FastNew.GetCreateInstance (Unstable currently)
    • FastNew.GetTryCreateInstance (Not finished)
  • SourceGenerator

    • Description
    • When to use SourceGenerator
    • Compiler Options (A lot of work is needed, too many options 😨)
  • Notes

    • TBD

I'll start working after the stable version is released

Fix caching behavior

The current caching behavior could be buggy if there are multiple projects in a solution referencing to the Source Generator version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.