Skip to content

Commit 01d662f

Browse files
martincostellokhellang
authored andcommitted
Set dictionary capacity
Set the capacity of the dictionary to exactly the number of items being added.
1 parent c664132 commit 01d662f

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/MimeTypes/MimeTypeFunctions.ttinclude

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
return GetMediaTypes(mimeTypes).ToList();
3333
}
3434

35-
private static IEnumerable<(string Extension, string Type)> GetMediaTypes(IEnumerable<KeyValuePair<string, MimeType>> mimeTypes)
35+
private static IList<(string Extension, string Type)> GetMediaTypes(IEnumerable<KeyValuePair<string, MimeType>> mimeTypes)
3636
=> mimeTypes.Where(x => x.Value.Extensions.Any())
3737
.SelectMany(x => x.Value.Extensions.Select(e => (e, x.Key)))
3838
.Where(x => x.Item1.Length <= 8 && x.Item1.All(char.IsLetterOrDigit))
3939
.GroupBy(x => x.Item1)
4040
.Select(x => x.First())
41-
.OrderBy(x => x.Item1, StringComparer.InvariantCulture);
41+
.OrderBy(x => x.Item1, StringComparer.InvariantCulture)
42+
.ToList();
4243

4344
public void GenerateClass(bool includeAttributes)
4445
{
@@ -83,9 +84,13 @@ namespace $rootnamespace$
8384
{
8485
s_fallbackMimeType = DefaultFallbackMimeType;
8586

86-
s_typeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
87+
<#+
88+
var mediaTypes = GetMediaTypeList();
89+
var mediaTypesCount = mediaTypes.Count.ToString(System.Globalization.CultureInfo.InvariantCulture);
90+
#>
91+
s_typeMap = new Dictionary<string, string>(<#= mediaTypesCount #>, StringComparer.OrdinalIgnoreCase)
8792
{
88-
<#+ foreach (var mediaType in GetMediaTypeList()) { #>
93+
<#+ foreach (var mediaType in mediaTypes) { #>
8994
{ "<#= mediaType.Item1 #>", "<#= mediaType.Item2 #>" },
9095
<#+ } #>
9196
};

src/MimeTypes/MimeTypes.NullableAttributes.cs.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{
3939
s_fallbackMimeType = DefaultFallbackMimeType;
4040

41-
s_typeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
41+
s_typeMap = new Dictionary<string, string>(1180, StringComparer.OrdinalIgnoreCase)
4242
{
4343
{ "123", "application/vnd.lotus-1-2-3" },
4444
{ "1km", "application/vnd.1000minds.decision-model+xml" },

src/MimeTypes/MimeTypes.cs.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{
3939
s_fallbackMimeType = DefaultFallbackMimeType;
4040

41-
s_typeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
41+
s_typeMap = new Dictionary<string, string>(1180, StringComparer.OrdinalIgnoreCase)
4242
{
4343
{ "123", "application/vnd.lotus-1-2-3" },
4444
{ "1km", "application/vnd.1000minds.decision-model+xml" },

0 commit comments

Comments
 (0)