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 1 commit
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
@@ -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.Equal(", ", s),
DustinCampbell marked this conversation as resolved.
Show resolved Hide resolved
s => Assert.Equal("WoRlD", s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
[] => 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 items => BuildResult(items, selector)
};

Expand Down Expand Up @@ -121,6 +121,48 @@ 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 !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
Contributor

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.
DustinCampbell marked this conversation as resolved.
Show resolved Hide resolved
// We push them on a stack so that we can pop them in order later
for (var i = source.Length - 1; i >= 0; i--)
DustinCampbell marked this conversation as resolved.
Show resolved Hide resolved
{
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