Skip to content

Commit 83728f9

Browse files
committed
fixup! perf: choose collects the elements in an array that is rented from the pool for a long list
1 parent 9884cd7 commit 83728f9

5 files changed

Lines changed: 156 additions & 29 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module's `.fsi` and the `.fs` on its `.fs`, so that Visual Studio shows them as
3535
│ │ ├── ListArrays.fs – the List functions of two or three lists, over arrays
3636
│ │ ├── Snapshot.fs – the snapshots in arrays of the collections without an underlying array
3737
│ │ ├── Collectors.fs – the accumulators that build an indexed list from many collections
38+
│ │ ├── Walk.fs – the loops over an indexed list with the enumerator that is faster for the element type
39+
│ │ ├── Buffers.fs – the arrays, new or rented, in which the elements of a result wait for their tree
3840
│ │ ├── Lengths.fs – the lengths of queues and stacks, counted as far as a caller needs
3941
│ │ ├── DescendingSorts.fs – the stable descending sorts in the order of F# generic comparison
4042
│ │ └── ListRanges.fs – the range checks of the functions that wrap members of ImmutableList

‎src/FSharp.Collections.Immutable/Helper/Buffers.fs‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
/// <see cref="F:FSharp.Collections.Immutable.Buffers.PooledUpTo"/> elements long; a longer result moves into a list.
2020
/// </para>
2121
/// <para>
22-
/// A buffer that is not turned into a list, because the chooser threw, is not returned to the pool. The pool allocates
23-
/// another array, and nothing leaks.
22+
/// The buffer is disposable, and the function that uses it disposes it: the rented array goes back to the pool also when
23+
/// the chooser throws before the list is built.
2424
/// </para>
2525
/// </remarks>
2626
module internal FSharp.Collections.Immutable.Buffers
@@ -54,33 +54,39 @@ let PooledUpTo = 1048576
5454
/// Collects the elements that a function chooses from a source of a known length, and builds the indexed list of them.
5555
/// </summary>
5656
/// <typeparam name="T">The type of the elements.</typeparam>
57+
/// <param name="bound">The number of elements to choose from, which is the most that the result can have.</param>
58+
/// <param name="pool">The pool that lends the array of a long result.</param>
5759
/// <remarks>
58-
/// A buffer is used once: <see cref="M:FSharp.Collections.Immutable.Buffers.ChosenBuffer`1.ToImmutable"/> ends it.
60+
/// A buffer is used once: <see cref="M:FSharp.Collections.Immutable.Buffers.ChosenBuffer`1.ToImmutable"/> ends it. The
61+
/// pool is a parameter so that a test can give one that records what it lends and what it gets back.
5962
/// </remarks>
6063
[<Sealed>]
61-
type ChosenBuffer<'T> (bound : int) =
64+
type ChosenBuffer<'T> (bound : int, pool : ArrayPool<'T>) =
6265

6366
let pooled = bound >= PooledFrom
6467

6568
let mutable buffer : 'T array =
66-
if bound = 0 then
67-
Array.Empty<'T>()
68-
elif pooled then
69-
ArrayPool<'T>.Shared.Rent(min bound PooledUpTo)
70-
else
71-
Array.zeroCreate bound
69+
if bound = 0 then Array.Empty<'T>()
70+
elif pooled then pool.Rent (min bound PooledUpTo)
71+
else Array.zeroCreate bound
7272

7373
let mutable count = 0
7474
let mutable overflow : ResizeArray<'T> | null = null
7575

76+
/// <summary>
77+
/// Creates a buffer that rents the array of a long result from <see cref="P:System.Buffers.ArrayPool`1.Shared"/>.
78+
/// </summary>
79+
/// <param name="bound">The number of elements to choose from, which is the most that the result can have.</param>
80+
new (bound : int) = new ChosenBuffer<'T> (bound, ArrayPool<'T>.Shared)
81+
7682
/// Returns the rented array to the pool, cleared as far as it was written when it holds references. The buffer is
7783
/// left with an empty array, which is full at once, so that an element goes to the slow path of Add.
7884
member private _.Release () =
7985
if pooled && buffer.Length > 0 then
8086
if RuntimeHelpers.IsReferenceOrContainsReferences<'T>() then
8187
Array.Clear (buffer, 0, count)
8288

83-
ArrayPool<'T>.Shared.Return(buffer, false)
89+
pool.Return (buffer, false)
8490

8591
buffer <- Array.Empty<'T>()
8692
count <- 0
@@ -123,3 +129,14 @@ type ChosenBuffer<'T> (bound : int) =
123129
this.Release ()
124130
result
125131
| spilled -> ImmutableList.Create<'T>(Span<'T>.op_Implicit(CollectionsMarshal.AsSpan spilled))
132+
133+
interface IDisposable with
134+
135+
/// <summary>
136+
/// Returns the rented array to the pool when the buffer still holds it.
137+
/// </summary>
138+
/// <remarks>
139+
/// A buffer that built its list, or that moved its elements into a list, holds no rented array, and nothing
140+
/// happens. The call matters when the chooser throws before the list is built.
141+
/// </remarks>
142+
member this.Dispose () = this.Release ()

‎src/FSharp.Collections.Immutable/IndexedList/IIndexedList.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ let append (list1 : IIndexedList<'T>) (list2 : IIndexedList<'T>) =
371371
let choose (chooser : 'T -> 'U voption) (list : IIndexedList<'T>) =
372372
checkNotNull (nameof list) list
373373
// As IndexedList.choose: the chosen elements wait in an array as long as the list (see Buffers)
374-
let chosen = Buffers.ChosenBuffer<'U> list.Count
374+
use chosen = new Buffers.ChosenBuffer<'U> (list.Count)
375375

376376
for item in list do
377377
match chooser item with

‎src/FSharp.Collections.Immutable/IndexedList/IndexedList.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ let choose (chooser : 'T -> 'U voption) (list : IndexedList<'T>) =
507507
// 100 000 elements), and it took no less time than the array. A builder rebalances its tree
508508
// after every Add and took 3.3 to 3.6 times as long for integers and 2.5 times for strings of 100 000 to a million
509509
// elements; there is no tree of an input to join to its tree here, as collect and concat have (see Collectors).
510-
let chosen = Buffers.ChosenBuffer<'U> list.Count
510+
use chosen = new Buffers.ChosenBuffer<'U> (list.Count)
511511

512512
Walk.iter
513513
(fun item ->
Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,66 @@
11
namespace FSharp.Collections.Immutable.Tests
22

3+
open System
34
open System.Buffers
45
open FSharp.Collections.Immutable
56
open Microsoft.VisualStudio.TestTools.UnitTesting
67

8+
/// <summary>
9+
/// A pool that records the arrays that it lends and what each of them holds when it comes back.
10+
/// </summary>
11+
/// <remarks>
12+
/// The shared pool does not tell which array it gets back, and the next array that it lends need not be that one, so a test
13+
/// of the return needs a pool of its own. The arrays are longer than asked and full of a marker, as an array of the shared
14+
/// pool can be longer and can hold what its last user left.
15+
/// </remarks>
16+
type RecordingPool<'T> (marker : 'T) =
17+
inherit ArrayPool<'T> ()
18+
19+
let lent = ResizeArray<'T array>()
20+
let returned = ResizeArray<struct ('T array * 'T array)>()
21+
22+
/// The arrays that the pool lent, in the order of the calls.
23+
member _.Lent = lent
24+
25+
/// The arrays that came back, each with a copy of what it held when it came back.
26+
member _.Returned = returned
27+
28+
override _.Rent (minimumLength : int) =
29+
let array = Array.create (minimumLength + 3) marker
30+
lent.Add array
31+
array
32+
33+
override _.Return (array : 'T array, _clearArray : bool) = returned.Add (struct (array, Array.copy array))
34+
735
/// The buffer in which `choose` collects the elements: an array of its own below `Buffers.PooledFrom` elements, one rented
836
/// from the pool above, and a list for a result that does not fit the rented array.
937
[<TestClass; IndexedListTestCategory>]
1038
type ChosenBufferTests () =
1139

40+
/// The marker of the arrays of the recording pool of strings.
41+
static let stale = "stale"
42+
43+
/// Asserts that a pool of strings lent one array and got it back with the written elements cleared and the rest as it was.
44+
static let assertReturnedClean (pool : RecordingPool<string>) (written : int) =
45+
let lent = Assert.ContainsSingle (pool.Lent, "The buffer must rent one array")
46+
let struct (array, contents) =
47+
Assert.ContainsSingle (pool.Returned, "The buffer must give one array back")
48+
Assert.AreSame<string array>(lent, array, "The buffer must give back the array that it rented")
49+
50+
Assert.IsEmpty (
51+
contents
52+
|> Seq.take written
53+
|> Seq.filter (fun item -> not (obj.ReferenceEquals (item, null))),
54+
"The array must come back without the elements that the buffer wrote"
55+
)
56+
57+
Assert.IsEmpty (
58+
contents
59+
|> Seq.skip written
60+
|> Seq.filter (fun item -> not (obj.ReferenceEquals (item, stale))),
61+
"The buffer must leave the part of the array that it did not write as it was"
62+
)
63+
1264
[<TestMethod>]
1365
[<DataRow(0, 0)>]
1466
[<DataRow(1, 1)>]
@@ -19,7 +71,7 @@ type ChosenBufferTests () =
1971
[<DataRow(5000, 17)>]
2072
[<DataRow(5000, 5000)>]
2173
member _.``the buffer builds the list of the elements that were added`` (bound : int, count : int) =
22-
let buffer = Buffers.ChosenBuffer<int> bound
74+
use buffer = new Buffers.ChosenBuffer<int> (bound)
2375

2476
for value in 0 .. count - 1 do
2577
buffer.Add value
@@ -33,11 +85,15 @@ type ChosenBufferTests () =
3385
[<TestMethod>]
3486
member _.``a result longer than the rented array moves into a list`` () =
3587
let count = Buffers.PooledUpTo + 10
36-
let buffer = Buffers.ChosenBuffer<int> count
88+
let pool = RecordingPool<int> -1
89+
use buffer = new Buffers.ChosenBuffer<int> (count, pool)
3790

3891
for value in 0 .. count - 1 do
3992
buffer.Add value
4093

94+
Assert.ContainsSingle (pool.Returned, "The buffer must give the rented array back when its elements move into a list")
95+
|> ignore
96+
4197
let list = buffer.ToImmutable ()
4298
Assert.HasCount (count, list, "The buffer must keep all elements of a result longer than the rented array")
4399
Assert.AreEqual<int>(
@@ -51,21 +107,73 @@ type ChosenBufferTests () =
51107
"The first element after the rented array must follow it"
52108
)
53109
Assert.AreEqual<int>(count - 1, list[count - 1], "The last element must stay")
110+
Assert.ContainsSingle (pool.Returned, "The buffer must not give an array back a second time")
111+
|> ignore
54112

55-
[<TestMethod; DataRow 1024; DataRow 5000>]
56-
member _.``the array that a buffer gives back holds no references`` (count : int) =
57-
let buffer = Buffers.ChosenBuffer<string> count
113+
[<TestMethod; DataRow 0; DataRow 1; DataRow(Buffers.PooledFrom - 1)>]
114+
member _.``a short buffer rents no array`` (bound : int) =
115+
let pool = RecordingPool<int> -1
116+
use buffer = new Buffers.ChosenBuffer<int> (bound, pool)
58117

59-
for _ in 1..count do
60-
buffer.Add "x"
118+
for value in 0 .. bound - 1 do
119+
buffer.Add value
61120

62121
buffer.ToImmutable () |> ignore
63-
let rented = ArrayPool<string>.Shared.Rent count
64-
65-
try
66-
let leftovers =
67-
rented
68-
|> Array.filter (fun item -> not (obj.ReferenceEquals (item, null)))
69-
Assert.IsEmpty (leftovers, "The pool must not keep the elements that a buffer held")
70-
finally
71-
ArrayPool<string>.Shared.Return rented
122+
Assert.IsEmpty (pool.Lent, "A buffer below the least pooled length must take an array of its own")
123+
124+
[<TestMethod; DataRow(Buffers.PooledFrom, Buffers.PooledFrom); DataRow(5000, 17); DataRow(5000, 0); DataRow(5000, 5000)>]
125+
member _.``a buffer that builds its list gives its array back without the elements`` (bound : int, count : int) =
126+
let pool = RecordingPool<string> stale
127+
128+
do
129+
use buffer = new Buffers.ChosenBuffer<string> (bound, pool)
130+
131+
for _ in 1..count do
132+
buffer.Add "x"
133+
134+
buffer.ToImmutable () |> ignore
135+
136+
// The buffer is disposed after it built the list, which must not return the array again
137+
assertReturnedClean pool count
138+
139+
[<TestMethod; DataRow(Buffers.PooledFrom, Buffers.PooledFrom); DataRow(5000, 17); DataRow(5000, 0)>]
140+
member _.``a buffer that is disposed before it builds a list gives its array back without the elements``
141+
(bound : int, count : int)
142+
=
143+
let pool = RecordingPool<string> stale
144+
145+
// As in choose, when the chooser throws: the buffer is disposed and ToImmutable does not run
146+
Assert.ThrowsExactly<InvalidOperationException>(
147+
Action (fun () ->
148+
use buffer = new Buffers.ChosenBuffer<string> (bound, pool)
149+
150+
for _ in 1..count do
151+
buffer.Add "x"
152+
153+
invalidOp "the chooser throws"
154+
),
155+
"The exception of the chooser must pass through"
156+
)
157+
|> ignore
158+
159+
assertReturnedClean pool count
160+
161+
[<TestMethod>]
162+
member _.``an array of values comes back as it was written`` () =
163+
let pool = RecordingPool<int> -1
164+
165+
do
166+
use buffer = new Buffers.ChosenBuffer<int> (5000, pool)
167+
168+
for value in 0..16 do
169+
buffer.Add value
170+
171+
buffer.ToImmutable () |> ignore
172+
173+
let struct (_, contents) = Assert.ContainsSingle (pool.Returned, "The buffer must give one array back")
174+
175+
CollectionAssert.AreEqual (
176+
Array.init 17 id,
177+
Array.take 17 contents,
178+
"An array of values holds no references, so the buffer does not clear it"
179+
)

0 commit comments

Comments
 (0)