Skip to content
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 dictionary lookup for singleton services #52035

Merged
merged 6 commits into from
Apr 30, 2021

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Apr 29, 2021

  • Stash the instance on the callsite itself and avoid the resolved services lookup.
  • This mainly benefits the reflection only path as the ILEmit codegen path will replace this code with an optimized delegate. This should help resolving singletons in the blazor code path and for nativeAOT.
  • The other benefit is that the concurrent dictionary that was used before to store singletons will be empty in the common case.
  • This change also introduces callsite caching (memoization). Today callsites are cached but only the root type (A -> B -> C), GetService<A> will only cache the A callsite. This change will result in caching A, B and C.

PS: Still testing this.

- Stash the instance on the callsite itself and avoid the resolved services lookup.
@ghost
Copy link

ghost commented Apr 29, 2021

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details
  • Stash the instance on the callsite itself and avoid the resolved services lookup.
Author: davidfowl
Assignees: -
Labels:

area-Extensions-DependencyInjection

Milestone: -

@davidfowl davidfowl requested a review from eerhardt April 29, 2021 04:58
- Don't lock if the callsite value is already resolved
- Make the value on ServiceCallsite volatile
- cache callsites
- update tests
@davidfowl davidfowl requested a review from pakrym April 29, 2021 14:55
@davidfowl
Copy link
Member Author

/azp list

@davidfowl
Copy link
Member Author

/azp run runtime

@davidfowl
Copy link
Member Author

/azp run runtime-staging

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@pakrym
Copy link
Contributor

pakrym commented Apr 29, 2021

LGTM, good change.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@@ -5,7 +5,7 @@

namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
{
internal struct ServiceCacheKey: IEquatable<ServiceCacheKey>
internal readonly struct ServiceCacheKey : IEquatable<ServiceCacheKey>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to your changes, but the HashCode calculation will throw for the static Empty, the Type is null then, and GetHashCode will throw a null ref exception.
I suggest changing it to HashCode.Combine(Type, Slot) which takes care of the null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do that in a different PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #52115 to fix that nullref exception.

@danmoseley
Copy link
Member

@dotnet/dnceng can you please investigate what happened to Android legs?

 Job 09719656-7f08-43c1-913f-c65ec565ba97 on Ubuntu.1804.Amd64.Android.Open is completed with 217 finished work items.
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error : System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error). [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error :    at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error :    at Microsoft.DotNet.Helix.Sdk.CreateTestsForWorkItems.<>c__DisplayClass5_0.<<AttachResultFileToTestResultAsync>b__0>d.MoveNext() in /_/src/Microsoft.DotNet.Helix/Sdk/CreateFailedTestsForFailedWorkItems.cs:line 73 [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error : --- End of stack trace from previous location --- [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error :    at Microsoft.DotNet.Helix.AzureDevOps.AzureDevOpsTask.<>c__DisplayClass13_0.<<RetryAsync>b__0>d.MoveNext() in /_/src/Microsoft.DotNet.Helix/Sdk/AzureDevOpsTask.cs:line 91 [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error : --- End of stack trace from previous location --- [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error :    at Microsoft.DotNet.Helix.AzureDevOps.AzureDevOpsTask.RetryAsync[T](Func`1 function, Action`1 logRetry, Func`2 isRetryable, CancellationToken cancellationToken) in /_/src/Microsoft.DotNet.Helix/Sdk/AzureDevOpsTask.cs:line 206 [/__w/1/s/src/libraries/sendtohelixhelp.proj]
/__w/1/s/.packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error :    at Microsoft.DotNet.Helix.AzureDevOps.AzureDevOpsTask.RetryAsync(Func`1 function) in /_/src/Microsoft.DotNet.Helix/Sdk/AzureDevOpsTask.cs:line 86 [/__w/1/s/src/libraries/sendtohelixhelp.proj]
##[error].packages/microsoft.dotnet.helix.sdk/6.0.0-beta.21222.1/tools/azure-pipelines/AzurePipelines.MultiQueue.targets(30,5): error : (NETCORE_ENGINEERING_TELEMETRY=Helix) System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at Microsoft.DotNet.Helix.Sdk.CreateTestsForWorkItems.<>c__DisplayClass5_0.<<AttachResultFileToTestResultAsync>b__0>d.MoveNext() in /_/src/Microsoft.DotNet.Helix/Sdk/CreateFailedTestsForFailedWorkItems.cs:line 73

I will rerun them

@missymessa
Copy link
Member

@danmoseley based on the error message, this looks related to the issue we noticed yesterday: https://github.com/dotnet/core-eng/issues/12969

@danmoseley
Copy link
Member

Thanks @missymessa . I also notice that there were test failures (https://dev.azure.com/dnceng/9ee6d478-d288-47f7-aacc-f6e6d082ae6d/_apis/build/builds/1115588/logs/91) but none of them were reported here. Is that related too?

@missymessa
Copy link
Member

Looks like the test failures are showing up in AzDO. Are you meaning that they're not showing up on the Build Result Analysis? I think that's due to not having proper multi-pipeline support right now.

@danmoseley
Copy link
Member

What are these failures

    System.Runtime.InteropServices.Tests.MarshalComDisabledTests.CreateAggregatedObject_ThrowsNotSupportedException [FAIL]
      Assert.Throws() Failure
      Expected: typeof(System.NotSupportedException)
      Actual:   typeof(System.PlatformNotSupportedException): COM Interop is not supported on this platform.
      ---- System.PlatformNotSupportedException : COM Interop is not supported on this platform.
      Stack Trace:
        /_/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NoCom.cs(23,0): at System.Runtime.InteropServices.Marshal.CreateAggregatedObject(IntPtr pOuter, Object o)
        /_/src/libraries/System.Runtime.InteropServices/tests/ComDisabled/System/Runtime/InteropServices/Marshal/MarshalComDisabledTests.cs(19,0): at System.Runtime.InteropServices.Tests.MarshalComDisabledTests.<>c__DisplayClass1_0.<CreateAggregatedObject_ThrowsNotSupportedException>b__0()
        ----- Inner Stack Trace -----
        /_/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NoCom.cs(23,0): at System.Runtime.InteropServices.Marshal.CreateAggregatedObject(IntPtr pOuter, Object o)
        /_/src/libraries/System.Runtime.InteropServices/tests/ComDisabled/System/Runtime/InteropServices/Marshal/MarshalComDisabledTests.cs(19,0): at System.Runtime.InteropServices.Tests.MarshalComDisabledTests.<>c__DisplayClass1_0.<CreateAggregatedObject_ThrowsNotSupportedException>b__0()

https://helixre8s23ayyeko0k025g8.blob.core.windows.net/dotnet-runtime-refs-pull-52035-merge-92c55ea76bff450dba/System.Runtime.InteropServices.ComDisabled.Tests/console.74d5849f.log?sv=2019-07-07&se=2021-05-20T07%3A25%3A35Z&sr=c&sp=rl&sig=EYVspnfSzruqdpqPZhSFmbpm9zO4vGi%2BbBsDfSpexnQ%3D

@elinor-fung ? I can't see a relevant change in a quick look.

@danmoseley
Copy link
Member

It seems to be from #50662 @LakshanF

@danmoseley
Copy link
Member

@missymessa it's not clear now because I clicked rerun failed tests 😞 but I thought I wasn't seeing failed checkmarks on this PR page. Perhaps I've lost the repro state now.

@elinor-fung
Copy link
Member

Thanks, @danmoseley. We shouldn't be running those on Mono - #52128

@LakshanF looks like we missed seeing the introduced test failures on mono+Windows in runtime-staging (test failures show up as passed checks on the PR) - something to make sure to check as the COM trimming work continues.

@davidfowl davidfowl merged commit 17b881c into main Apr 30, 2021
@jkotas jkotas deleted the davidfowl/skip-second-lookup branch May 3, 2021 14:59
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants