Skip to content

Conversation

@christiaanderidder
Copy link
Contributor

@christiaanderidder christiaanderidder commented Dec 21, 2023

See #96253

Currently to make sure no scoped dependencies are injected into singleton dependencies, the entire tree of a call site is walked for each dependency registered in the DI container. This happens even when that tree has been walked before when validating another call site.

By making a small adjustment to the existing code, we cache each walked call site tree instead of just the trees containing a scoped dependency in one of the branches.

This massively reduces the time it takes to validate the DI container when ValidateOnBuild is used, at the expense of some additional memory needed to store the cached entries.

This solution can most likely be made slightly more efficient by keeping a separate HashSet for just the visited trees instead of inserting null values into the existing ConcurrentDictionary, but I opted to keep the solution as close as possible to the original implementation.

@ghost ghost added area-Extensions-DependencyInjection community-contribution Indicates that the PR has been added by a community member labels Dec 21, 2023
@ghost
Copy link

ghost commented Dec 21, 2023

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.

Issue Details

See #96253

Author: christiaanderidder
Assignees: -
Labels:

area-Extensions-DependencyInjection, community-contribution

Milestone: -

@christiaanderidder
Copy link
Contributor Author

@dotnet-policy-service agree company="ChannelEngine.com B.V."

@christiaanderidder
Copy link
Contributor Author

/azp run runtime

@azure-pipelines
Copy link

Commenter does not have sufficient privileges for PR 96254 in repo dotnet/runtime

@steveharter
Copy link
Contributor

Should the tests added to dotnet/extensions#2374 be added here? And the benchmark added to dotnet/performance?

@steveharter steveharter self-assigned this Jan 10, 2024
@christiaanderidder
Copy link
Contributor Author

Should the tests added to dotnet/extensions#2374 be added here? And the benchmark added to dotnet/performance?

I had a look at the unit tests and added them in. They don't seem to test anything related to this performance fix (as the behavior should remain exactly the same in that regard), but they do seem to cover some untested edge cases.

Regarding the benchmark, I have added it to the dotnet/performance repository, the PR can be found here: dotnet/performance#3878

Copy link
Contributor

@steveharter steveharter left a comment

Choose a reason for hiding this comment

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

Thanks!

@davidfowl
Copy link
Member

Seems like there might be a regression caused by this fix:

#98551

@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Extensions-DependencyInjection community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants