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.) |
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Moved UnmanagedMarker constant declaration to the top of the struct.
Simplified GetMethodDesc by removing unnecessary variable and conditional.
| static MethodDesc* GetMethodDescForOpenVirtualDelegate(OBJECTREF orDelegate); | ||
| static BOOL IsTrueMulticastDelegate(OBJECTREF delegate); | ||
| static MethodDesc* GetMethodDesc(OBJECTREF obj); | ||
| static MethodDesc* GetCachedMethodDesc(DELEGATEREF delegate); |
There was a problem hiding this comment.
| static MethodDesc* GetCachedMethodDesc(DELEGATEREF delegate); | |
| static MethodDesc* GetMethodDescForOpenVirtualDelegate(DELEGATEREF delegate); |
If you apply my other feedback, this can stay with the more specific name
There was a problem hiding this comment.
I don't think the old name makes sense anymore since it will return results for non open virtual delegates.
There was a problem hiding this comment.
The implementation can assert that it is only used for open virtual delegates.
There was a problem hiding this comment.
The implementation can assert that it is only used for open virtual delegates.
I don't think there's an easy way to do that anymore? We'd need to check if the method is virtual and that the delegate is open which need a lot of checks.
There was a problem hiding this comment.
Right, we would have to assert that the aux pointer points into VSD heap. I am not sure how hard is it to put the assert together. Asserting is optional to enforce the precondition. We have many methods in the VM that document the preconditions without asserting them.
It is better to name cross-component calls based on their external contract, not based on the implementation details. It makes the code easier to understand, and it makes refactoring easier. For example, GetMethodDescForOpenVirtualDelegate name is likely to survive changes in implementation details. GetCachedMethodDesc name not so much.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
|
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);
}
| // both delegates are open | ||
| if (_methodPtrAux == other._methodPtrAux) | ||
| return true; |
There was a problem hiding this comment.
Yes, it looks like it.
I think it is only a problem for FEATURE_CACHED_INTERFACE_DISPATCH that is only used in production on iOS.
There was a problem hiding this comment.
We should add a test that hits the issue before fixing it.
| // A null invocationList can be one of the following: | ||
| // - Instance closed, Instance open non-virt, Instance open virtual, Static closed, Static opened, Unmanaged FtnPtr | ||
| // - Instance open virtual is complex and we need to figure out what to do (TODO). | ||
| // For the others the logic is the following: | ||
| // if _methodPtrAux is 0 the target is in _methodPtr, otherwise the taret is _methodPtrAux | ||
| // - Instance closed | ||
| // - Instance open non-virt | ||
| // - Instance open virtual | ||
| // - Static closed | ||
| // - Static opened | ||
| // - Instance open virtual | ||
| // - Unmanaged FtnPtr | ||
| // if _methodPtrAux is 0 the target is in _methodPtr, otherwise the target is _methodPtrAux |
| Contract Constants: | ||
| | Name | Type | Purpose | Value | | ||
| | --- | --- | --- | --- | | ||
| | `UnmanagedMarker` | nint | Sentinel value for detecting unmanaged pointer delegates. | `-1` | | ||
|
|
|
|
||
| MethodTable* methodTable = RuntimeHelpers.GetMethodTable(this); | ||
| #if FEATURE_TYPEEQUIVALENCE | ||
| if (methodTable->HasTypeEquivalence) |
There was a problem hiding this comment.
I have started PR to add a test that hits this path: #130542 . The test should fail against main now, and it should pass once this PR is merged.
|
LGTM once the remaining feedback is addressed |
I'll get to it tomorrow |
First attempt at making delegate GC fields immutable in CoreCLR so that they can be allocated on the NonGC heap.
I've checked it with a simple app and corerun locally with a delegate from an unloadable ALC and it seemed to not crash, assert nor unload the ALC from under the delegate, however I couldn't actually find any runtime tests that would verify delegates from unloadable ALCs work so the CI coverage might be missing.
One small point of concern is that this might make delegate equality checks slower since they rely on checking the methods in the last "slow path" check, which is however always hit for different delegates AFAIR.
Contributes to #85014.
cc @jkotas