-
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
A delegate object is created for each IOptionsMonitor<T>.Get call #61086
Comments
Tagging subscribers to this area: @maryamariyan Issue Details
It could be avoided if the delegate is a member, or simply Add a TryGetValue call before calling GetOrAdd. The object allocation is much more expensive than the getting itself, when CurrentValue/Get is used in hot code path, the cost will be unexpected.
|
We can fix this by not using the IOptionsCache. It's possible we can use the concrete type if it hasn't been overridden as an optimization. |
@davidfowl I didn't get why we need the IOptionsMonitorCache service, if OptionsMonitor maintains the cache itself, everything will be much easier. I didn't find IOptionsMonitorCache is used anywhere else, could we just remove it or add some more methods? |
For what it's worth, I came across this as a hot path allocation today in an application using |
There isn't one: runtime/src/libraries/Microsoft.Extensions.Options/src/IOptionsMonitorCache.cs Lines 13 to 43 in 8b1a0c8
|
Isn't this a clear case where a new interface method with a default implementation would be perfect? It seems really simple to add a TArg version that would just allocate and call the existing GetOrAdd in the default implementation. |
…66688) * Avoid allocating a delegate in OptionsMonitor.Get() when possible. Fix #61086 * Address feedback from #66688 (comment) * Use locals to avoid unnecessary closure allocations. * Remove another closure allocation in OptionsMonitor and add test for #61086.
runtime/src/libraries/Microsoft.Extensions.Options/src/OptionsMonitor.cs
Line 89 in df5b118
It could be avoided if the delegate is a member, or simply Add a TryGetValue call before calling GetOrAdd.
The object allocation is much more expensive than the getting itself, when CurrentValue/Get is used in hot code path, the cost will be unexpected.
The text was updated successfully, but these errors were encountered: