Skip to content
New issue

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

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

Already on GitHub? # to your account

Refactoring AuthenticationPropertiesExtensions #1585

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

Conversation

testfirstcoder
Copy link
Contributor

@testfirstcoder testfirstcoder commented Aug 20, 2024

Use Append method instead of ToList and Add methods.

[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class Benchmark
{
    private string clientId = "1";

    [Benchmark]
    public void ToList_Add()
    {
        IEnumerable<string> clients = [];

        var update = clients.ToList();
        update.Add(clientId);
    }

    [Benchmark]
    public void Append()
    {
        IEnumerable<string> clients = [];
        clients.Append(clientId);
    }
}

void Main() => BenchmarkRunner.Run<Benchmark>();
// * Summary *

BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.4780/22H2/2022Update)
Intel Core i7-10700 CPU 2.90GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.401
  [Host] : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2

Job=.NET 8.0  Runtime=.NET 8.0  

| Method     | Mean      | Error     | StdDev    | Median    | Gen0   | Allocated |
|----------- |----------:|----------:|----------:|----------:|-------:|----------:|
| ToList_Add | 28.061 ns | 0.4924 ns | 0.5473 ns | 28.045 ns | 0.0105 |      88 B |
| Append     |  8.149 ns | 0.1996 ns | 0.5499 ns |  7.959 ns | 0.0076 |      64 B |
// * Hints *
Outliers
  Benchmark.ToList_Add: .NET 8.0 -> 1 outlier  was  removed (31.57 ns)
  Benchmark.Append: .NET 8.0     -> 12 outliers were removed (11.61 ns..14.89 ns)

// * Legends *
  Mean      : Arithmetic mean of all measurements
  Error     : Half of 99.9% confidence interval
  StdDev    : Standard deviation of all measurements
  Median    : Value separating the higher half of all measurements (50th percentile)
  Gen0      : GC Generation 0 collects per 1000 operations
  Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
  1 ns      : 1 Nanosecond (0.000000001 sec)

Use TryGetValue method instead of ContainsKey method and Indexer (CA1854).

[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class Benchmark
{
    private IDictionary<string, string> items = new Dictionary<string, string>
    {
        ["1"] = "A"
    };

    private string clientListKey = "1";

    [Benchmark]
    public void ContainsKey_Indexer()
    {
        if (items.ContainsKey(clientListKey) == true)
        {
            var value = items[clientListKey];
        }
    }

    [Benchmark]
    public void TryGetValue()
    {
        if (items.TryGetValue(clientListKey, out var value) == true)
        {
        }
    }
}

void Main() => BenchmarkRunner.Run<Benchmark>();
// * Summary *

BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.4780/22H2/2022Update)
Intel Core i7-10700 CPU 2.90GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.401
  [Host] : .NET 8.0.8 (8.0.824.36612), X64 RyuJIT AVX2

Job=.NET 8.0  Runtime=.NET 8.0  

| Method              | Mean     | Error    | StdDev   | Allocated |
|-------------------- |---------:|---------:|---------:|----------:|
| ContainsKey_Indexer | 19.34 ns | 0.226 ns | 0.200 ns |         - |
| TryGetValue         | 10.46 ns | 0.171 ns | 0.142 ns |         - |
// * Hints *
Outliers
  Benchmark.ContainsKey_Indexer: .NET 8.0 -> 1 outlier  was  removed (21.41 ns)
  Benchmark.TryGetValue: .NET 8.0         -> 2 outliers were removed (12.39 ns, 13.60 ns)

// * Legends *
  Mean      : Arithmetic mean of all measurements
  Error     : Half of 99.9% confidence interval
  StdDev    : Standard deviation of all measurements
  Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
  1 ns      : 1 Nanosecond (0.000000001 sec)

@testfirstcoder testfirstcoder changed the title Refactoring authentication properties extensions Refactoring AuthenticationPropertiesExtensions Aug 20, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant