Skip to content

Commit 4ba7b8f

Browse files
perf: cache IgnoreWithBasePath (#1758)
Repeatedly compiling regex for ignore files is a significant amount of runtime, caches the `IgnoreWithBasePath` to prevent this. See also #1776 ### Before | Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated | |---------- |---------:|---------:|---------:|----------:|----------:|----------:| | FormatCli | 955.0 ms | 17.65 ms | 29.98 ms | 9000.0000 | 1000.0000 | 81.5 MB | ### After | Method | Mean | Error | StdDev | Gen0 | Allocated | |---------- |---------:|---------:|---------:|----------:|----------:| | FormatCli | 683.5 ms | 13.61 ms | 30.17 ms | 7000.0000 | 69.33 MB | Co-authored-by: Bela VanderVoort <[email protected]>
1 parent 387e6ee commit 4ba7b8f

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Src/CSharpier.Cli/IgnoreFile.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Concurrent;
12
using System.IO.Abstractions;
23
using CSharpier.Cli.DotIgnore;
34
using CSharpier.Core;
@@ -35,17 +36,27 @@ public bool IsIgnored(string filePath)
3536
string baseDirectoryPath,
3637
IFileSystem fileSystem,
3738
string? ignorePath,
39+
ConcurrentDictionary<string, IgnoreList>? ignoreCache,
3840
CancellationToken cancellationToken
3941
)
4042
{
41-
Task<IgnoreList> CreateIgnore(string ignoreFilePath, string? overrideBasePath)
43+
async Task<IgnoreList> CreateIgnore(string ignoreFilePath, string? overrideBasePath)
4244
{
43-
return IgnoreList.CreateAsync(
45+
if (ignoreCache is not null && ignoreCache.TryGetValue(ignoreFilePath, out var ignore))
46+
{
47+
return ignore;
48+
}
49+
50+
ignore = await IgnoreList.CreateAsync(
4451
fileSystem,
4552
overrideBasePath ?? Path.GetDirectoryName(ignoreFilePath)!,
4653
ignoreFilePath,
4754
cancellationToken
4855
);
56+
57+
ignoreCache?[ignoreFilePath] = ignore;
58+
59+
return ignore;
4960
}
5061

5162
return await SharedFunc<IgnoreFile?>

Src/CSharpier.Cli/Options/OptionsProvider.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Concurrent;
22
using System.IO.Abstractions;
33
using System.Text.Json;
4+
using CSharpier.Cli.DotIgnore;
45
using CSharpier.Cli.EditorConfig;
56
using CSharpier.Core;
67
using Microsoft.Extensions.Logging;
@@ -15,6 +16,7 @@ private readonly ConcurrentDictionary<
1516
string,
1617
CSharpierConfigData?
1718
> csharpierConfigsByDirectory = new();
19+
private readonly ConcurrentDictionary<string, IgnoreList> ignoreWithPathCache = new();
1820
private readonly ConcurrentDictionary<string, IgnoreFile?> ignoreFilesByDirectory = new();
1921
private readonly ConfigurationFileOptions? specifiedConfigFile;
2022
private readonly EditorConfigSections? specifiedEditorConfig;
@@ -60,6 +62,7 @@ CancellationToken cancellationToken
6062
directoryName,
6163
fileSystem,
6264
ignorePath,
65+
null,
6366
cancellationToken
6467
);
6568

@@ -204,7 +207,13 @@ CancellationToken cancellationToken
204207
Path.Combine(searchingDirectory, ".csharpierignore")
205208
),
206209
(searchingDirectory) =>
207-
IgnoreFile.CreateAsync(searchingDirectory, this.fileSystem, null, cancellationToken)
210+
IgnoreFile.CreateAsync(
211+
searchingDirectory,
212+
this.fileSystem,
213+
null,
214+
ignoreWithPathCache,
215+
cancellationToken
216+
)
208217
);
209218

210219
#pragma warning disable IDE0270

Src/CSharpier.Tests/Cli/IgnoreFileTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,13 @@ private void GitBasedTest(string gitignore, string[] files)
621621
var gitIgnoredFiles = files.Where(o => !gitUntrackedFiles.Contains(o));
622622

623623
var ignoreFile = IgnoreFile
624-
.CreateAsync(this.gitRepository.RepoPath, this.fileSystem, null, CancellationToken.None)
624+
.CreateAsync(
625+
this.gitRepository.RepoPath,
626+
this.fileSystem,
627+
null,
628+
null,
629+
CancellationToken.None
630+
)
625631
.GetAwaiter()
626632
.GetResult();
627633

0 commit comments

Comments
 (0)