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

Use FrozenDictionary instead of Dictionary. #169

Merged
merged 1 commit into from
Feb 28, 2024

Conversation

InCerryGit
Copy link
Member

Use FrozenDictionary instead of Dictionary in scenarios where the dictionary remains unchanged to improve performance.

benchmark codes:

using System.Collections.Frozen;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

_ = BenchmarkRunner.Run<DictionaryBenchmark>();

[MemoryDiagnoser]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
public class DictionaryBenchmark
{
    private static readonly Dictionary<string, string> _dictionary = new Dictionary<string, string>
    {
        {
            "Config.Add", string.Empty
        },
        {
            "Config.AddRange", string.Empty
        },
        {
            "Config.EnvSync", string.Empty
        },
        {
            "Config.Edit", string.Empty
        },
        {
            "Config.Delete", string.Empty
        },
        {
            "Config.DeleteSome", string.Empty
        },
        {
            "Config.Offline", string.Empty
        },
        {
            "Config.OfflineSome", string.Empty
        },
        {
            "Config.Publish", string.Empty
        },
        {
            "Config.Publish_API", string.Empty
        },
        {
            "Config.Rollback", string.Empty
        },
        {
            "Config.Rollback_API", string.Empty
        },
        {
            "App.Add", string.Empty
        },
        {
            "App.Edit", string.Empty
        },
        {
            "App.Delete", string.Empty
        },
        {
            "App.DisableOrEanble", string.Empty
        },
        {
            "App.Auth", string.Empty
        },
        {
            "Node.Add", string.Empty
        },
        {
            "Node.Delete", string.Empty
        }
    };

    private static readonly FrozenDictionary<string, string> _frozenDictionary = _dictionary.ToFrozenDictionary();

    private static readonly string[] _keys = _dictionary.Keys.ToArray();

    [Benchmark]
    public void Dictionary()
    {
        for (var i = 0; i < _keys.Length; i++)
        {
            _ = _dictionary[_keys[i]];
        }
    }

    [Benchmark]
    public void FrozenDictionary()
    {
        for (var i = 0; i < _keys.Length; i++)
        {
            _ = _frozenDictionary[_keys[i]];
        }
    }
}

result:

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3155/23H2/2023Update/SunValley3)
Intel Core i7-14700K, 1 CPU, 28 logical and 20 physical cores
.NET SDK 8.0.201
  [Host]     : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX2
  DefaultJob : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX2


| Method           | Mean      | Error    | StdDev   | Allocated |
|----------------- |----------:|---------:|---------:|----------:|
| FrozenDictionary |  52.90 ns | 0.415 ns | 0.368 ns |         - |
| Dictionary       | 108.02 ns | 0.293 ns | 0.274 ns |         - |

@kklldog
Copy link
Collaborator

kklldog commented Feb 28, 2024

厉害了我的总!
Thanks a lot.

@kklldog kklldog merged commit 9d3f9c4 into dotnetcore:master Feb 28, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants