-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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 new Dictionary AlternateLookup to avoid allocation in DictionaryJumpTable #58305
Use new Dictionary AlternateLookup to avoid allocation in DictionaryJumpTable #58305
Conversation
Awesome, was looking forward to aspnetcore adopting the new alternate lookup feature. Could you run the micro-benchmarks before and after this change? |
Interestingly enough the PR looks to be slightly slower (5ns -> 11ns) with the benchmark as-is whereas I would have expected no difference. However there's a flaw with the benchmark in that it currently always compares the entire string which is unusual and which negates the benefit proposed in this PR. In most real-world cases the PathSegment is only part of the string and so substrings are allocated. I've modified the benchmark to account for this by using only part of the path string. The modified benchmark results are below. Overall less significant than I would have liked but it's faster and allocates less memory but only if we accept PathSegment usually points to a substring of Path. [edit: I confirmed that this is always the case since all paths start with Before:
After:
|
@@ -27,7 +27,7 @@ public void Setup() | |||
|
|||
for (var i = 0; i < _strings.Length; i++) | |||
{ | |||
_segments[i] = new PathSegment(0, _strings[i].Length); | |||
_segments[i] = new PathSegment(1, _strings[i].Length - 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be - 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I attempted to trim one from the start and one from the end but I don't suppose it matters too much as long as it's a substring.
I've created dotnet/runtime#108732 which if accepted/merged should further improve the performance of the AlternateLookup used in this PR. |
/azp run Was waiting for the runtime PR so we could see the full perf gains. But since it's taking time to get a review we can just merge this since it's still goodness. |
Command 'run
Was' is not supported by Azure Pipelines.
See additional documentation. |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
Use new Dictionary AlternateLookup to avoid allocation in DictionaryJumpTable
Description
Simple change which removes the
substring
allocation in DictionaryJumpTable using theGetAlternateLookup<ReadOnlySpan<char>>
feature of .NET 9.With this change all JumpTable implementations can accept
ReadOnlySpan<char>
as a parameter instead ofstring, PathSegment
. I'd be happy to include that change if it's wanted.