Skip to content

Commit

Permalink
Remove most LINQ usage from Microsoft.Extensions.Configuration (#44825)
Browse files Browse the repository at this point in the history
Resulting in several hundred Func, IEnumerable, set, and string allocations at startup.
  • Loading branch information
stephentoub authored Nov 18, 2020
1 parent 35e535e commit 57c14a2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Configuration
Expand Down Expand Up @@ -78,11 +77,14 @@ public IEnumerable<string> GetChildKeys(
string parentPath)
{
IConfiguration section = parentPath == null ? _config : _config.GetSection(parentPath);
IEnumerable<IConfigurationSection> children = section.GetChildren();
var keys = new List<string>();
keys.AddRange(children.Select(c => c.Key));
return keys.Concat(earlierKeys)
.OrderBy(k => k, ConfigurationKeyComparer.Instance);
foreach (IConfigurationSection child in section.GetChildren())
{
keys.Add(child.Key);
}
keys.AddRange(earlierKeys);
keys.Sort(ConfigurationKeyComparer.Comparison);
return keys;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class ConfigurationKeyComparer : IComparer<string>
/// </summary>
public static ConfigurationKeyComparer Instance { get; } = new ConfigurationKeyComparer();

/// <summary>A comparer delegate with the default instance.</summary>
internal static Comparison<string> Comparison { get; } = Instance.Compare;

/// <summary>
/// Compares two strings.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.Primitives;

Expand Down Expand Up @@ -62,13 +62,35 @@ public virtual IEnumerable<string> GetChildKeys(
IEnumerable<string> earlierKeys,
string parentPath)
{
string prefix = parentPath == null ? string.Empty : parentPath + ConfigurationPath.KeyDelimiter;
var results = new List<string>();

return Data
.Where(kv => kv.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
.Select(kv => Segment(kv.Key, prefix.Length))
.Concat(earlierKeys)
.OrderBy(k => k, ConfigurationKeyComparer.Instance);
if (parentPath is null)
{
foreach (KeyValuePair<string, string> kv in Data)
{
results.Add(Segment(kv.Key, 0));
}
}
else
{
Debug.Assert(ConfigurationPath.KeyDelimiter == ":");

foreach (KeyValuePair<string, string> kv in Data)
{
if (kv.Key.Length > parentPath.Length &&
kv.Key.StartsWith(parentPath, StringComparison.OrdinalIgnoreCase) &&
kv.Key[parentPath.Length] == ':')
{
results.Add(Segment(kv.Key, parentPath.Length + 1));
}
}
}

results.AddRange(earlierKeys);

results.Sort(ConfigurationKeyComparer.Comparison);

return results;
}

private static string Segment(string key, int prefixLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Primitives;

Expand Down Expand Up @@ -66,7 +65,7 @@ public string this[string key]
}
set
{
if (!_providers.Any())
if (_providers.Count == 0)
{
throw new InvalidOperationException(SR.Error_NoSources);
}
Expand Down

0 comments on commit 57c14a2

Please sign in to comment.