From e6cce92d05922d8cb9b640c1c4f95ba81c102ea0 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 17 Mar 2020 18:22:10 -0700 Subject: [PATCH] Use immutable arrays. --- .../VisualStudioDesignerAttributeService.cs | 2 +- .../VisualStudioTodoCommentsService.cs | 2 +- .../IDesignerAttributeListener.cs | 4 +- .../TodoComments/ITodoCommentsListener.cs | 4 +- .../Core/Utilities/AsyncBatchingWorkQueue.cs | 44 +++++++++++++------ 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/VisualStudio/Core/Def/Implementation/DesignerAttribute/VisualStudioDesignerAttributeService.cs b/src/VisualStudio/Core/Def/Implementation/DesignerAttribute/VisualStudioDesignerAttributeService.cs index e66f75ff927..5e6b798a796 100644 --- a/src/VisualStudio/Core/Def/Implementation/DesignerAttribute/VisualStudioDesignerAttributeService.cs +++ b/src/VisualStudio/Core/Def/Implementation/DesignerAttribute/VisualStudioDesignerAttributeService.cs @@ -313,7 +313,7 @@ private void AddFilteredInfos(ImmutableArray data, ArrayB /// /// Callback from the OOP service back into us. /// - public Task ReportDesignerAttributeDataAsync(IList data, CancellationToken cancellationToken) + public Task ReportDesignerAttributeDataAsync(ImmutableArray data, CancellationToken cancellationToken) { Contract.ThrowIfNull(_workQueue); _workQueue.AddWork(data); diff --git a/src/VisualStudio/Core/Def/Implementation/TodoComments/VisualStudioTodoCommentsService.cs b/src/VisualStudio/Core/Def/Implementation/TodoComments/VisualStudioTodoCommentsService.cs index 95ba833611a..15eb1b44a79 100644 --- a/src/VisualStudio/Core/Def/Implementation/TodoComments/VisualStudioTodoCommentsService.cs +++ b/src/VisualStudio/Core/Def/Implementation/TodoComments/VisualStudioTodoCommentsService.cs @@ -181,7 +181,7 @@ public ImmutableArray GetTodoItems(Workspace workspace, Documen /// /// Callback from the OOP service back into us. /// - public Task ReportTodoCommentDataAsync(DocumentId documentId, List infos, CancellationToken cancellationToken) + public Task ReportTodoCommentDataAsync(DocumentId documentId, ImmutableArray infos, CancellationToken cancellationToken) { Contract.ThrowIfNull(_workQueue); _workQueue.AddWork(new DocumentAndComments(documentId, infos.ToImmutableArray())); diff --git a/src/Workspaces/Core/Portable/DesignerAttribute/IDesignerAttributeListener.cs b/src/Workspaces/Core/Portable/DesignerAttribute/IDesignerAttributeListener.cs index 3b7bb52a8f8..17f9e31c9df 100644 --- a/src/Workspaces/Core/Portable/DesignerAttribute/IDesignerAttributeListener.cs +++ b/src/Workspaces/Core/Portable/DesignerAttribute/IDesignerAttributeListener.cs @@ -4,7 +4,7 @@ #nullable enable -using System.Collections.Generic; +using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -19,6 +19,6 @@ namespace Microsoft.CodeAnalysis.DesignerAttribute internal interface IDesignerAttributeListener { Task OnProjectRemovedAsync(ProjectId projectId, CancellationToken cancellationToken); - Task ReportDesignerAttributeDataAsync(IList data, CancellationToken cancellationToken); + Task ReportDesignerAttributeDataAsync(ImmutableArray data, CancellationToken cancellationToken); } } diff --git a/src/Workspaces/Core/Portable/TodoComments/ITodoCommentsListener.cs b/src/Workspaces/Core/Portable/TodoComments/ITodoCommentsListener.cs index e8ad58400e1..6069012a031 100644 --- a/src/Workspaces/Core/Portable/TodoComments/ITodoCommentsListener.cs +++ b/src/Workspaces/Core/Portable/TodoComments/ITodoCommentsListener.cs @@ -4,7 +4,7 @@ #nullable enable -using System.Collections.Generic; +using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -17,6 +17,6 @@ namespace Microsoft.CodeAnalysis.TodoComments internal interface ITodoCommentsListener { Task OnDocumentRemovedAsync(DocumentId documentId, CancellationToken cancellationToken); - Task ReportTodoCommentDataAsync(DocumentId documentId, List data, CancellationToken cancellationToken); + Task ReportTodoCommentDataAsync(DocumentId documentId, ImmutableArray data, CancellationToken cancellationToken); } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AsyncBatchingWorkQueue.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AsyncBatchingWorkQueue.cs index 243deadbc99..fb5e28133fa 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AsyncBatchingWorkQueue.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AsyncBatchingWorkQueue.cs @@ -90,20 +90,38 @@ public void AddWork(IEnumerable items) { // add our work to the set we'll process in the next batch. _nextBatch.AddRange(items); + TryKickOffNextBatchTask(); + } + } + + public void AddWork(ImmutableArray items) + { + // Don't do any more work if we've been asked to shutdown. + if (_cancellationToken.IsCancellationRequested) + return; + + lock (_gate) + { + // add our work to the set we'll process in the next batch. + _nextBatch.AddRange(items); + TryKickOffNextBatchTask(); + } + } - if (!_taskInFlight) - { - // No in-flight task. Kick one off to process these messages a second from now. - // We always attach the task to the previous one so that notifications to the ui - // follow the same order as the notification the OOP server sent to us. - _updateTask = _updateTask.ContinueWithAfterDelayFromAsync( - _ => ProcessNextBatchAsync(_cancellationToken), - _cancellationToken, - (int)_delay.TotalMilliseconds, - TaskContinuationOptions.RunContinuationsAsynchronously, - TaskScheduler.Default); - _taskInFlight = true; - } + private void TryKickOffNextBatchTask() + { + if (!_taskInFlight) + { + // No in-flight task. Kick one off to process these messages a second from now. + // We always attach the task to the previous one so that notifications to the ui + // follow the same order as the notification the OOP server sent to us. + _updateTask = _updateTask.ContinueWithAfterDelayFromAsync( + _ => ProcessNextBatchAsync(_cancellationToken), + _cancellationToken, + (int)_delay.TotalMilliseconds, + TaskContinuationOptions.RunContinuationsAsynchronously, + TaskScheduler.Default); + _taskInFlight = true; } } -- GitLab