Created
July 11, 2023 16:53
-
-
Save antonfirsov/a312e1199110e03ec818f4b9097dc7df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using System.Collections.Frozen; | |
[MemoryDiagnoser] | |
public class StatusCodeBoxingBenchmarks | |
{ | |
[Benchmark] | |
public object Get1_FrozenDictionary() | |
{ | |
return GetBoxedStatusCode_FrozenDictionary(200); | |
} | |
[Benchmark] | |
public object Get1_LazyArray() | |
{ | |
return GetBoxedStatusCode_LazyArray(200); | |
} | |
[Benchmark] | |
public object Get4_FrozenDictionary() | |
{ | |
_ = GetBoxedStatusCode_FrozenDictionary(500); | |
_ = GetBoxedStatusCode_FrozenDictionary(400); | |
_ = GetBoxedStatusCode_FrozenDictionary(300); | |
return GetBoxedStatusCode_FrozenDictionary(200); | |
} | |
[Benchmark] | |
public object Get4_LazyArray() | |
{ | |
_ = GetBoxedStatusCode_LazyArray(500); | |
_ = GetBoxedStatusCode_LazyArray(400); | |
_ = GetBoxedStatusCode_LazyArray(300); | |
return GetBoxedStatusCode_LazyArray(200); | |
} | |
[Benchmark] | |
public void Get100_FrozenDictionary() | |
{ | |
for (int i = 100; i < 200; i++) | |
{ | |
_ = GetBoxedStatusCode_FrozenDictionary(i); | |
} | |
} | |
[Benchmark] | |
public void Get100_LazyArray() | |
{ | |
for (int i = 100; i < 200; i++) | |
{ | |
_ = GetBoxedStatusCode_LazyArray(i); | |
} | |
} | |
private static readonly FrozenDictionary<int, object> BoxedStatusCodes = FrozenDictionary.ToFrozenDictionary(new[] | |
{ | |
KeyValuePair.Create<int, object>(100, 100), | |
KeyValuePair.Create<int, object>(101, 101), | |
KeyValuePair.Create<int, object>(102, 102), | |
KeyValuePair.Create<int, object>(200, 200), | |
KeyValuePair.Create<int, object>(201, 201), | |
KeyValuePair.Create<int, object>(202, 202), | |
KeyValuePair.Create<int, object>(203, 203), | |
KeyValuePair.Create<int, object>(204, 204), | |
KeyValuePair.Create<int, object>(205, 205), | |
KeyValuePair.Create<int, object>(206, 206), | |
KeyValuePair.Create<int, object>(207, 207), | |
KeyValuePair.Create<int, object>(208, 208), | |
KeyValuePair.Create<int, object>(226, 226), | |
KeyValuePair.Create<int, object>(300, 300), | |
KeyValuePair.Create<int, object>(301, 301), | |
KeyValuePair.Create<int, object>(302, 302), | |
KeyValuePair.Create<int, object>(303, 303), | |
KeyValuePair.Create<int, object>(304, 304), | |
KeyValuePair.Create<int, object>(305, 305), | |
KeyValuePair.Create<int, object>(306, 306), | |
KeyValuePair.Create<int, object>(307, 307), | |
KeyValuePair.Create<int, object>(308, 308), | |
KeyValuePair.Create<int, object>(400, 400), | |
KeyValuePair.Create<int, object>(401, 401), | |
KeyValuePair.Create<int, object>(402, 402), | |
KeyValuePair.Create<int, object>(403, 403), | |
KeyValuePair.Create<int, object>(404, 404), | |
KeyValuePair.Create<int, object>(405, 405), | |
KeyValuePair.Create<int, object>(406, 406), | |
KeyValuePair.Create<int, object>(407, 407), | |
KeyValuePair.Create<int, object>(408, 408), | |
KeyValuePair.Create<int, object>(409, 409), | |
KeyValuePair.Create<int, object>(410, 410), | |
KeyValuePair.Create<int, object>(411, 411), | |
KeyValuePair.Create<int, object>(412, 412), | |
KeyValuePair.Create<int, object>(413, 413), | |
KeyValuePair.Create<int, object>(414, 414), | |
KeyValuePair.Create<int, object>(415, 415), | |
KeyValuePair.Create<int, object>(416, 416), | |
KeyValuePair.Create<int, object>(417, 417), | |
KeyValuePair.Create<int, object>(418, 418), | |
KeyValuePair.Create<int, object>(419, 419), | |
KeyValuePair.Create<int, object>(421, 421), | |
KeyValuePair.Create<int, object>(422, 422), | |
KeyValuePair.Create<int, object>(423, 423), | |
KeyValuePair.Create<int, object>(424, 424), | |
KeyValuePair.Create<int, object>(426, 426), | |
KeyValuePair.Create<int, object>(428, 428), | |
KeyValuePair.Create<int, object>(429, 429), | |
KeyValuePair.Create<int, object>(431, 431), | |
KeyValuePair.Create<int, object>(451, 451), | |
KeyValuePair.Create<int, object>(499, 499), | |
KeyValuePair.Create<int, object>(500, 500), | |
KeyValuePair.Create<int, object>(501, 501), | |
KeyValuePair.Create<int, object>(502, 502), | |
KeyValuePair.Create<int, object>(503, 503), | |
KeyValuePair.Create<int, object>(504, 504), | |
KeyValuePair.Create<int, object>(505, 505), | |
KeyValuePair.Create<int, object>(506, 506), | |
KeyValuePair.Create<int, object>(507, 507), | |
KeyValuePair.Create<int, object>(508, 508), | |
KeyValuePair.Create<int, object>(510, 510), | |
KeyValuePair.Create<int, object>(511, 511) | |
}); | |
private static object GetBoxedStatusCode_FrozenDictionary(int statusCode) | |
{ | |
if (BoxedStatusCodes.TryGetValue(statusCode, out var result)) | |
{ | |
return result; | |
} | |
return statusCode; | |
} | |
private static readonly object[] s_boxedStatusCodes = new object[512]; | |
private static object GetBoxedStatusCode_LazyArray(int statusCode) | |
{ | |
object[] boxes = s_boxedStatusCodes; | |
return (uint)statusCode < (uint)boxes.Length | |
? boxes[statusCode] ??= statusCode | |
: statusCode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment