Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce thread contention on ProjectSystemProject._gate #72424

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim.Framework;
Expand All @@ -14,8 +17,6 @@

namespace Roslyn.VisualStudio.CSharp.UnitTests.ProjectSystemShim.CPS
{
using System.Collections.Generic;
using System.Threading.Tasks;
using static CSharpHelpers;

[UseExportProvider]
Expand Down Expand Up @@ -127,21 +128,22 @@ public async Task ReorderSourceFilesBatch_CPS()
// Add a file outside the batch.
project.AddSourceFile(sourceFileFullPath2);

project.StartBatch();
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);
var disposableBatchScope = await project.CreateBatchScopeAsync(CancellationToken.None).ConfigureAwait(false);
await using (disposableBatchScope.ConfigureAwait(false))
{
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);

// Removing path2 to test removal of a file the actual internal project state has changed outside of the batch.
project.RemoveSourceFile(sourceFileFullPath2);
// Removing path2 to test removal of a file the actual internal project state has changed outside of the batch.
project.RemoveSourceFile(sourceFileFullPath2);

// Removing path4 to test remove of a file when it was also added in a batch.
project.RemoveSourceFile(sourceFileFullPath4);
// Removing path4 to test remove of a file when it was also added in a batch.
project.RemoveSourceFile(sourceFileFullPath4);

project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath3, sourceFileFullPath1 });

await project.EndBatchAsync();
project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath3, sourceFileFullPath1 });
}

var documents = GetCurrentDocuments().ToArray();

Expand All @@ -168,31 +170,32 @@ public async Task ReorderSourceFilesBatchWithReAdding_CPS()
// Add a file outside the batch.
project.AddSourceFile(sourceFileFullPath2);

project.StartBatch();
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);
var disposableBatchScope = await project.CreateBatchScopeAsync(CancellationToken.None).ConfigureAwait(false);
await using (disposableBatchScope.ConfigureAwait(false))
{
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);

// Removing path2 to test removal of a file the actual internal project state has changed outside of the batch.
project.RemoveSourceFile(sourceFileFullPath2);

// Removing path4 to test remove of a file when it was also added in a batch.
project.RemoveSourceFile(sourceFileFullPath4);
// Removing path2 to test removal of a file the actual internal project state has changed outside of the batch.
project.RemoveSourceFile(sourceFileFullPath2);

project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath3, sourceFileFullPath1 });
// Removing path4 to test remove of a file when it was also added in a batch.
project.RemoveSourceFile(sourceFileFullPath4);

// Re-adding / re-removing / re-adding again.
project.AddSourceFile(sourceFileFullPath2);
project.AddSourceFile(sourceFileFullPath4);
project.RemoveSourceFile(sourceFileFullPath2);
project.RemoveSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath2);
project.AddSourceFile(sourceFileFullPath4);
project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath3, sourceFileFullPath1 });

project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath4, sourceFileFullPath3, sourceFileFullPath2, sourceFileFullPath1 });
// Re-adding / re-removing / re-adding again.
project.AddSourceFile(sourceFileFullPath2);
project.AddSourceFile(sourceFileFullPath4);
project.RemoveSourceFile(sourceFileFullPath2);
project.RemoveSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath2);
project.AddSourceFile(sourceFileFullPath4);

await project.EndBatchAsync();
project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath4, sourceFileFullPath3, sourceFileFullPath2, sourceFileFullPath1 });
}

var documents = GetCurrentDocuments().ToArray();

Expand All @@ -218,18 +221,18 @@ public async Task ReorderSourceFilesBatchAddAfterReorder_CPS()
var sourceFileFullPath4 = @"c:\source4.cs";
var sourceFileFullPath5 = @"c:\source5.cs";

project.StartBatch();
var disposableBatchScope = await project.CreateBatchScopeAsync(CancellationToken.None).ConfigureAwait(false);
await using (disposableBatchScope.ConfigureAwait(false))
{
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath2);

project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath2);
project.ReorderSourceFiles(new[] { sourceFileFullPath2, sourceFileFullPath1 });

project.ReorderSourceFiles(new[] { sourceFileFullPath2, sourceFileFullPath1 });

project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);

await project.EndBatchAsync();
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);
}

project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath4, sourceFileFullPath3, sourceFileFullPath2, sourceFileFullPath1 });

Expand Down Expand Up @@ -257,21 +260,21 @@ public async Task ReorderSourceFilesBatchRemoveAfterReorder_CPS()
var sourceFileFullPath4 = @"c:\source4.cs";
var sourceFileFullPath5 = @"c:\source5.cs";

project.StartBatch();
var disposableBatchScope = await project.CreateBatchScopeAsync(CancellationToken.None).ConfigureAwait(false);
await using (disposableBatchScope.ConfigureAwait(false))
{
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath2);
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);

project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath2);
project.AddSourceFile(sourceFileFullPath3);
project.AddSourceFile(sourceFileFullPath4);
project.AddSourceFile(sourceFileFullPath5);

project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath4, sourceFileFullPath3, sourceFileFullPath2, sourceFileFullPath1 });

project.RemoveSourceFile(sourceFileFullPath3);
project.RemoveSourceFile(sourceFileFullPath4);
project.RemoveSourceFile(sourceFileFullPath5);
project.ReorderSourceFiles(new[] { sourceFileFullPath5, sourceFileFullPath4, sourceFileFullPath3, sourceFileFullPath2, sourceFileFullPath1 });

await project.EndBatchAsync();
project.RemoveSourceFile(sourceFileFullPath3);
project.RemoveSourceFile(sourceFileFullPath4);
project.RemoveSourceFile(sourceFileFullPath5);
}

project.ReorderSourceFiles(new[] { sourceFileFullPath2, sourceFileFullPath1 });

Expand Down Expand Up @@ -332,7 +335,8 @@ public async Task ReorderSourceFilesBatchExceptions_CPS()
var sourceFileFullPath4 = @"c:\source4.cs";
var sourceFileFullPath5 = @"c:\source5.cs";

project.StartBatch();
var disposableBatchScope = await project.CreateBatchScopeAsync(CancellationToken.None).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

Assert.Throws<ArgumentException>(() => project.ReorderSourceFiles(new[] { sourceFileFullPath4, sourceFileFullPath5 }));
Assert.Throws<ArgumentException>(() => project.ReorderSourceFiles(new[] { @"C:\invalid source file" })); // no files were added, therefore we should get an argument exception
Expand Down Expand Up @@ -362,8 +366,6 @@ public async Task ReorderSourceFilesBatchExceptions_CPS()
Assert.Throws<InvalidOperationException>(() => project.ReorderSourceFiles(new[] { @"C:\invalid source file", sourceFileFullPath2, sourceFileFullPath3, sourceFileFullPath4, sourceFileFullPath5 }));
Assert.Throws<ArgumentOutOfRangeException>(() => project.ReorderSourceFiles(new List<string>()));
Assert.Throws<ArgumentOutOfRangeException>(() => project.ReorderSourceFiles(null));

await project.EndBatchAsync();
}

[WpfFact]
Expand All @@ -381,12 +383,12 @@ public async Task ReorderSourceFilesBatchExceptionRemoveFile_CPS()
project.AddSourceFile(sourceFileFullPath1);
project.AddSourceFile(sourceFileFullPath2);

project.StartBatch();

project.RemoveSourceFile(sourceFileFullPath2);
Assert.Throws<InvalidOperationException>(() => project.ReorderSourceFiles(new[] { sourceFileFullPath2 }));

await project.EndBatchAsync();
var disposableBatchScope = await project.CreateBatchScopeAsync(CancellationToken.None).ConfigureAwait(false);
await using (disposableBatchScope.ConfigureAwait(false))
{
project.RemoveSourceFile(sourceFileFullPath2);
Assert.Throws<InvalidOperationException>(() => project.ReorderSourceFiles(new[] { sourceFileFullPath2 }));
}

var documents = GetCurrentDocuments().ToArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,70 @@ public void Dispose()
[Obsolete($"Call the {nameof(AddAdditionalFilesAsync)} overload that takes {nameof(SourceFileInfo)}.")]
public async Task AddAdditionalFilesAsync(IReadOnlyList<string> additionalFilePaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var additionalFilePath in additionalFilePaths)
_project.AddAdditionalFile(additionalFilePath);
}

public async Task AddAdditionalFilesAsync(IReadOnlyList<SourceFileInfo> additionalFiles, CancellationToken cancellationToken)
{
await using var batchScope = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var additionalFile in additionalFiles)
_project.AddAdditionalFile(additionalFile.FilePath, folderNames: additionalFile.FolderNames.ToImmutableArray());
}

public async Task RemoveAdditionalFilesAsync(IReadOnlyList<string> additionalFilePaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var additionalFilePath in additionalFilePaths)
_project.RemoveAdditionalFile(additionalFilePath);
}

public async Task AddAnalyzerConfigFilesAsync(IReadOnlyList<string> analyzerConfigPaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var analyzerConfigPath in analyzerConfigPaths)
_project.AddAnalyzerConfigFile(analyzerConfigPath);
}
public async Task RemoveAnalyzerConfigFilesAsync(IReadOnlyList<string> analyzerConfigPaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var analyzerConfigPath in analyzerConfigPaths)
_project.RemoveAnalyzerConfigFile(analyzerConfigPath);
}

public async Task AddAnalyzerReferencesAsync(IReadOnlyList<string> analyzerPaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var analyzerPath in analyzerPaths)
_project.AddAnalyzerReference(analyzerPath);
}

public async Task RemoveAnalyzerReferencesAsync(IReadOnlyList<string> analyzerPaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var analyzerPath in analyzerPaths)
_project.RemoveAnalyzerReference(analyzerPath);
}

public async Task AddMetadataReferencesAsync(IReadOnlyList<MetadataReferenceInfo> metadataReferences, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var metadataReference in metadataReferences)
{
Expand All @@ -99,7 +107,8 @@ public async Task AddMetadataReferencesAsync(IReadOnlyList<MetadataReferenceInfo

public async Task RemoveMetadataReferencesAsync(IReadOnlyList<MetadataReferenceInfo> metadataReferences, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

// The existing IWorkspaceProjectContext API here is a bit odd in that it only looks at the file path, and trusts that there aren't two
// references with the same path but different properties.
Expand All @@ -109,7 +118,8 @@ public async Task RemoveMetadataReferencesAsync(IReadOnlyList<MetadataReferenceI

public async Task AddSourceFilesAsync(IReadOnlyList<SourceFileInfo> sourceFiles, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var sourceFile in sourceFiles)
{
Expand All @@ -120,31 +130,35 @@ public async Task AddSourceFilesAsync(IReadOnlyList<SourceFileInfo> sourceFiles,
}
public async Task RemoveSourceFilesAsync(IReadOnlyList<string> sourceFiles, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var sourceFile in sourceFiles)
_project.RemoveSourceFile(sourceFile);
}

public async Task AddDynamicFilesAsync(IReadOnlyList<string> dynamicFilePaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var dynamicFilePath in dynamicFilePaths)
_project.AddDynamicFile(dynamicFilePath);
}

public async Task RemoveDynamicFilesAsync(IReadOnlyList<string> dynamicFilePaths, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var dynamicFilePath in dynamicFilePaths)
_project.RemoveDynamicFile(dynamicFilePath);
}

public async Task SetBuildSystemPropertiesAsync(IReadOnlyDictionary<string, string> properties, CancellationToken cancellationToken)
{
await using var batch = _project.CreateBatchScope().ConfigureAwait(false);
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);
await using var _ = disposableBatchScope.ConfigureAwait(false);

foreach (var property in properties)
_project.SetProperty(property.Key, property.Value);
Expand All @@ -168,9 +182,11 @@ public Task SetProjectHasAllInformationAsync(bool hasAllInformation, Cancellatio
return Task.CompletedTask;
}

public Task<IWorkspaceProjectBatch> StartBatchAsync(CancellationToken cancellationToken)
public async Task<IWorkspaceProjectBatch> StartBatchAsync(CancellationToken cancellationToken)
{
return Task.FromResult<IWorkspaceProjectBatch>(new WorkspaceProjectBatch(_project.CreateBatchScope()));
var disposableBatchScope = await _project.CreateBatchScopeAsync(cancellationToken).ConfigureAwait(false);

return new WorkspaceProjectBatch(disposableBatchScope);
}

private class WorkspaceProjectBatch : IWorkspaceProjectBatch
Expand Down
Loading
Loading