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

Replace BatchingWorkQueue with AsyncBatchingWorkQueue from Roslyn #10140

Merged
merged 17 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -50,8 +50,6 @@ public OpenDocumentGenerator(
_workQueue = new AsyncBatchingWorkQueue<IDocumentSnapshot>(
s_delay,
ProcessBatchAsync,
Comparer.Instance,
preferMostRecentItems: true,
_disposeTokenSource.Token);

_projectManager.Changed += ProjectManager_Changed;
Expand All @@ -70,7 +68,7 @@ public void Dispose()

private async ValueTask ProcessBatchAsync(ImmutableArray<IDocumentSnapshot> items, CancellationToken token)
{
foreach (var document in items)
foreach (var document in items.GetMostRecentUniqueItems(Comparer.Instance))
{
if (token.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.CodeAnalysis.Razor.Utilities;
internal class AsyncBatchingWorkQueue(
TimeSpan delay,
Func<CancellationToken, ValueTask> processBatchAsync,
CancellationToken cancellationToken) : AsyncBatchingWorkQueue<VoidResult>(delay, Convert(processBatchAsync), EqualityComparer<VoidResult>.Default, false, cancellationToken)
CancellationToken cancellationToken) : AsyncBatchingWorkQueue<VoidResult>(delay, Convert(processBatchAsync), EqualityComparer<VoidResult>.Default, cancellationToken)
{
private static Func<ImmutableArray<VoidResult>, CancellationToken, ValueTask> Convert(Func<CancellationToken, ValueTask> processBatchAsync)
=> (items, ct) => processBatchAsync(ct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ internal class AsyncBatchingWorkQueue<TItem>(
TimeSpan delay,
Func<ImmutableArray<TItem>, CancellationToken, ValueTask> processBatchAsync,
IEqualityComparer<TItem>? equalityComparer,
bool preferMostRecentItems,
CancellationToken cancellationToken) : AsyncBatchingWorkQueue<TItem, VoidResult>(delay, Convert(processBatchAsync), equalityComparer, preferMostRecentItems, cancellationToken)
CancellationToken cancellationToken) : AsyncBatchingWorkQueue<TItem, VoidResult>(delay, Convert(processBatchAsync), equalityComparer, cancellationToken)
{
public AsyncBatchingWorkQueue(
TimeSpan delay,
Expand All @@ -28,7 +27,6 @@ public AsyncBatchingWorkQueue(
: this(delay,
processBatchAsync,
equalityComparer: null,
preferMostRecentItems: false,
cancellationToken)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
Expand Down Expand Up @@ -43,11 +42,6 @@ internal class AsyncBatchingWorkQueue<TItem, TResult>
/// </summary>
private readonly IEqualityComparer<TItem>? _equalityComparer;

/// <summary>
/// Determines whether the most recent items are preferred when deduping.
/// </summary>
private readonly bool _preferMostRecentItems;

/// <summary>
/// Callback to actually perform the processing of the next batch of work.
/// </summary>
Expand Down Expand Up @@ -113,13 +107,11 @@ public AsyncBatchingWorkQueue(
TimeSpan delay,
Func<ImmutableArray<TItem>, CancellationToken, ValueTask<TResult>> processBatchAsync,
IEqualityComparer<TItem>? equalityComparer,
bool preferMostRecentItems,
CancellationToken cancellationToken)
{
_delay = delay;
_processBatchAsync = processBatchAsync;
_equalityComparer = equalityComparer;
_preferMostRecentItems = preferMostRecentItems;
_entireQueueCancellationToken = cancellationToken;

_uniqueItems = new HashSet<TItem>(equalityComparer);
Expand Down Expand Up @@ -194,14 +186,6 @@ void AddItemsToBatch(IEnumerable<TItem> items)
return;
}

// In the case that we want to dedupe and prefer the most recent
// items, we add everything to the batch now and will remove them later.
if (_preferMostRecentItems)
{
_nextBatch.AddRange(items);
return;
}

// Otherwise, we dedupe and prefer the first item that we see.
foreach (var item in items)
{
Expand Down Expand Up @@ -297,38 +281,6 @@ void AddItemsToBatch(IEnumerable<TItem> items)
{
lock (_gate)
{
// If we're deduping and preferring the most recent items, we need
// to compare items and adjust the next batch here.
if (_nextBatch.Count > 0 && _equalityComparer is not null && _preferMostRecentItems)
{
Debug.Assert(_uniqueItems.Count == 0, "Deduping should not have already occurred.");

using var stack = new PooledArrayBuilder<TItem>(capacity: _nextBatch.Count);

// Walk the next batch in reverse and to identify unique items.
// We push them on a stack so that we can pop them in order later
for (var i = _nextBatch.Count - 1; i >= 0; i--)
{
var item = _nextBatch[i];

if (_uniqueItems.Add(item))
{
stack.Push(item);
}
}

// Did we actually dedupe anything? If so, adjust the next batch.
if (stack.Count < _nextBatch.Count)
{
_nextBatch.Clear();

while (stack.Count > 0)
{
_nextBatch.Add(stack.Pop());
}
}
}

var nextBatch = _nextBatch.ToImmutable();

// mark there being no existing update task so that the next OOP notification will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public WorkspaceProjectStateChangeDetector(
_workQueue = new AsyncBatchingWorkQueue<(Project?, IProjectSnapshot)>(
s_delay,
ProcessBatchAsync,
Comparer.Instance,
preferMostRecentItems: true,
_disposeTokenSource.Token);

_projectManager.Changed += ProjectManager_Changed;
Expand All @@ -82,7 +80,7 @@ public void Dispose()

private async ValueTask ProcessBatchAsync(ImmutableArray<(Project? Project, IProjectSnapshot ProjectSnapshot)> items, CancellationToken token)
{
foreach (var (project, projectSnapshot) in items)
foreach (var (project, projectSnapshot) in items.GetMostRecentUniqueItems(Comparer.Instance))
{
if (token.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public async Task DedupesItems()
return default;
},
equalityComparer: EqualityComparer<int>.Default,
preferMostRecentItems: false,
DisposalToken);

for (var i = 0; i < 1000; i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Utilities.Shared.Test;

public class ImmutableArrayExtensionsTests
{
[Fact]
public void GetMostRecentUniqueItems()
{
ImmutableArray<string> items =
[
"Hello",
"HELLO",
"HeLlO",
", ",
", ",
"World",
"WORLD",
"WoRlD"
];

var mostRecent = items.GetMostRecentUniqueItems(StringComparer.OrdinalIgnoreCase);

Assert.Collection(mostRecent,
s => Assert.Equal("HeLlO", s),
s => Assert.Same(items[4], s), // make sure it's the most recent ", "
s => Assert.Equal("WoRlD", s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class ImmutableArrayExtensions
/// </summary>
public static ImmutableArray<T> NullToEmpty<T>(this ImmutableArray<T> array)
{
return array.IsDefault ? ImmutableArray<T>.Empty : array;
return array.IsDefault ? [] : array;
}

public static void SetCapacityIfLarger<T>(this ImmutableArray<T>.Builder builder, int newCapacity)
Expand Down Expand Up @@ -46,7 +46,7 @@ public static ImmutableArray<T> DrainToImmutable<T>(this ImmutableArray<T>.Build
#else
if (builder.Count == 0)
{
return ImmutableArray<T>.Empty;
return [];
}

if (builder.Count == builder.Capacity)
Expand All @@ -64,11 +64,11 @@ public static ImmutableArray<TResult> SelectAsArray<T, TResult>(this ImmutableAr
{
return source switch
{
[] => ImmutableArray<TResult>.Empty,
[var item] => ImmutableArray.Create(selector(item)),
[var item1, var item2] => ImmutableArray.Create(selector(item1), selector(item2)),
[var item1, var item2, var item3] => ImmutableArray.Create(selector(item1), selector(item2), selector(item3)),
[var item1, var item2, var item3, var item4] => ImmutableArray.Create(selector(item1), selector(item2), selector(item3), selector(item4)),
[] => [],
[var item] => [selector(item)],
[var item1, var item2] => [selector(item1), selector(item2)],
[var item1, var item2, var item3] => [selector(item1), selector(item2), selector(item3)],
[var item1, var item2, var item3, var item4] => [selector(item1), selector(item2), selector(item3), selector(item4)],
var items => BuildResult(items, selector)
};

Expand All @@ -89,7 +89,7 @@ public static ImmutableArray<TResult> SelectManyAsArray<TSource, TResult>(this I
{
if (source is null || source.Count == 0)
{
return ImmutableArray<TResult>.Empty;
return [];
}

using var builder = new PooledArrayBuilder<TResult>(capacity: source.Count);
Expand All @@ -105,7 +105,7 @@ public static ImmutableArray<T> WhereAsArray<T>(this ImmutableArray<T> source, F
{
if (source is [])
{
return ImmutableArray<T>.Empty;
return [];
}

using var builder = new PooledArrayBuilder<T>();
Expand All @@ -121,6 +121,53 @@ public static ImmutableArray<T> WhereAsArray<T>(this ImmutableArray<T> source, F
return builder.DrainToImmutable();
}

/// <summary>
/// Returns an <see cref="ImmutableArray{T}"/> that contains no duplicates from the <paramref name="source"/> array
/// and returns the most recent copy of each item.
/// </summary>
public static ImmutableArray<T> GetMostRecentUniqueItems<T>(this ImmutableArray<T> source, IEqualityComparer<T> comparer)
{
if (source.IsEmpty)
{
return [];
}

#if !NETSTANDARD2_0
var uniqueItems = new HashSet<T>(capacity: source.Length, comparer);
#else
var uniqueItems = new HashSet<T>(comparer);
#endif
Comment on lines +135 to +139
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/Should this be pooled?

Copy link
Member Author

@DustinCampbell DustinCampbell Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, no. It can't because of the comparer. Like Dictionary<TKey, TValue>, there's no way to set a HashSet<T> comparer other than in the constructor.


using var stack = new PooledArrayBuilder<T>(capacity: source.Length);

// Walk the next batch in reverse and to identify unique items.
// We push them on a stack so that we can pop them in order later
for (var i = source.Length - 1; i >= 0; i--)
{
var item = source[i];

if (uniqueItems.Add(item))
{
stack.Push(item);
}
}

// Did we actually dedupe anything? If not, just return the original.
if (stack.Count == source.Length)
{
return source;
}

using var result = new PooledArrayBuilder<T>(capacity: stack.Count);

while (stack.Count > 0)
{
result.Add(stack.Pop());
}

return result.DrainToImmutable();
}

/// <summary>
/// Executes a binary search over an array, but allows the caller to decide what constitutes a match
/// </summary>
Expand Down