Skip to content

Commit 1232ee6

Browse files
perf: move async to separate methods
1 parent 3e66e31 commit 1232ee6

File tree

1 file changed

+73
-20
lines changed

1 file changed

+73
-20
lines changed

Src/CSharpier.Cli/Options/OptionsProvider.cs

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ CancellationToken cancellationToken
172172
);
173173
}
174174

175-
private async ValueTask<EditorConfigSections?> FindEditorConfigAsync(
175+
private ValueTask<EditorConfigSections?> FindEditorConfigAsync(
176176
string directoryName,
177177
CancellationToken cancellationToken
178178
)
179179
{
180-
return await this.FindFileAsync(
180+
return this.FindFileAsync(
181181
directoryName,
182182
this.editorConfigByDirectory,
183183
(searchingDirectory, cancellationToken) =>
@@ -193,15 +193,15 @@ await this.FindIgnoreFileAsync(searchingDirectory, cancellationToken),
193193
);
194194
}
195195

196-
private async ValueTask<IgnoreFile> FindIgnoreFileAsync(
196+
private ValueTask<IgnoreFile> FindIgnoreFileAsync(
197197
string directoryName,
198198
CancellationToken cancellationToken
199199
)
200200
{
201-
var ignoreFile = await this.FindFileAsync(
201+
var ignoreFileTask = this.FindFileAsync(
202202
directoryName,
203203
this.ignoreFilesByDirectory,
204-
(searchingDirectory, cancellationToken) =>
204+
(searchingDirectory, _) =>
205205
this.fileSystem.File.Exists(Path.Combine(searchingDirectory, ".gitignore"))
206206
|| this.fileSystem.File.Exists(
207207
Path.Combine(searchingDirectory, ".csharpierignore")
@@ -216,23 +216,45 @@ CancellationToken cancellationToken
216216
cancellationToken
217217
);
218218

219-
#pragma warning disable IDE0270
220-
if (ignoreFile is null)
219+
if (ignoreFileTask.IsCompletedSuccessfully)
221220
{
222-
// should never happen
223-
throw new Exception("Unable to locate an IgnoreFile for " + directoryName);
224-
}
221+
#pragma warning disable IDE0270
222+
if (ignoreFileTask.Result is null)
223+
{
224+
// should never happen
225+
throw new Exception("Unable to locate an IgnoreFile for " + directoryName);
226+
}
225227
#pragma warning restore IDE0270
226228

227-
return ignoreFile;
229+
return ignoreFileTask!;
230+
}
231+
232+
return new ValueTask<IgnoreFile>(FindIgnoreFileAsyncInner(ignoreFileTask, directoryName));
233+
234+
static async Task<IgnoreFile> FindIgnoreFileAsyncInner(
235+
ValueTask<IgnoreFile?> ignoreFileTask,
236+
string directoryName
237+
)
238+
{
239+
var ignoreFile = await ignoreFileTask;
240+
241+
#pragma warning disable IDE0270
242+
if (ignoreFile is null)
243+
{
244+
// should never happen
245+
throw new Exception("Unable to locate an IgnoreFile for " + directoryName);
246+
}
247+
#pragma warning restore IDE0270
248+
return ignoreFile;
249+
}
228250
}
229251

230252
/// <summary>
231253
/// this is a type of lazy lookup. We preload file type for the initial directory of the format command
232254
/// When trying to format a file in a given subdirectory if we've already found the appropriate file type then return it
233255
/// otherwise track it down (parsing if we need to) and set the references for any parent directories
234256
/// </summary>
235-
private async ValueTask<T?> FindFileAsync<T>(
257+
private ValueTask<T?> FindFileAsync<T>(
236258
string directoryName,
237259
ConcurrentDictionary<string, T?> dictionary,
238260
Func<string, CancellationToken, bool> shouldConsiderDirectory,
@@ -242,9 +264,27 @@ CancellationToken cancellationToken
242264
{
243265
if (dictionary.TryGetValue(directoryName, out var result))
244266
{
245-
return result;
267+
return new ValueTask<T?>(result);
246268
}
247269

270+
return FindFileAsyncInner(
271+
directoryName,
272+
dictionary,
273+
shouldConsiderDirectory,
274+
createFileAsync,
275+
cancellationToken
276+
);
277+
}
278+
279+
private async ValueTask<T?> FindFileAsyncInner<T>(
280+
string directoryName,
281+
ConcurrentDictionary<string, T?> dictionary,
282+
Func<string, CancellationToken, bool> shouldConsiderDirectory,
283+
Func<string, CancellationToken, Task<T?>> createFileAsync,
284+
CancellationToken cancellationToken
285+
)
286+
{
287+
T? result = default;
248288
var directoriesToSet = new List<string>();
249289
var searchingDirectory = this.fileSystem.DirectoryInfo.New(directoryName);
250290
while (
@@ -273,17 +313,30 @@ searchingDirectory is not null
273313
return result;
274314
}
275315

276-
public async ValueTask<bool> IsIgnoredAsync(
316+
public ValueTask<bool> IsIgnoredAsync(
277317
string actualFilePath,
278318
CancellationToken cancellationToken
279319
)
280320
{
281-
return (
282-
await this.FindIgnoreFileAsync(
283-
Path.GetDirectoryName(actualFilePath)!,
284-
cancellationToken
285-
)
286-
).IsIgnored(actualFilePath);
321+
var ignoredTask = this.FindIgnoreFileAsync(
322+
Path.GetDirectoryName(actualFilePath)!,
323+
cancellationToken
324+
);
325+
326+
if (ignoredTask.IsCompletedSuccessfully)
327+
{
328+
return new ValueTask<bool>(ignoredTask.Result.IsIgnored(actualFilePath));
329+
}
330+
331+
return new ValueTask<bool>(IsIgnoredAsyncInner(ignoredTask, actualFilePath));
332+
333+
static async Task<bool> IsIgnoredAsyncInner(
334+
ValueTask<IgnoreFile> task,
335+
string actualFilePath
336+
)
337+
{
338+
return (await task).IsIgnored(actualFilePath);
339+
}
287340
}
288341

289342
public string Serialize()

0 commit comments

Comments
 (0)