Skip to content

Commit

Permalink
Merge pull request #508 from microsoft/rossgrambo-allocation-cleanup
Browse files Browse the repository at this point in the history
Allocation cleanup
  • Loading branch information
rossgrambo authored Oct 30, 2024
2 parents f47e188 + 60b0e34 commit e1e98b5
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 126 deletions.
7 changes: 7 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Why this PR?

-motivation for making this change-

## Visible Changes

-changes that are visible to developers using this library-
6 changes: 3 additions & 3 deletions examples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/ConsoleApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
}
]
}
}
}
6 changes: 3 additions & 3 deletions examples/TargetingConsoleApp/TargetingConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public FeatureGateAttribute(RequirementType requirementType, params object[] fea
public RequirementType RequirementType { get; }

/// <summary>
/// Performs controller action pre-procesing to ensure that any or all of the specified features are enabled.
/// Performs controller action pre-processing to ensure that any or all of the specified features are enabled.
/// </summary>
/// <param name="context">The context of the MVC action.</param>
/// <param name="next">The action delegate.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public sealed class ConfigurationFeatureDefinitionProvider : IFeatureDefinitionP
// provider to be marked for caching as well.

private readonly IConfiguration _configuration;
private readonly ConcurrentDictionary<string, FeatureDefinition> _definitions;
private readonly ConcurrentDictionary<string, Task<FeatureDefinition>> _definitions;
private IDisposable _changeSubscription;
private int _stale = 0;
private Func<string, Task<FeatureDefinition>> _getFeatureDefinitionFunc;

const string ParseValueErrorString = "Invalid setting '{0}' with value '{1}' for feature '{2}'.";

Expand All @@ -37,11 +38,16 @@ public sealed class ConfigurationFeatureDefinitionProvider : IFeatureDefinitionP
public ConfigurationFeatureDefinitionProvider(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_definitions = new ConcurrentDictionary<string, FeatureDefinition>();
_definitions = new ConcurrentDictionary<string, Task<FeatureDefinition>>();

_changeSubscription = ChangeToken.OnChange(
() => _configuration.GetReloadToken(),
() => _stale = 1);

_getFeatureDefinitionFunc = (featureName) =>
{
return Task.FromResult(GetMicrosoftSchemaFeatureDefinition(featureName) ?? GetDotnetSchemaFeatureDefinition(featureName));
};
}

/// <summary>
Expand Down Expand Up @@ -86,10 +92,7 @@ public Task<FeatureDefinition> GetFeatureDefinitionAsync(string featureName)
_definitions.Clear();
}

return Task.FromResult(
_definitions.GetOrAdd(
featureName,
(_) => GetMicrosoftSchemaFeatureDefinition(featureName) ?? GetDotnetSchemaFeatureDefinition(featureName)));
return _definitions.GetOrAdd(featureName, _getFeatureDefinitionFunc);
}

/// <summary>
Expand All @@ -98,7 +101,7 @@ public Task<FeatureDefinition> GetFeatureDefinitionAsync(string featureName)
/// <returns>An enumerator which provides asynchronous iteration over feature definitions.</returns>
//
// The async key word is necessary for creating IAsyncEnumerable.
// The need to disable this warning occurs when implementaing async stream synchronously.
// The need to disable this warning occurs when implementing async stream synchronously.
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()
#pragma warning restore CS1998
Expand All @@ -121,7 +124,7 @@ public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()

//
// Underlying IConfigurationSection data is dynamic so latest feature definitions are returned
FeatureDefinition definition = _definitions.GetOrAdd(featureName, (_) => ParseMicrosoftSchemaFeatureDefinition(featureSection));
FeatureDefinition definition = _definitions.GetOrAdd(featureName, _getFeatureDefinitionFunc).Result;

if (definition != null)
{
Expand All @@ -142,7 +145,7 @@ public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()

//
// Underlying IConfigurationSection data is dynamic so latest feature definitions are returned
FeatureDefinition definition = _definitions.GetOrAdd(featureName, (_) => ParseDotnetSchemaFeatureDefinition(featureSection));
FeatureDefinition definition = _definitions.GetOrAdd(featureName, _getFeatureDefinitionFunc).Result;

if (definition != null)
{
Expand All @@ -155,32 +158,32 @@ private FeatureDefinition GetDotnetSchemaFeatureDefinition(string featureName)
{
IEnumerable<IConfigurationSection> dotnetFeatureDefinitionSections = GetDotnetFeatureDefinitionSections();

IConfigurationSection configuration = dotnetFeatureDefinitionSections
IConfigurationSection dotnetFeatureDefinitionConfiguration = dotnetFeatureDefinitionSections
.FirstOrDefault(section =>
string.Equals(section.Key, featureName, StringComparison.OrdinalIgnoreCase));

if (configuration == null)
if (dotnetFeatureDefinitionConfiguration == null)
{
return null;
}

return ParseDotnetSchemaFeatureDefinition(configuration);
return ParseDotnetSchemaFeatureDefinition(dotnetFeatureDefinitionConfiguration);
}

private FeatureDefinition GetMicrosoftSchemaFeatureDefinition(string featureName)
{
IEnumerable<IConfigurationSection> microsoftFeatureDefinitionSections = GetMicrosoftFeatureDefinitionSections();

IConfigurationSection configuration = microsoftFeatureDefinitionSections
IConfigurationSection microsoftFeatureDefinitionConfiguration = microsoftFeatureDefinitionSections
.LastOrDefault(section =>
string.Equals(section[MicrosoftFeatureManagementFields.Id], featureName, StringComparison.OrdinalIgnoreCase));

if (configuration == null)
if (microsoftFeatureDefinitionConfiguration == null)
{
return null;
}

return ParseMicrosoftSchemaFeatureDefinition(configuration);
return ParseMicrosoftSchemaFeatureDefinition(microsoftFeatureDefinitionConfiguration);
}

private IEnumerable<IConfigurationSection> GetDotnetFeatureDefinitionSections()
Expand Down Expand Up @@ -518,7 +521,7 @@ private FeatureDefinition ParseMicrosoftSchemaFeatureDefinition(IConfigurationSe
};
}

private T ParseEnum<T>(string feature, string rawValue, string fieldKeyword)
private static T ParseEnum<T>(string feature, string rawValue, string fieldKeyword)
where T : struct, Enum
{
Debug.Assert(!string.IsNullOrEmpty(rawValue));
Expand All @@ -533,7 +536,7 @@ private T ParseEnum<T>(string feature, string rawValue, string fieldKeyword)
return value;
}

private double ParseDouble(string feature, string rawValue, string fieldKeyword)
private static double ParseDouble(string feature, string rawValue, string fieldKeyword)
{
Debug.Assert(!string.IsNullOrEmpty(rawValue));

Expand All @@ -547,7 +550,7 @@ private double ParseDouble(string feature, string rawValue, string fieldKeyword)
return value;
}

private bool ParseBool(string feature, string rawValue, string fieldKeyword)
private static bool ParseBool(string feature, string rawValue, string fieldKeyword)
{
Debug.Assert(!string.IsNullOrEmpty(rawValue));

Expand Down
Loading

0 comments on commit e1e98b5

Please sign in to comment.