Improve Enum.ToString performance (#6645)
authorBen Adams <thundercat@illyriad.co.uk>
Wed, 10 Aug 2016 15:17:37 +0000 (16:17 +0100)
committerJan Kotas <jkotas@microsoft.com>
Wed, 10 Aug 2016 15:17:37 +0000 (08:17 -0700)
commit571b963cd4f7af8674d1031c4adefb3d8bc47618
treec8d21005ccd095ecfa804fad8226ba14da1c3d2e
parent3afa34a104960b5057b38e83724b79cacce41285
Improve Enum.ToString performance (#6645)

Reduces allocations by 66%; increases performance by 600% (x7)

With these changes ToString no longer boxes the value and doesn't create an empty array for `CustomAttributeRecords` when the enum is not flagged; also caches whether the enum is flagged.

It still boxes the enum to Enum to make the ToString virtual call however.

Using the @jkotas enummark :wink:

```csharp
enum MyEnum { One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten };
public static void Main(string[] args)
{
    (MyEnum.Seven).ToString();
    int start = Environment.TickCount;
    for (int i = 0; i < 10000000; i++)
    {
        (MyEnum.Seven).ToString();
    }
    int end = Environment.TickCount;
    Console.WriteLine((end - start).ToString());
}
```
Pre: 5828ms, 5828ms, 5829ms (1.7M/s)
Post: 828ms, 828ms, 828ms (12.1M/s)
src/mscorlib/src/System/Enum.cs
src/mscorlib/src/System/Reflection/CustomAttribute.cs
src/mscorlib/src/System/RtType.cs