Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FAST GetAttribute methods. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

soi013
Copy link

@soi013 soi013 commented Sep 8, 2019

Hi,

This PR is adding fast GetAttribute methods.

How to use

//from Member 
Fruits.Apple.ToMember().GetAttribute<DescriptionAttribute>()?.Description;

//Direct: it is little bit faster, but verbose(`<Fruits`).
Fruits.Apple.GetAttribute<Fruits, DescriptionAttribute>()?.Description;

Benchmark

Method Mean Error StdDev Ratio
NetCore 1,308.74 ns 119.295 ns 6.5390 ns 1.000
EnumsNet 24.24 ns 2.391 ns 0.1310 ns 0.019
FastEnumFromMember 17.09 ns 2.669 ns 0.1463 ns 0.013
FastEnumDirect 11.42 ns 2.344 ns 0.1285 ns 0.009

@xin9le xin9le self-requested a review September 8, 2019 08:53
@xin9le xin9le added the enhancement New feature or request label Sep 8, 2019
@xin9le
Copy link
Owner

xin9le commented Sep 8, 2019

Thank you for your PR! I like your attribute caching approch. However, I have two questions.

Questions

  1. There is a possibility of two or more attribute annotated (like LabelAttribute). I think your PR doesn't implement its case.

  2. What attributes do you want to annotate to the enum field? Almost case, I think LabelAttribute and EnumMemberAttribute are enough.

@soi013
Copy link
Author

soi013 commented Sep 8, 2019

There is a possibility of two or more attribute annotated (like LabelAttribute). I think your PR doesn't implement its case.

OK. I will add it.

What attributes do you want to annotate to the enum field? Almost case, I think LabelAttribute and EnumMemberAttribute are enough.

I usually use original attributes for extend enum.
It's a sample.

enum FileTypes
{
    [FileExt(".txt"), Description("Full text"), CanRead, CanWrite]
    FullText,
    [FileExt(".txt"), Description("Simple text: can't read"), CanWrite]
    SimpleText,
    [FileExt(".mybin2"), Description("my original format"), CanRead, CanWrite]
    Binary,
    [FileExt(".mybin"), Description("my old original format"), CanRead, CanWrite, Obsolete]
    OldBinary,
}

//At file save dialog
FastEnum.GetValues<FileTypes>()
    .Where(x => x.GetAttribute<FileTypes, CanWrite>() != null)
    .Where(x => x.GetAttribute<FileTypes, Obsolete>()==null)
    .Select(x =>
        (
            x.GetAttribute<FileTypes, FileExtAttribute>().FileExt,
            x.GetAttribute<FileTypes, DescriptionAttribute>().Description
        ))
    .Select((a) =>$"{a.Description} (*{a.FileExt})|*{a.FileExt}")
//create Filter of dialg

@soi013
Copy link
Author

soi013 commented Sep 10, 2019

I added GetAttribute"s" methods.

This is Benchmark.

Method Mean Error StdDev Ratio
NetCore 2,098.96 ns 33.497 ns 1.8361 ns 1.00
EnumsNet 177.24 ns 2.348 ns 0.1287 ns 0.08
FastEnumFromMember 70.98 ns 11.527 ns 0.6318 ns 0.03
FastEnumDirect 79.32 ns 299.535 ns 16.4185 ns 0.04

@xin9le
Copy link
Owner

xin9le commented Sep 30, 2019

I think almost ok. However I'm considering about a better way now. Please wait for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants