Skip to content

Commit

Permalink
Use C# 10's enhanced struct initialization features (dotnet#63131)
Browse files Browse the repository at this point in the history
* Use C# 10's enhanced struct initialization features

* Addressed usages

* Remove #pragmas

* Fixed build

Cf. dotnet#63131 (comment)

* Update src/libraries/Common/src/System/Collections/Generic/EnumerableHelpers.Linq.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
gfoidl and stephentoub committed Mar 15, 2022
1 parent 7eaabea commit 466b0f6
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal static T[] ToArray<T>(IEnumerable<T> source)
return result;
}

var builder = new LargeArrayBuilder<T>(initialize: true);
LargeArrayBuilder<T> builder = new();
builder.AddRange(source);
return builder.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ internal struct LargeArrayBuilder<T>
{
private ArrayBuilder<T> _builder; // mutable struct; do not make this readonly

public LargeArrayBuilder(bool initialize) : this()
{
// This is a workaround for C# not having parameterless struct constructors yet.
// Once it gets them, replace this with a parameterless constructor.
Debug.Assert(initialize);
}
/// <summary>
/// Constructs a new builder.
/// </summary>
public LargeArrayBuilder() => this = default;

public int Count => _builder.Count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ internal struct LargeArrayBuilder<T>
/// <summary>
/// Constructs a new builder.
/// </summary>
/// <param name="initialize">Pass <c>true</c>.</param>
public LargeArrayBuilder(bool initialize)
public LargeArrayBuilder()
: this(maxCapacity: int.MaxValue)
{
// This is a workaround for C# not having parameterless struct constructors yet.
// Once it gets them, replace this with a parameterless constructor.
Debug.Assert(initialize);
}

/// <summary>
Expand All @@ -42,10 +38,10 @@ public LargeArrayBuilder(bool initialize)
/// Do not add more than <paramref name="maxCapacity"/> items to this builder.
/// </remarks>
public LargeArrayBuilder(int maxCapacity)
: this()
{
Debug.Assert(maxCapacity >= 0);

this = default;
_first = _current = Array.Empty<T>();
_maxCapacity = maxCapacity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,10 @@ internal struct SparseArrayBuilder<T>
/// <summary>
/// Constructs a new builder.
/// </summary>
/// <param name="initialize">Pass <c>true</c>.</param>
public SparseArrayBuilder(bool initialize)
: this()
public SparseArrayBuilder()
{
// Once C# gains parameterless struct constructors, please
// remove this workaround.
Debug.Assert(initialize);

_builder = new LargeArrayBuilder<T>(initialize: true);
this = default;
_builder = new LargeArrayBuilder<T>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace System.Collections.Generic.Tests
[Fact]
public void Constructor()
{
var builder = new LargeArrayBuilder<T>(initialize: true);
var builder = new LargeArrayBuilder<T>();

Assert.Equal(0, builder.Count);
Assert.Same(Array.Empty<T>(), builder.ToArray());
Expand All @@ -24,8 +24,8 @@ public void Constructor()
[MemberData(nameof(EnumerableData))]
public void AddCountAndToArray(IEnumerable<T> seed)
{
var builder1 = new LargeArrayBuilder<T>(initialize: true);
var builder2 = new LargeArrayBuilder<T>(initialize: true);
var builder1 = new LargeArrayBuilder<T>();
var builder2 = new LargeArrayBuilder<T>();

int count = 0;
foreach (T item in seed)
Expand Down Expand Up @@ -63,7 +63,7 @@ public void MaxCapacity(IEnumerable<T> seed, int maxCapacity)
[MemberData(nameof(EnumerableData))]
public void AddRange(IEnumerable<T> seed)
{
var builder = new LargeArrayBuilder<T>(initialize: true);
var builder = new LargeArrayBuilder<T>();

// Call AddRange multiple times and verify contents w/ each iteration.
for (int i = 1; i <= 10; i++)
Expand All @@ -81,7 +81,7 @@ public void CopyTo(IEnumerable<T> seed, int index, int count)
{
var array = new T[seed.Count()];

var builder = new LargeArrayBuilder<T>(initialize: true);
var builder = new LargeArrayBuilder<T>();
builder.AddRange(seed);
builder.CopyTo(array, index, count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private TSource[] LazyToArray()
{
Debug.Assert(GetCount(onlyIfCheap: true) == -1);

var builder = new LargeArrayBuilder<TSource>(initialize: true);
LargeArrayBuilder<TSource> builder = new();

if (!_appending)
{
Expand Down Expand Up @@ -106,7 +106,7 @@ private TSource[] LazyToArray()
{
Debug.Assert(GetCount(onlyIfCheap: true) == -1);

var builder = new SparseArrayBuilder<TSource>(initialize: true);
SparseArrayBuilder<TSource> builder = new();

if (_prepended != null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Concat.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override int GetCount(bool onlyIfCheap)

public override TSource[] ToArray()
{
var builder = new SparseArrayBuilder<TSource>(initialize: true);
SparseArrayBuilder<TSource> builder = new();

bool reservedFirst = builder.ReserveOrAdd(_first);
bool reservedSecond = builder.ReserveOrAdd(_second);
Expand Down Expand Up @@ -102,7 +102,7 @@ private TSource[] LazyToArray()
{
Debug.Assert(!_hasOnlyCollections);

var builder = new SparseArrayBuilder<TSource>(initialize: true);
SparseArrayBuilder<TSource> builder = new();
ArrayBuilder<int> deferredCopies = default;

for (int i = 0; ; i++)
Expand Down
5 changes: 3 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Select.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private sealed partial class SelectEnumerableIterator<TSource, TResult> : IIList
{
public TResult[] ToArray()
{
var builder = new LargeArrayBuilder<TResult>(initialize: true);
LargeArrayBuilder<TResult> builder = new();

foreach (TSource item in _source)
{
Expand Down Expand Up @@ -585,7 +585,8 @@ private TResult[] LazyToArray()
{
Debug.Assert(_source.GetCount(onlyIfCheap: true) == -1);

var builder = new LargeArrayBuilder<TResult>(initialize: true);
LargeArrayBuilder<TResult> builder = new();

foreach (TSource input in _source)
{
builder.Add(_selector(input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int GetCount(bool onlyIfCheap)

public TResult[] ToArray()
{
var builder = new SparseArrayBuilder<TResult>(initialize: true);
SparseArrayBuilder<TResult> builder = new();
ArrayBuilder<IEnumerable<TResult>> deferredCopies = default;

foreach (TSource element in _source)
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/src/System/Linq/Where.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public int GetCount(bool onlyIfCheap)

public TSource[] ToArray()
{
var builder = new LargeArrayBuilder<TSource>(initialize: true);
LargeArrayBuilder<TSource> builder = new();

foreach (TSource item in _source)
{
Expand Down Expand Up @@ -332,7 +332,7 @@ public int GetCount(bool onlyIfCheap)

public TResult[] ToArray()
{
var builder = new LargeArrayBuilder<TResult>(initialize: true);
LargeArrayBuilder<TResult> builder = new();

foreach (TSource item in _source)
{
Expand Down

0 comments on commit 466b0f6

Please sign in to comment.