All Projects → xin9le → Fastenum

xin9le / Fastenum

Licence: mit
The world fastest enum utilities for C#/.NET

Programming Languages

csharp
926 projects
enum
40 projects

Projects that are alternatives of or similar to Fastenum

Weihanli.common
common tools,methods,extension methods etc... .net 常用工具类,公共方法,常用扩展方法等,基础类库
Stars: ✭ 152 (-7.88%)
Mutual labels:  utility, netstandard
Shackle
High-Performance Erlang Network Client Framework
Stars: ✭ 163 (-1.21%)
Mutual labels:  high-performance
Akka
Build highly concurrent, distributed, and resilient message-driven applications on the JVM
Stars: ✭ 11,938 (+7135.15%)
Mutual labels:  high-performance
Fast Float Rust
Super-fast float parser in Rust
Stars: ✭ 160 (-3.03%)
Mutual labels:  high-performance
String Inflection
underscore -> UPCASE -> CamelCase conversion of names
Stars: ✭ 157 (-4.85%)
Mutual labels:  utility
9volt
A modern, distributed monitoring system written in Go
Stars: ✭ 160 (-3.03%)
Mutual labels:  high-performance
Osharp
OSharp是一个基于.NetCore的快速开发框架,框架对 AspNetCore 的配置、依赖注入、日志、缓存、实体框架、Mvc(WebApi)、身份认证、功能权限、数据权限等模块进行更高一级的自动化封装,并规范了一套业务实现的代码结构与操作流程,使 .Net Core 框架更易于应用到实际项目开发中。
Stars: ✭ 2,151 (+1203.64%)
Mutual labels:  netstandard
Node Git Server
🎡 A configurable git server written in Node.js
Stars: ✭ 163 (-1.21%)
Mutual labels:  utility
Vulkancore
Vulkan 1.0 graphics and compute API bindings for .NET Standard
Stars: ✭ 162 (-1.82%)
Mutual labels:  netstandard
Clojurecuda
Clojure library for CUDA development
Stars: ✭ 158 (-4.24%)
Mutual labels:  high-performance
Vald
Vald. A Highly Scalable Distributed Vector Search Engine
Stars: ✭ 158 (-4.24%)
Mutual labels:  high-performance
Ffmediatoolkit
FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
Stars: ✭ 156 (-5.45%)
Mutual labels:  netstandard
Tesla
Tesla is a gateway service that provides dynamic routing,waf,support spring cloud,gRPC,DUBBO and more.
Stars: ✭ 161 (-2.42%)
Mutual labels:  high-performance
Vert.x
Vert.x is a tool-kit for building reactive applications on the JVM
Stars: ✭ 12,544 (+7502.42%)
Mutual labels:  high-performance
Haproxy
HAProxy Load Balancer's development branch (mirror of git.haproxy.org)
Stars: ✭ 2,463 (+1392.73%)
Mutual labels:  high-performance
Licia
Useful utility collection with zero dependencies
Stars: ✭ 1,984 (+1102.42%)
Mutual labels:  utility
Fselect
Find files with SQL-like queries
Stars: ✭ 3,103 (+1780.61%)
Mutual labels:  utility
Graphtage
A semantic diff utility and library for tree-like files such as JSON, JSON5, XML, HTML, YAML, and CSV.
Stars: ✭ 2,062 (+1149.7%)
Mutual labels:  utility
Aspjson
A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
Stars: ✭ 165 (+0%)
Mutual labels:  utility
Revo
Event Sourcing, CQRS and DDD framework for C#/.NET Core.
Stars: ✭ 162 (-1.82%)
Mutual labels:  netstandard

FastEnum

FastEnum is the fastest enum utilities for C#/.NET. It's much faster than .NET Core, and also faster than Enums.NET that is similar library. Provided methods are all achieved zero allocation and are designed easy to use like System.Enum. This library is quite useful to significantly improve your performance because enum is really popular feature.

Releases

Performance

image

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 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
  ShortRun : .NET Core 5.0.0 (CoreCLR 5.0.20.51904, CoreFX 5.0.20.51904), X64 RyuJIT

Support Platform

  • .NET Framework 4.6.1+
  • .NET Standard 2.0+
  • .NET 5.0+

How to use

This library super easy to use like System.Enum that is standard of .NET. Look below:

//--- FastEnum
var values = FastEnum.GetValues<Fruits>();
var names = FastEnum.GetNames<Fruits>();
var name = FastEnum.GetName<Fruits>(Fruits.Apple);
var name = Fruits.Apple.ToName();
var defined = FastEnum.IsDefined<Fruits>(123);
var parse = FastEnum.Parse<Fruits>("Apple");
var tryParse = FastEnum.TryParse<Fruits>("Apple", out var value);
//--- .NET
var values = Enum.GetValues(typeof(Fruits)) as Fruits[];
var names = Enum.GetNames(typeof(Fruits));
var name = Enum.GetName(typeof(Fruits), Fruits.Apple);
var name = Fruits.Apple.ToString();
var defined = Enum.IsDefined(typeof(Fruits), 123);
var parse = Enum.Parse<Fruits>("Apple");
var tryParse = Enum.TryParse<Fruits>("Apple", out var value);

As you can see, the replacement from System.Enum is very easy. You never confuse.

More features

There are some functions that are often used for enum, and you can be used more conveniently by including them together.

1. Gets pairwised member information

Sometimes you want name / value pair of enum. Member<TEnum> can be used under such cases. Of course supports deconstruction feature. FieldInfo is also included, so please use it for reflection code.

class Member<TEnum>
{
    public TEnum Value { get; }
    public string Name { get; }
    public FieldInfo FieldInfo { get; }
    // etc...
}

var member = Fruits.Apple.ToMember();
var (name, value) = member;  // Supports deconstruction

2. Gets EnumMemberAttribute.Value

I often see the developer using EnumMemberAttribute as an alias for field name. So FastEnum provides an API that the value can be quickly obtained from the EnumMemberAttribute.Value property.

enum Company
{
    [EnumMember(Value = "Apple, Inc.")]
    Apple = 0,
}

var value = Company.Apple.GetEnumMemberValue();  // Apple, Inc.

3. Adds multiple label annotations to a field

Multiple attributes can’t be attached to the same field, since EnumMemberAttribute is specified AllowMultiple = false. It’s inconvenient and I don’t like it personally, so I often use my own LabelAttribute as an alternative. You can use it conveniently as follows, because FastEnum provides this feature.

enum Company
{
    [Label("Apple, Inc.")]
    [Label("AAPL", 1)]
    Apple = 0,
}

var x1 = Company.Apple.GetLabel();   // Apple, Inc.
var x2 = Company.Apple.GetLabel(1);  // AAPL

Limitation

1. Provides only generics API

FastEnum provides only generics version method because of performance reason. System.Enum provides System.Type argument overload, but that’s too slow because of boxing occuration. If you need to use the method that passes System.Type type, please use System.Enum version.

2. Can’t parse comma-separated string

System.Enum.Parse can parse like following string. I think that it isn’t well known because it is a specification that exists quietly.

//--- Assuming there is an enum type like following...
[Flags]
enum Fruits
{
    Apple = 1,
    Lemon = 2,
    Melon = 4,
    Banana = 8,
}

//--- Passes comma-separated string
var value = Enum.Parse<Fruits>("Apple, Melon");
Console.WriteLine((int)value);  // 5

It seems to be a useful function when performing flag processing, but if tries to add such a comma-separated analysis, the overhead will come out, so cutting this feature off makes speed up. I think that in most cases there is no problem, because this feature is rarely used (at least I have NEVER used for 12 years).

Why fast ?

As you might expect, it’s because cached internally. It takes the approach of Static Type Caching, so the reading cost is almost zero. Based on this, I use techniques for avoiding allocation, and create specialized dictionary for specific key internally.

Installation

Getting started from downloading NuGet package.

dotnet add package FastEnum
PM> Install-Package FastEnum

License

This library is provided under MIT License.

Author

Takaaki Suzuki (a.k.a @xin9le) is software developer in Japan who awarded Microsoft MVP for Developer Technologies (C#) since July 2012.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].