-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Regression and Perf Fixes #52956
Regression and Perf Fixes #52956
Conversation
Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti Issue DetailsThis change is fixing two issues:
|
CC @reyang @cijothomas @victlu Thanks to @victlu for catching these issues that early :-) |
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.
LGTM!
The failure in the CI leg |
/backport to release/6.0-preview5 |
Started backporting to release/6.0-preview5: https://github.com/dotnet/runtime/actions/runs/857818026 |
{ | ||
if (measurement is byte byteMeasurement) | ||
if (typeof(T) == typeof(byte)) |
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.
@tarekgh Hey I was just noticing this change. Would you indulge me on a couple of questions?
-
Why remove the
else if
from the checks? -
I understand the switch from
is
totypeof(T)
is because some frameworks allocate onis
. If we added back the old version under a preprocessor directive for >= NET5.0 would it then be allocation free for users on NET5.0 or NET6.0? Right now those users will see at least one boxing from theobject
cast, no?
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.
Why remove the else if from the checks?
It doesn't matter as the JIT optimize the code anyway and will remove all other code when generating type specific code.
I understand the switch from is to typeof(T) is because some frameworks allocate on is. If we added back the old version under a preprocessor directive for >= NET5.0 would it then be allocation free for users on NET5.0 or NET6.0? Right now those users will see at least one boxing from the object cast, no?
No, boxing will be optimized away too. no boxing will occur. try it yourself writing some simple code like:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int NotifyMeasurement<T>(T measurement) where T : struct
{
if (typeof(T) == typeof(int))
{
return (int)(object)measurement;
}
if (typeof(T) == typeof(long))
{
return (int)(long)(object)measurement;
}
return 0;
}
[Benchmark]
public void TestAllocation()
{
int t = 0;
for (int i = 0; i < 10; i++)
{
t += NotifyMeasurement<int>(i);
t += NotifyMeasurement<long>(i);
}
}
and you will see no allocation happening
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
|--------------- |---------:|----------:|----------:|------:|------:|------:|----------:|
| TestAllocation | 6.096 ns | 0.0659 ns | 0.0584 ns | - | - | - | - |
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.
Good stuff thanks @tarekgh! Is this JIT magic documented anywhere? A cheat sheet of things it will optimize away would be great to have.
This change is fixing two issues: