-
-
Notifications
You must be signed in to change notification settings - Fork 837
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
Avoid redundant null-checking #1429
base: develop
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #1429 +/- ##
===========================================
+ Coverage 78.60% 87.73% +9.12%
===========================================
Files 200 200
Lines 5716 5682 -34
Branches 1160 1143 -17
===========================================
+ Hits 4493 4985 +492
+ Misses 712 696 -16
+ Partials 511 1 -510
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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 extra-null-check-removal looks fair, but the extra if-defs make it a bunch more complicated.
src/Autofac/ResolutionExtensions.cs
Outdated
@@ -194,6 +194,10 @@ public static bool IsRegistered(this IComponentContext context, Type serviceType | |||
/// <returns>True if the service is registered.</returns> | |||
public static bool IsRegisteredService(this IComponentContext context, Service service) | |||
{ | |||
#if NET7_0_OR_GREATER |
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 don't like adding a load of additional #if
blocks like this just to optimise the null check. Can you provide a benchmark with evidence of the gain? It would have to be a pretty big improvement to justify I think.
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.
Can you provide a benchmark with evidence of the gain
It will be difficult to model the impact of CPU I-cache pollution on a real application.
Typical benchmarks contain little code and CPU cache is big enough.
And big-code benchmark would have many other, may be volatile, factors
I don't like adding a load of additional #if
There is other way: to write custom ThrowHelper
with the same functionality as ArgumentNullException.ThrowIfNull()
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.
We've definitely avoided adding the ThrowIfNull thing in the past simply because the maintenance overhead has outweighed the micro-perf you might get from that helper. I don't think they do this in the core framework libraries, either.
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 don't think they do this in the core framework libraries
They do this since the beginning of the "Core fork". e.g.
https://github.com/dotnet/runtime/blame/a0e9764708d5781168c22d86ccb7437f1e54a915/src/coreclr/src/mscorlib/src/System/Collections/Generic/Dictionary.cs#L106
This is not exacly .ThrowIfNull()
, but the key idea is the same - to extract verbose throw
code into separate function to avoid caching it in the CPU
Reverted Also:
|
ResolutionExtensions.TryResolveService()
already checkscontext != null
No need to check for this in any caller.
Each branching has significant overhead in modern CPUs with high pipe-lining.
Also:
ArgumentNullException.ThrowIfNull()
instead of directif
s .it is more efficient as explained here: https://dunnhq.com/posts/2022/throw-helper/
(less of "cold code" would be cached in CPU I-cache)