From 28637d2c616205c7d57a5b02b512117d1f6d387d Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 12 Oct 2026 20:37:37 +1100 Subject: [PATCH] Grow geometrically in CollectionsMarshal.SetCount --- src/Polyfill/CollectionsMarshal.cs | 23 +++- src/Polyfill/CollectionsMarshalSetCount.cs | 23 +++- src/Split/net461/CollectionsMarshal.cs | 18 ++- src/Split/net462/CollectionsMarshal.cs | 18 ++- src/Split/net47/CollectionsMarshal.cs | 18 ++- src/Split/net471/CollectionsMarshal.cs | 18 ++- src/Split/net472/CollectionsMarshal.cs | 18 ++- src/Split/net48/CollectionsMarshal.cs | 18 ++- src/Split/net481/CollectionsMarshal.cs | 18 ++- .../net5.0/CollectionsMarshalSetCount.cs | 18 ++- .../net6.0/CollectionsMarshalSetCount.cs | 18 ++- .../net7.0/CollectionsMarshalSetCount.cs | 18 ++- src/Split/netcoreapp2.0/CollectionsMarshal.cs | 18 ++- src/Split/netcoreapp2.1/CollectionsMarshal.cs | 18 ++- src/Split/netcoreapp2.2/CollectionsMarshal.cs | 18 ++- src/Split/netcoreapp3.0/CollectionsMarshal.cs | 18 ++- src/Split/netcoreapp3.1/CollectionsMarshal.cs | 18 ++- .../netstandard2.0/CollectionsMarshal.cs | 18 ++- .../netstandard2.1/CollectionsMarshal.cs | 18 ++- src/Split/uap10.0/CollectionsMarshal.cs | 18 ++- src/Tests/PolyfillTests_CollectionsMarshal.cs | 123 ++++++++++++++++++ 21 files changed, 453 insertions(+), 40 deletions(-) diff --git a/src/Polyfill/CollectionsMarshal.cs b/src/Polyfill/CollectionsMarshal.cs index f61ea3c6..bd71e47e 100644 --- a/src/Polyfill/CollectionsMarshal.cs +++ b/src/Polyfill/CollectionsMarshal.cs @@ -73,9 +73,28 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + // Grow geometrically, as List does. Setting the capacity to exactly count would reallocate + // on every call when the count is raised a little at a time. + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + + if (grown < count) + { + grown = count; + } + + list.Capacity = (int) grown; } for (var index = current; index < count; index++) diff --git a/src/Polyfill/CollectionsMarshalSetCount.cs b/src/Polyfill/CollectionsMarshalSetCount.cs index 15048601..deaf8ace 100644 --- a/src/Polyfill/CollectionsMarshalSetCount.cs +++ b/src/Polyfill/CollectionsMarshalSetCount.cs @@ -40,9 +40,28 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + // Grow geometrically, as List does. Setting the capacity to exactly count would reallocate + // on every call when the count is raised a little at a time. + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + + if (grown < count) + { + grown = count; + } + + list.Capacity = (int) grown; } for (var index = current; index < count; index++) diff --git a/src/Split/net461/CollectionsMarshal.cs b/src/Split/net461/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net461/CollectionsMarshal.cs +++ b/src/Split/net461/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net462/CollectionsMarshal.cs b/src/Split/net462/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net462/CollectionsMarshal.cs +++ b/src/Split/net462/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net47/CollectionsMarshal.cs b/src/Split/net47/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net47/CollectionsMarshal.cs +++ b/src/Split/net47/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net471/CollectionsMarshal.cs b/src/Split/net471/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net471/CollectionsMarshal.cs +++ b/src/Split/net471/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net472/CollectionsMarshal.cs b/src/Split/net472/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net472/CollectionsMarshal.cs +++ b/src/Split/net472/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net48/CollectionsMarshal.cs b/src/Split/net48/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net48/CollectionsMarshal.cs +++ b/src/Split/net48/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net481/CollectionsMarshal.cs b/src/Split/net481/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/net481/CollectionsMarshal.cs +++ b/src/Split/net481/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net5.0/CollectionsMarshalSetCount.cs b/src/Split/net5.0/CollectionsMarshalSetCount.cs index 88d37845..07557737 100644 --- a/src/Split/net5.0/CollectionsMarshalSetCount.cs +++ b/src/Split/net5.0/CollectionsMarshalSetCount.cs @@ -29,9 +29,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net6.0/CollectionsMarshalSetCount.cs b/src/Split/net6.0/CollectionsMarshalSetCount.cs index 88d37845..07557737 100644 --- a/src/Split/net6.0/CollectionsMarshalSetCount.cs +++ b/src/Split/net6.0/CollectionsMarshalSetCount.cs @@ -29,9 +29,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/net7.0/CollectionsMarshalSetCount.cs b/src/Split/net7.0/CollectionsMarshalSetCount.cs index 88d37845..07557737 100644 --- a/src/Split/net7.0/CollectionsMarshalSetCount.cs +++ b/src/Split/net7.0/CollectionsMarshalSetCount.cs @@ -29,9 +29,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netcoreapp2.0/CollectionsMarshal.cs b/src/Split/netcoreapp2.0/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netcoreapp2.0/CollectionsMarshal.cs +++ b/src/Split/netcoreapp2.0/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netcoreapp2.1/CollectionsMarshal.cs b/src/Split/netcoreapp2.1/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netcoreapp2.1/CollectionsMarshal.cs +++ b/src/Split/netcoreapp2.1/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netcoreapp2.2/CollectionsMarshal.cs b/src/Split/netcoreapp2.2/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netcoreapp2.2/CollectionsMarshal.cs +++ b/src/Split/netcoreapp2.2/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netcoreapp3.0/CollectionsMarshal.cs b/src/Split/netcoreapp3.0/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netcoreapp3.0/CollectionsMarshal.cs +++ b/src/Split/netcoreapp3.0/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netcoreapp3.1/CollectionsMarshal.cs b/src/Split/netcoreapp3.1/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netcoreapp3.1/CollectionsMarshal.cs +++ b/src/Split/netcoreapp3.1/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netstandard2.0/CollectionsMarshal.cs b/src/Split/netstandard2.0/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netstandard2.0/CollectionsMarshal.cs +++ b/src/Split/netstandard2.0/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/netstandard2.1/CollectionsMarshal.cs b/src/Split/netstandard2.1/CollectionsMarshal.cs index b9d2378e..3b3b47e3 100644 --- a/src/Split/netstandard2.1/CollectionsMarshal.cs +++ b/src/Split/netstandard2.1/CollectionsMarshal.cs @@ -59,9 +59,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Split/uap10.0/CollectionsMarshal.cs b/src/Split/uap10.0/CollectionsMarshal.cs index 06555a18..5f7356d8 100644 --- a/src/Split/uap10.0/CollectionsMarshal.cs +++ b/src/Split/uap10.0/CollectionsMarshal.cs @@ -40,9 +40,23 @@ public static void SetCount(List list, int count) } else if (count > current) { - if (list.Capacity < count) + var capacity = list.Capacity; + if (capacity < count) { - list.Capacity = count; + long grown = 4; + if (capacity != 0) + { + grown = 2L * capacity; + } + if (grown > 0X7FEFFFFF) + { + grown = 0X7FEFFFFF; + } + if (grown < count) + { + grown = count; + } + list.Capacity = (int) grown; } for (var index = current; index < count; index++) { diff --git a/src/Tests/PolyfillTests_CollectionsMarshal.cs b/src/Tests/PolyfillTests_CollectionsMarshal.cs index 7a2a8213..ed39ba3e 100644 --- a/src/Tests/PolyfillTests_CollectionsMarshal.cs +++ b/src/Tests/PolyfillTests_CollectionsMarshal.cs @@ -46,6 +46,129 @@ public async Task CollectionsMarshal_SetCount_Grow() await Assert.That(list[1]).IsEqualTo(2); } + [Test] + public async Task CollectionsMarshal_SetCount_GrowKeepsItemsAndDefaultsTheRest() + { + var list = new List {"a", "b"}; + CollectionsMarshal.SetCount(list, 2); + CollectionsMarshal.SetCount(list, 3); + list[2] = "c"; + CollectionsMarshal.SetCount(list, 40); + list[39] = "z"; + + await Assert.That(list.Count).IsEqualTo(40); + await Assert.That(list.Capacity >= 40).IsTrue(); + await Assert.That(list[0]).IsEqualTo("a"); + await Assert.That(list[1]).IsEqualTo("b"); + await Assert.That(list[2]).IsEqualTo("c"); + await Assert.That(list[39]).IsEqualTo("z"); + await Assert.That(list.IndexOf("z")).IsEqualTo(39); + } + + [Test] + public async Task CollectionsMarshal_SetCount_GrowFromEmpty() + { + var list = new List(); + CollectionsMarshal.SetCount(list, 0); + await Assert.That(list.Count).IsEqualTo(0); + + CollectionsMarshal.SetCount(list, 1); + list[0] = 7; + await Assert.That(list.Count).IsEqualTo(1); + await Assert.That(list[0]).IsEqualTo(7); + + CollectionsMarshal.SetCount(list, 1000); + await Assert.That(list.Count).IsEqualTo(1000); + await Assert.That(list[0]).IsEqualTo(7); + await Assert.That(list.Capacity >= 1000).IsTrue(); + } + + [Test] + public async Task CollectionsMarshal_SetCount_GrowWithinCapacity() + { + var list = new List(16) {1, 2}; + CollectionsMarshal.SetCount(list, 10); + + await Assert.That(list.Count).IsEqualTo(10); + await Assert.That(list.Capacity).IsEqualTo(16); + await Assert.That(list[0]).IsEqualTo(1); + await Assert.That(list[1]).IsEqualTo(2); + } + + [Test] + public async Task CollectionsMarshal_SetCount_GrowsCapacityLikeList() + { + // an exact fit when doubling is not enough, otherwise double + var list = new List(); + CollectionsMarshal.SetCount(list, 5); + var afterFive = list.Capacity; + CollectionsMarshal.SetCount(list, 6); + var afterSix = list.Capacity; + CollectionsMarshal.SetCount(list, 7); + var afterSeven = list.Capacity; + CollectionsMarshal.SetCount(list, 25); + var afterTwentyFive = list.Capacity; + + var empty = new List(); + CollectionsMarshal.SetCount(empty, 1); + var afterOne = empty.Capacity; + + await Assert.That(afterFive).IsEqualTo(5); + await Assert.That(afterSix).IsEqualTo(10); + await Assert.That(afterSeven).IsEqualTo(10); + await Assert.That(afterTwentyFive).IsEqualTo(25); + await Assert.That(afterOne).IsEqualTo(4); + } + + [Test] + public async Task CollectionsMarshal_SetCount_IncrementalGrowth() + { + var list = new List(); + for (var count = 1; count <= 20000; count++) + { + CollectionsMarshal.SetCount(list, count); + list[count - 1] = count; + } + + var mismatches = 0; + for (var index = 0; index < list.Count; index++) + { + if (list[index] != index + 1) + { + mismatches++; + } + } + + await Assert.That(list.Count).IsEqualTo(20000); + await Assert.That(mismatches).IsEqualTo(0); + await Assert.That(list.Capacity >= 20000).IsTrue(); + await Assert.That(list.Capacity <= 40000).IsTrue(); + } + + [Test] + public async Task CollectionsMarshal_SetCount_ShrinkThenGrow() + { + var list = new List {1, 2, 3, 4}; + var capacity = list.Capacity; + CollectionsMarshal.SetCount(list, 1); + CollectionsMarshal.SetCount(list, 3); + list[1] = 20; + list[2] = 30; + + await Assert.That(list.Count).IsEqualTo(3); + await Assert.That(list.Capacity).IsEqualTo(capacity); + await Assert.That(list[0]).IsEqualTo(1); + await Assert.That(list[1]).IsEqualTo(20); + await Assert.That(list[2]).IsEqualTo(30); + } + + [Test] + public async Task CollectionsMarshal_SetCount_InvalidArguments() + { + await Assert.That(() => CollectionsMarshal.SetCount(new List(), -1)).Throws(); + await Assert.That(() => CollectionsMarshal.SetCount(null!, 1)).Throws(); + } + [Test] public async Task CollectionsMarshal_SetCount_Shrink() {