-
Notifications
You must be signed in to change notification settings - Fork 736
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
Improve GetUri #2947
Merged
Merged
Improve GetUri #2947
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa1f3f0
Improve GetUri
Tornhoof d045602
Push namespace usage down to get around IDE0005
Tornhoof bfabcd1
Merge branch 'main' into ImproveGetUri
reyang f0e8c1b
Merge branch 'main' into ImproveGetUri
cijothomas d54a24b
Add line to changelog
Tornhoof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would hoist the delegate to a static field. Eg:
Invoke like this:
Why do that?
As it is written we are relying on the compiler to do the right thing and not allocate a delegate for each invocation. Sometimes it does the right thing, sometimes it does not. It is a bit mysterious to me as to the reasons for that. But even if it does decide to do the right thing, the emitted IL will be to check if it has created a static, and if not, create the static, and then do the invocation. So it is a bit better perf to just make the static initially and avoid the check on each call.
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.
Mysterious?, it depends on capturing variables, that's why you can now have static expressions and static local methods etc. You're correct that the current code will do a null check against the static method and initiate if required.
I still think it's a bad idea, because the static field would be located (if I follow the other source codes with static fields, including this type) up above the constructor, between other fields:
opentelemetry-dotnet/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs
Lines 44 to 46 in aa1f3f0
And that reduces the readability of the class way too much.
As for performance:
The null check could be elided by Tier2 compilation with static/dynamic pgo, I don't know if that happens here though. The performance difference is not high, the noise in my benchmark runs is higher (I've seen everything between 1ns to 3ns difference):
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.
A local function with captures is always going to allocate. The mysterious part I was referring to is sometimes it will allocate a delegate even for static local functions.
Don't believe me? Check this out...
CallDoSomethingNoAllocation
compiles to...CallDoSomethingAllocation
compiles to...Why did
CallDoSomethingAllocation
decide to allocate a delegate? Who knows! That's the mystery I'm referring to. Something to do with the generic, I think.Anyway from what I have seen the pattern I'm recommending always works to bypass allocations and has a speed benefit so for me the tradeoff in readability is worth it 😄
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.
If I enter your example in https://sharplab.io/#v2:EYLgtghglgdgNAFxFANgHwAICYCMBYAKAwGYACbUgYVIG9DSHT7GWWAHAJygDcIEBTcjgBspDvwgATAPYwUATyFYAPAAoMOAAxxSsBAEoAfKQBiUfikmUIKFMAgBjANakAvKVXqtpAHIQw/Dp6pACSkvqkAM4IfPwRrsY0pAC+ANzMrORkGqIYACxUNigAItIAytIBCAAWsADmPtIAgrbSDnxQsqr6Gax0BJmZpRVVtTB1qmYWVkX2zjqqAERg8tGxizqa+vrpA4PJvSwkQrkF1rbDlfw19S0obR1dPXuZ/YOsl6P1qgAyDyjnOyOJwLZarGICDakLY7Q6DHLkAp/doA2bAzwaTS+fyBXQwBChcJRCFxOGvMmsA4vSkU44I/KkT7XMZ1ZQAFUMXhUHNIKKB81IbOJsWe7ze70YfLmTlUawEsOpLCpVMIQA==
Both do ldsfld. So depending on where you've seen that code, it might be some bug or unoptimized code.
Apparently the old netfx does it your way.
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.
What I sent is compiled/optimized with .NET 6.
Not familiar with sharplab. Looks like your link is using a newer roslyn? Cool to see they are making improvements to this! Here is a link that shows the same:
https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAE9MGFMDeqmJmxpFFADgE5gBuAhsAKbYIBsmNLjAJgHsAdhACe7OAB4AFDAQAGADSYwQ4AEoAfJgBiYFhD65GECACNGAYwDWmALyZpshZgByjALYtlq4JgCSfOqYAM7AzCzBdtoEmAC+ANzklNhYclwwACx4JhAAIgIAygJewAAWqgDmrgIAgqYClsxgwtLqyZREKCkpBcWlFUKV0noGRrkWNsrSAEQeomERM8ry6upJ3T1xHRQY7BnZxqZ9JSzlVfUQjc2t7ZspXT2UJwNV0gAy1xBH5lbW03MFuFWMtMKt1jseulsNlPk1vhM/k45PI3J5vCo1AEgqFgZFIQ8CZRtvdiUS9tCspgXmdBpVJAAVTTOKRMzDw35TTAM3ERO5PR5PUgcybWaSLVgQ0kUEkk1BAA===
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.
The newer Version includes dotnet/roslyn#58288 which is what you're missing in the other type.
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.
@Tornhoof Thanks for the link. You are kind of making my point for me 😄 It is a magical thing that requires (currently) experimental compiler features to work sometimes (there are limitations on that PR). I prefer to use the fail-safe pattern (which also has the nice benefit of being faster).
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.
In this case your problem does not apply, my link just shows that the behavior you're seeing is fixed for C#11 for the work on caching delegates for method group conversions, because that's what your code does.
If you do it without method group call conversion, e.g.
DoSomething(q => LocalCallback(q), ("mystate", 0));
it will generate the ldsfld correctly even in old compiler versions (including netfx).My link was just to tell you that you can skip doing the static functor dance in C#11 and use method groups without them to your heart's content.
In case of the span code, it never did that as it is not a method group conversion call, but a proper functor call with arguments.
As I've shown that the performance difference is negligable, the only remaining point is readability. And I highly value readability of a class over a minor perf enhancement.
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.
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.
To unblock, it'll propose to merge this now, and create a new issue tracking further improvements/discussions on this.
This is an internal implementation detail, and involves no public API change, so we can change it anytime.