Skip to content

Commit c04e407

Browse files
authored
Use frozen collections for improved performance (#738)
1 parent 2e81d1b commit c04e407

8 files changed

Lines changed: 102 additions & 54 deletions

File tree

docs/exclusion.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ All binary files as defined by https://github.com/sindresorhus/binary-extensions
361361
"zip",
362362
"zipx"
363363
```
364-
<sup><a href='/src/MarkdownSnippets/Reading/Exclusions/SnippetFileExclusions.cs#L30-L299' title='Snippet source file'>snippet source</a> | <a href='#snippet-BinaryFileExtensions' title='Start of snippet'>anchor</a></sup>
364+
<sup><a href='/src/MarkdownSnippets/Reading/Exclusions/SnippetFileExclusions.cs#L47-L316' title='Snippet source file'>snippet source</a> | <a href='#snippet-BinaryFileExtensions' title='Start of snippet'>anchor</a></sup>
365365
<!-- endSnippet -->
366366

367367

@@ -378,5 +378,5 @@ Files that cannot contain comments are excluded.
378378
"geojson",
379379
"sln"
380380
```
381-
<sup><a href='/src/MarkdownSnippets/Reading/Exclusions/SnippetFileExclusions.cs#L16-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-NoAcceptCommentsExtensions' title='Start of snippet'>anchor</a></sup>
381+
<sup><a href='/src/MarkdownSnippets/Reading/Exclusions/SnippetFileExclusions.cs#L14-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-NoAcceptCommentsExtensions' title='Start of snippet'>anchor</a></sup>
382382
<!-- endSnippet -->

src/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
<PackageVersion Include="Polyfill" Version="9.7.2" />
1313
<PackageVersion Include="ProjectDefaults" Version="1.0.170" />
1414
<PackageVersion Include="ProjectFiles" Version="0.4.0" />
15+
<PackageVersion Include="System.Collections.Immutable" Version="10.0.2" />
1516
<PackageVersion Include="System.Memory" Version="4.6.3" />
1617
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
1718
<PackageVersion Include="Verify.DiffPlex" Version="3.1.2" />
1819
<PackageVersion Include="Verify.XunitV3" Version="31.9.4" />
1920
<PackageVersion Include="xunit.v3" Version="3.2.1" />
2021
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
2122
</ItemGroup>
22-
</Project>
23+
</Project>

src/MarkdownSnippets/ContentValidation.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
static class ContentValidation
22
{
3-
static Dictionary<string, string> phrases = new()
4-
{
5-
{"a majority of ", "most"},
6-
{"a number of", "some or many"},
7-
{"at an early date", "soon"},
8-
{"at the conclusion of", "after or following"},
9-
{"at the present time", "now"},
10-
{"at this point in time", "now"},
11-
{"based on the fact that", "because or since"},
12-
{"despite the fact that", "although"},
13-
{"due to the fact that", "because"},
14-
{"during the course of", "during"},
15-
{"during the time that", "during or while"},
16-
{"have the capability to", "can"},
17-
{"in connection with", "about"},
18-
{"in order to", "to"},
19-
{"in regard to ", "regarding or about"},
20-
{"in the event of", "if"},
21-
{"in view of the fact that", "because"},
22-
{"it is often the case that", "often"},
23-
{"make reference to ", "refer to"},
24-
{"of the opinion that", "think that "},
25-
{"on a daily basis", "daily"},
26-
{"on the grounds that", "because"},
27-
{"prior to", "before"},
28-
{"so as to", "to"},
29-
{"subsequent to", "after"},
30-
{"take into consideration", "consider"},
31-
{"until such time as", "until"},
32-
{"a lot", "many"},
33-
{"sort of", "similar or approximately"},
34-
{"kind of", "similar or approximately "}
35-
};
3+
static FrozenDictionary<string, string> phrases = FrozenDictionary.Create<string, string>([
4+
new("a majority of ", "most"),
5+
new("a number of", "some or many"),
6+
new("at an early date", "soon"),
7+
new("at the conclusion of", "after or following"),
8+
new("at the present time", "now"),
9+
new("at this point in time", "now"),
10+
new("based on the fact that", "because or since"),
11+
new("despite the fact that", "although"),
12+
new("due to the fact that", "because"),
13+
new("during the course of", "during"),
14+
new("during the time that", "during or while"),
15+
new("have the capability to", "can"),
16+
new("in connection with", "about"),
17+
new("in order to", "to"),
18+
new("in regard to ", "regarding or about"),
19+
new("in the event of", "if"),
20+
new("in view of the fact that", "because"),
21+
new("it is often the case that", "often"),
22+
new("make reference to ", "refer to"),
23+
new("of the opinion that", "think that "),
24+
new("on a daily basis", "daily"),
25+
new("on the grounds that", "because"),
26+
new("prior to", "before"),
27+
new("so as to", "to"),
28+
new("subsequent to", "after"),
29+
new("take into consideration", "consider"),
30+
new("until such time as", "until"),
31+
new("a lot", "many"),
32+
new("sort of", "similar or approximately"),
33+
new("kind of", "similar or approximately ")
34+
]);
3635

3736
static List<string> invalidStrings;
3837

@@ -135,4 +134,4 @@ static string Clean(string input)
135134
span[index] = ' ';
136135
});
137136
}
138-
}
137+
}

src/MarkdownSnippets/Downloader/FileNameFromUrl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
static class FileNameFromUrl
22
{
3-
static HashSet<char> invalid = [..Path.GetInvalidFileNameChars().Concat(Path.GetInvalidPathChars())];
3+
static FrozenSet<char> invalid = Path.GetInvalidFileNameChars().Concat(Path.GetInvalidPathChars()).ToFrozenSet();
44

55
public static string ConvertToFileName(string url)
66
{

src/MarkdownSnippets/GlobalUsings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
global using System.Diagnostics.CodeAnalysis;
1+
global using System.Collections.Frozen;
2+
global using System.Diagnostics.CodeAnalysis;
23
global using System.Net;
34
global using System.Net.Http;
45
global using System.Text.RegularExpressions;

src/MarkdownSnippets/MarkdownSnippets.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<PackageReference Include="Microsoft.Sbom.Targets" PrivateAssets="all" />
88
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
99
<PackageReference Include="Polyfill" PrivateAssets="all" />
10+
<PackageReference Include="System.Collections.Immutable" Condition="'$(TargetFramework)' != 'net10.0'"/>
1011
<PackageReference Include="System.Memory" Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard' OR '$(TargetFrameworkIdentifier)' == '.NETFramework' OR '$(TargetFrameworkIdentifier)' == '.NETCOREAPP'" />
1112
<PackageReference Include="System.Net.Http" Condition="'$(TargetFramework)' == 'net48'" />
1213
</ItemGroup>
13-
</Project>
14+
</Project>

src/MarkdownSnippets/Reading/Exclusions/SnippetFileExclusions.cs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
public static class SnippetFileExclusions
44
{
55
public static bool IsBinary(string extension) =>
6-
BinaryFileExtensions.Contains(extension);
6+
binaryFileExtensionsFrozen.Contains(extension);
77

88
public static bool CanContainCommentsExtension(string extension) =>
9-
!NoAcceptCommentsExtensions.Contains(extension);
9+
!noAcceptCommentsExtensionsFrozen.Contains(extension);
1010

11-
public static HashSet<string> NoAcceptCommentsExtensions { get; set; } =
12-
new(StringComparer.OrdinalIgnoreCase)
13-
{
11+
static FrozenSet<string> noAcceptCommentsExtensionsFrozen = FrozenSet.Create(
12+
StringComparer.OrdinalIgnoreCase,
13+
source:
14+
[
1415
//files that dont accept comments hence cant contain snippets
1516

1617
#region NoAcceptCommentsExtensions
@@ -22,11 +23,34 @@ public static bool CanContainCommentsExtension(string extension) =>
2223
"sln"
2324

2425
#endregion
25-
};
26+
]);
27+
28+
public static void AddNoAcceptCommentsExtensions(params string[] extensions)
29+
{
30+
var set = new HashSet<string>(noAcceptCommentsExtensionsFrozen, StringComparer.OrdinalIgnoreCase);
31+
foreach (var extension in extensions)
32+
{
33+
set.Add(extension);
34+
}
2635

27-
public static HashSet<string> BinaryFileExtensions { get; set; } =
28-
new(StringComparer.OrdinalIgnoreCase)
36+
noAcceptCommentsExtensionsFrozen = set.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
37+
}
38+
39+
public static void RemoveNoAcceptCommentsExtensions(params string[] extensions)
40+
{
41+
var set = new HashSet<string>(noAcceptCommentsExtensionsFrozen, StringComparer.OrdinalIgnoreCase);
42+
foreach (var extension in extensions)
2943
{
44+
set.Remove(extension);
45+
}
46+
47+
noAcceptCommentsExtensionsFrozen = set.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
48+
}
49+
50+
static FrozenSet<string> binaryFileExtensionsFrozen = FrozenSet.Create(
51+
StringComparer.OrdinalIgnoreCase,
52+
source:
53+
[
3054
#region BinaryFileExtensions
3155

3256
"user",
@@ -297,5 +321,27 @@ public static bool CanContainCommentsExtension(string extension) =>
297321
"zipx"
298322

299323
#endregion
300-
};
301-
}
324+
]);
325+
326+
public static void AddBinaryFileExtensions(params string[] extensions)
327+
{
328+
var set = new HashSet<string>(binaryFileExtensionsFrozen, StringComparer.OrdinalIgnoreCase);
329+
foreach (var extension in extensions)
330+
{
331+
set.Add(extension);
332+
}
333+
334+
binaryFileExtensionsFrozen = set.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
335+
}
336+
337+
public static void RemoveBinaryFileExtensions(params string[] extensions)
338+
{
339+
var set = new HashSet<string>(binaryFileExtensionsFrozen, StringComparer.OrdinalIgnoreCase);
340+
foreach (var extension in extensions)
341+
{
342+
set.Remove(extension);
343+
}
344+
345+
binaryFileExtensionsFrozen = set.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
346+
}
347+
}

src/MarkdownSnippets/Reading/FileSnippetExtractor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Task AppendUrlAsSnippet(this ICollection<Snippet> snippets, string
1818
/// Each url will be accessible using the file name as a key. Any snippets within the files will be extracted and accessible as individual keyed snippets.
1919
/// </summary>
2020
public static Task AppendUrlsAsSnippets(this ICollection<Snippet> snippets, params string[] urls) =>
21-
AppendUrlsAsSnippets(snippets, (IEnumerable<string>) urls);
21+
snippets.AppendUrlsAsSnippets((IEnumerable<string>) urls);
2222

2323
/// <summary>
2424
/// Each url will be accessible using the file name as a key. Any snippets within the files will be extracted and accessible as individual keyed snippets.
@@ -27,7 +27,7 @@ public static async Task AppendUrlsAsSnippets(this ICollection<Snippet> snippets
2727
{
2828
foreach (var url in urls)
2929
{
30-
await AppendUrlAsSnippet(snippets, url);
30+
await snippets.AppendUrlAsSnippet(url);
3131
}
3232
}
3333

@@ -63,7 +63,7 @@ public static void AppendFilesAsSnippets(this ICollection<Snippet> snippets, par
6363
{
6464
foreach (var filePath in filePaths)
6565
{
66-
AppendFileAsSnippet(snippets, filePath);
66+
snippets.AppendFileAsSnippet(filePath);
6767
}
6868
}
6969

@@ -211,4 +211,4 @@ static Snippet BuildSnippet(string path, LoopStack loopStack, string language, i
211211
expressiveCode: loopState.ExpressiveCode
212212
);
213213
}
214-
}
214+
}

0 commit comments

Comments
 (0)