Performance: Add Identifier helper class#9493
Performance: Add Identifier helper class#9493henon merged 4 commits intoMudBlazor:devfrom xC0dex:performance/identifier
Conversation
|
Hey @henon @ScarletKuro what's your opinion on this idea? |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #9493 +/- ##
==========================================
+ Coverage 89.82% 90.57% +0.74%
==========================================
Files 412 407 -5
Lines 11878 12715 +837
Branches 2364 2463 +99
==========================================
+ Hits 10670 11516 +846
+ Misses 681 638 -43
- Partials 527 561 +34 ☔ View full report in Codecov by Sentry. |
|
Wouldn't something like this be better if we chase performance / allocation? private const string Chars = "abcdefghijklmnopqrstuvwxyz0123456789";
private const int RandomStringLength = 8;
internal static string Create(ReadOnlySpan<char> prefix)
{
Span<char> identifierSpan = stackalloc char[prefix.Length + RandomStringLength];
prefix.CopyTo(identifierSpan);
Span<byte> randomBytes = stackalloc byte[RandomStringLength];
RandomNumberGenerator.Fill(randomBytes);
for (int i = 0; i < RandomStringLength; i++)
{
identifierSpan[prefix.Length + i] = Chars[randomBytes[i] % Chars.Length];
}
return identifierSpan.ToString();
}I feel like |
|
Sure, would suffice. I do something similar in my apps actually. |
|
You are right. I didn't think about that, great idea. However, the allocations on the heap are unchanged, but it's faster. Now I have the following code, but "only" the private const string Chars = "abcdefghijklmnopqrstuvwxyz0123456789";
private const int CharsLength = 35;
private const int RandomStringLength = 8;
internal static string Create(ReadOnlySpan<char> prefix)
{
Span<char> identifierSpan = stackalloc char[prefix.Length + RandomStringLength];
prefix.CopyTo(identifierSpan);
for (var i = 0; i < RandomStringLength; i++)
{
var index = Random.Shared.Next(CharsLength);
identifierSpan[prefix.Length + i] = Chars[index];
}
return identifierSpan.ToString();
}with these results:
|
|
I would update all references where the identifier can be used 👍 |
|
I have three notes:
|
As long as it doesn't break any unit tests, etc., then I'm okay with the rename. The shorter, the better.
No.
Yes. |
Okay, they do break. I'll leave it as it is. PR is ready 👍. |
|
Thanks! |
Hey,
this PR adds a new helper class
Identifierwhich provides aCreatemethod to create a unique string with fewer allocations than the current approach.Benchmark:
Currently, I only updated the
MudCheckBoxcomponent. Depending on your feedback, I would update all usages.How Has This Been Tested?
Added tests.
Type of Changes
Checklist
dev).