Optimize and simplify delegate layout#99200
Conversation
|
I'm not sure what's up with the failures here, tests that are failing on the CI seem to pass on my machine. |
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
|
/azp run runtime-coreclr gcstress0x3-gcstress0xc |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Could you please collect some perf numbers to give us an idea about the improvements and regressions in the affected areas? We may want to do some optimizations to mitigate the regressions. |
The existing code tries to compare MethodInfos as a cheap fast path. Most delegates do not have cached MethodInfo, so this fast path is hit rarely - but it is very cheap, so it is still worth it. This cheap fast path is not cheap anymore with this change. It may be best to delete the fast path that is trying to compare the MethodInfos and potentially optimize |
I think the thing that'd need benchmarking here are equality checks and maybe the impact of collectible delegates being stored in the CWT on the GC, the rest of things shouldn't be performance sensitive enough to matter I think? I'm not fully sure what'd be the proper way for benchmarking the latter.
I am going to benchmark the impact of the equality change tomorrow, I feel like if the impact won't be big, potential optimizations to it can be done later. |
Write a small program that loads an assembly as collectible and calls a method in it. The method in collectible assembly can create delegates in a loop. (If you would like to do it under benchmarknet, it works too - but it is probably more work.) |
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
src/native/managed/cdac/tests/UnitTests/ObjectTests.cs:450
- GetDelegateInfo now has explicit multicast detection via Delegate.HelperObject being an array, but the unit tests no longer exercise that path (the previous "Multicast" test was repurposed to "Unmanaged"). Please add a multicast test that sets HelperObject to an array object and verifies DelegateType.Unknown (and zeroed Target/MethodPtr) for multicast delegates.
[Theory]
[ClassData(typeof(MockTarget.StdArch))]
public void GetDelegateInfo_Unmanaged(MockTarget.Architecture arch)
{
const ulong TestMethodTable = 0x00000000_10000200;
const ulong TestMethodPtr = 0x00000000_aaaa0000;
const long TestExtraData = -1;
TargetPointer delegateAddress = default;
IObject contract = CreateObjectContract(
arch,
objectBuilder =>
{
delegateAddress = objectBuilder.AddDelegateObject(
methodTable: TestMethodTable,
target: 0,
methodPtr: TestMethodPtr,
methodPtrAux: 0,
extraData: TestExtraData);
});
DelegateInfo info = contract.GetDelegateInfo(delegateAddress);
Assert.Equal(DelegateType.Unknown, info.DelegateType);
Assert.Equal(0ul, info.TargetObject.Value);
Assert.Equal(0ul, info.TargetMethodPtr.Value);
}
|
LGTM once the remaining feedback is addressed |
I'll get to it tomorrow |
Fix Delegate.GetHashCode to return same hashcodes for type-equivalent delegates pointing to the same method. Delegate.Equals returns true for such delegates. GetHashCode contract requires hashcode to be the same in that case. Found during codereview of #99200 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Michał Petryka <35800402+MichalPetryka@users.noreply.github.com> Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
|
@jkotas I think everything should be ready, I decided to go with |
|
/ba-g infrastructure timeouts |
|
Thank you! There are two follow up changes that I would like to see (two separate PRs):
|
Actually, let me do this one - it is trivial and it is going to fix app compat break #130612 |
Fix Delegate.GetHashCode to return same hashcodes for type-equivalent delegates pointing to the same method. Delegate.Equals returns true for such delegates. GetHashCode contract requires hashcode to be the same in that case. Found during codereview of #99200 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Michał Petryka <35800402+MichalPetryka@users.noreply.github.com> Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Reduce size of delegate instance by one pointer field
Contributes to #85014.