-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change IOptionsSnapshot to reuse options instances across scopes #56271
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
31ac591
Change IOptionsSnapshot to reuse options instances across scopes
NinoFloris 6fd7f10
Improve snapshot perf for unnamed option and delay dictionary alloc
NinoFloris 0b4c719
Address review feedback
NinoFloris 48fc34b
Update src/libraries/Microsoft.Extensions.Options/src/OptionsSnapshot.cs
NinoFloris File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/libraries/Microsoft.Extensions.Options/src/OptionsSnapshot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading; | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="IOptionsSnapshot{TOptions}"/>. | ||
/// </summary> | ||
/// <typeparam name="TOptions">Options type.</typeparam> | ||
internal sealed class OptionsSnapshot<[DynamicallyAccessedMembers(Options.DynamicallyAccessedMembers)] TOptions> : | ||
IOptionsSnapshot<TOptions> | ||
where TOptions : class | ||
{ | ||
private readonly IOptionsMonitor<TOptions> _optionsMonitor; | ||
|
||
private volatile ConcurrentDictionary<string, TOptions> _cache; | ||
private volatile TOptions _unnamedOptionsValue; | ||
|
||
/// <summary> | ||
/// Initializes a new instance with the specified options configurations. | ||
/// </summary> | ||
/// <param name="optionsMonitor">The options monitor to use to provide options.</param> | ||
public OptionsSnapshot(IOptionsMonitor<TOptions> optionsMonitor) | ||
{ | ||
_optionsMonitor = optionsMonitor; | ||
} | ||
|
||
/// <summary> | ||
/// The default configured <typeparamref name="TOptions"/> instance, equivalent to Get(Options.DefaultName). | ||
/// </summary> | ||
public TOptions Value => Get(Options.DefaultName); | ||
|
||
/// <summary> | ||
/// Returns a configured <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>. | ||
/// </summary> | ||
public TOptions Get(string name) | ||
{ | ||
if (name == null || name == Options.DefaultName) | ||
{ | ||
if (_unnamedOptionsValue is TOptions value) | ||
{ | ||
return value; | ||
} | ||
|
||
return _unnamedOptionsValue = _optionsMonitor.Get(Options.DefaultName); | ||
} | ||
|
||
var cache = _cache ?? Interlocked.CompareExchange(ref _cache, new(concurrencyLevel: 1, capacity: 5, StringComparer.Ordinal), null) ?? _cache; | ||
|
||
#if NETSTANDARD2_1 | ||
TOptions options = cache.GetOrAdd(name, static (name, optionsMonitor) => optionsMonitor.Get(name), _optionsMonitor); | ||
#else | ||
if (!cache.TryGetValue(name, out TOptions options)) | ||
{ | ||
options = cache.GetOrAdd(name, _optionsMonitor.Get(name)); | ||
} | ||
#endif | ||
return options; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephentoub can you look here? I think it's copied from the other pattern we use in Options<T>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern looks fine. It's saying:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm waiting for your book to come out. Concurrency patterns with @stephentoub