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

Update the generic collection types to internally use GC.AllocateUninitializedArray<T> #47186

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte

if (count != 0)
{
T[] array = new T[count];
T[] array = GC.AllocateUninitializedArray<T>(count);
CopyTo(array, 0);
info.AddValue(ValuesName, array, typeof(T[]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Queue(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, SR.ArgumentOutOfRange_NeedNonNegNum);
_array = new T[capacity];
_array = GC.AllocateUninitializedArray<T>(capacity);
}

// Fills a Queue with the elements of an ICollection. Uses the enumerator
Expand Down Expand Up @@ -317,7 +317,7 @@ public T[] ToArray()
return Array.Empty<T>();
}

T[] arr = new T[_size];
T[] arr = GC.AllocateUninitializedArray<T>(_size);

if (_head < _tail)
{
Expand All @@ -336,7 +336,7 @@ public T[] ToArray()
// must be >= _size.
private void SetCapacity(int capacity)
{
T[] newarray = new T[capacity];
T[] newarray = GC.AllocateUninitializedArray<T>(capacity);
if (_size > 0)
{
if (_head < _tail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public SortedList(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, SR.ArgumentOutOfRange_NeedNonNegNum);
keys = new TKey[capacity];
values = new TValue[capacity];
keys = GC.AllocateUninitializedArray<TKey>(capacity);
values = GC.AllocateUninitializedArray<TValue>(capacity);
comparer = Comparer<TKey>.Default;
}

Expand Down Expand Up @@ -238,8 +238,8 @@ public int Capacity

if (value > 0)
{
TKey[] newKeys = new TKey[value];
TValue[] newValues = new TValue[value];
TKey[] newKeys = GC.AllocateUninitializedArray<TKey>(value);
TValue[] newValues = GC.AllocateUninitializedArray<TValue>(value);
if (_size > 0)
{
Array.Copy(keys, newKeys, _size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ public void UnionWith(IEnumerable<T> other)
if (asSorted != null && treeSubset == null && HasEqualComparer(asSorted) && (asSorted.Count > this.Count / 2))
{
// First do a merge sort to an array.
T[] merged = new T[asSorted.Count + this.Count];
T[] merged = GC.AllocateUninitializedArray<T>(asSorted.Count + this.Count);
int c = 0;
Enumerator mine = this.GetEnumerator();
Enumerator theirs = asSorted.GetEnumerator();
Expand Down Expand Up @@ -1026,7 +1026,7 @@ public virtual void IntersectWith(IEnumerable<T> other)
if (asSorted != null && treeSubset == null && HasEqualComparer(asSorted))
{
// First do a merge sort to an array.
T[] merged = new T[this.Count];
T[] merged = GC.AllocateUninitializedArray<T>(this.Count);
int c = 0;
Enumerator mine = this.GetEnumerator();
Enumerator theirs = asSorted.GetEnumerator();
Expand Down Expand Up @@ -1590,7 +1590,7 @@ protected virtual void GetObjectData(SerializationInfo info, StreamingContext co

if (root != null)
{
T[] items = new T[Count];
T[] items = GC.AllocateUninitializedArray<T>(Count);
CopyTo(items, 0);
info.AddValue(ItemsName, items, typeof(T[]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Stack(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, SR.ArgumentOutOfRange_NeedNonNegNum);
_array = new T[capacity];
_array = GC.AllocateUninitializedArray<T>(capacity);
}

// Fills a Stack with the contents of a particular collection. The items are
Expand Down Expand Up @@ -294,7 +294,7 @@ public T[] ToArray()
if (_size == 0)
return Array.Empty<T>();

T[] objArray = new T[_size];
T[] objArray = GC.AllocateUninitializedArray<T>(_size);
int i = 0;
while (i < _size)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte

if (_buckets != null)
{
var array = new KeyValuePair<TKey, TValue>[Count];
var array = GC.AllocateUninitializedArray<KeyValuePair<TKey, TValue>>(Count);
CopyTo(array, 0);
info.AddValue(KeyValuePairsName, array, typeof(KeyValuePair<TKey, TValue>[]));
}
Expand Down Expand Up @@ -489,8 +489,8 @@ private ref TValue FindValue(TKey key)
private int Initialize(int capacity)
{
int size = HashHelpers.GetPrime(capacity);
int[] buckets = new int[size];
Entry[] entries = new Entry[size];
int[] buckets = GC.AllocateUninitializedArray<int>(size);
Entry[] entries = GC.AllocateUninitializedArray<Entry>(size);

// Assign member variables after both arrays allocated to guard against corruption from OOM if second fails
_freeList = -1;
Expand Down Expand Up @@ -744,7 +744,7 @@ private void Resize(int newSize, bool forceNewHashCodes)
Debug.Assert(_entries != null, "_entries should be non-null");
Debug.Assert(newSize >= _entries.Length);

Entry[] entries = new Entry[newSize];
Entry[] entries = GC.AllocateUninitializedArray<Entry>(newSize);

int count = _count;
Array.Copy(_entries, entries, count);
Expand All @@ -769,7 +769,7 @@ private void Resize(int newSize, bool forceNewHashCodes)
}

// Assign member variables after both arrays allocated to guard against corruption from OOM if second fails
_buckets = new int[newSize];
_buckets = GC.AllocateUninitializedArray<int>(newSize);
#if TARGET_64BIT
_fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)newSize);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte

if (_buckets != null)
{
var array = new T[Count];
var array = GC.AllocateUninitializedArray<T>(Count);
CopyTo(array);
info.AddValue(ElementsName, array, typeof(T[]));
}
Expand All @@ -426,8 +426,8 @@ public virtual void OnDeserialization(object? sender)

if (capacity != 0)
{
_buckets = new int[capacity];
_entries = new Entry[capacity];
_buckets = GC.AllocateUninitializedArray<int>(capacity);
_entries = GC.AllocateUninitializedArray<Entry>(capacity);
#if TARGET_64BIT
_fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)capacity);
#endif
Expand Down Expand Up @@ -974,7 +974,7 @@ private void Resize(int newSize, bool forceNewHashCodes)
Debug.Assert(_entries != null, "_entries should be non-null");
Debug.Assert(newSize >= _entries.Length);

var entries = new Entry[newSize];
var entries = GC.AllocateUninitializedArray<Entry>(newSize);

int count = _count;
Array.Copy(_entries, entries, count);
Expand All @@ -1000,7 +1000,7 @@ private void Resize(int newSize, bool forceNewHashCodes)
}

// Assign member variables after both arrays allocated to guard against corruption from OOM if second fails
_buckets = new int[newSize];
_buckets = GC.AllocateUninitializedArray<int>(newSize);
#if TARGET_64BIT
_fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)newSize);
#endif
Expand Down Expand Up @@ -1071,8 +1071,8 @@ public void TrimExcess()
private int Initialize(int capacity)
{
int size = HashHelpers.GetPrime(capacity);
var buckets = new int[size];
var entries = new Entry[size];
var buckets = GC.AllocateUninitializedArray<int>(size);
var entries = GC.AllocateUninitializedArray<Entry>(size);

// Assign member variables after both arrays are allocated to guard against corruption from OOM if second fails.
_freeList = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List(int capacity)
if (capacity == 0)
_items = s_emptyArray;
else
_items = new T[capacity];
_items = GC.AllocateUninitializedArray<T>(capacity);
}

// Constructs a List, copying the contents of the given collection. The
Expand All @@ -72,7 +72,7 @@ public List(IEnumerable<T> collection)
}
else
{
_items = new T[count];
_items = GC.AllocateUninitializedArray<T>(count);
c.CopyTo(_items, 0);
_size = count;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public int Capacity
{
if (value > 0)
{
T[] newItems = new T[value];
T[] newItems = GC.AllocateUninitializedArray<T>(value);
if (_size > 0)
{
Array.Copy(_items, newItems, _size);
Expand Down Expand Up @@ -1030,7 +1030,7 @@ public T[] ToArray()
return s_emptyArray;
}

T[] array = new T[_size];
T[] array = GC.AllocateUninitializedArray<T>(_size);
Array.Copy(_items, array, _size);
return array;
}
Expand Down