@@ -130,49 +130,55 @@ module AsyncVal =
130
130
/// executed asynchronously, one by one with regard to their order in array.
131
131
/// Returned array maintain order of values.
132
132
/// If the array contains a Failure, then the entire array will not resolve
133
- let collectSequential ( values : AsyncVal < 'T >[]) : AsyncVal < 'T []> =
134
- if values.Length = 0 then Value [||]
135
- elif values |> Array.exists isAsync then
133
+ let collectSequential ( values : AsyncVal < 'T > seq ) : AsyncVal < 'T []> =
134
+ let values = new PooledResizeArray<_> ( values)
135
+ let length = values.Count
136
+ if length = 0 then Value [||]
137
+ elif values.Exists isAsync then
136
138
Async ( async {
137
- let results = Array.zeroCreate values.Length
138
- let exceptions = ResizeArray values.Length
139
- for i = 0 to values.Length - 1 do
139
+ let results = Array.zeroCreate length
140
+ use exceptions = new PooledResizeArray <_> ( length )
141
+ for i = 0 to length - 1 do
140
142
let v = values.[ i]
141
143
match v with
142
144
| Value v -> results.[ i] <- v
143
145
| Async a ->
144
146
let! r = a
145
147
results.[ i] <- r
146
148
| Failure f -> exceptions.Add f
149
+ values.Dispose()
147
150
match exceptions.Count with
148
151
| 0 -> return results
149
152
| 1 -> return exceptions.First() .Reraise ()
150
- | _ -> return AggregateException exceptions |> raise
153
+ | _ -> return AggregateException ( exceptions.AsReadOnly ()) |> raise
151
154
})
152
155
else
153
- let exceptions =
156
+ use values = values
157
+ use exceptions =
154
158
values
155
- |> Array.choose ( function
156
- | Failure f -> Some f
157
- | _ -> None )
158
- match exceptions.Length with
159
- | 0 -> Value ( values |> Array .map ( fun ( Value v ) -> v))
159
+ |> PooledResizeArray.vChoose ( function
160
+ | Failure f -> ValueSome f
161
+ | _ -> ValueNone )
162
+ match exceptions.Count with
163
+ | 0 -> Value ( values |> Seq .map ( fun ( Value v ) -> v) |> Seq.toArray )
160
164
| 1 -> Failure ( exceptions.First ())
161
- | _ -> Failure ( AggregateException exceptions)
165
+ | _ -> Failure ( AggregateException ( exceptions.AsReadOnly ()) )
162
166
163
167
/// Converts array of AsyncVals into AsyncVal with array results.
164
168
/// In case when are non-immediate values in provided array, they are
165
169
/// executed all in parallel, in unordered fashion. Order of values
166
170
/// inside returned array is maintained.
167
171
/// If the array contains a Failure, then the entire array will not resolve
168
- let collectParallel ( values : AsyncVal < 'T >[]) : AsyncVal < 'T []> =
169
- if values.Length = 0 then Value [||]
172
+ let collectParallel ( values : AsyncVal < 'T > seq ) : AsyncVal < 'T []> =
173
+ use values = new PooledResizeArray<_> ( values)
174
+ let length = values.Count
175
+ if length = 0 then Value [||]
170
176
else
171
- let indexes = List <_> ( 0 )
172
- let continuations = List <_> ( 0 )
173
- let results = Array.zeroCreate values.Length
174
- let exceptions = ResizeArray values.Length
175
- for i = 0 to values.Length - 1 do
177
+ let indexes = new PooledResizeArray <_> ( length )
178
+ let continuations = new PooledResizeArray <_> ( length )
179
+ let results = Array.zeroCreate length
180
+ use exceptions = new PooledResizeArray <_> ( length )
181
+ for i = 0 to length - 1 do
176
182
let value = values.[ i]
177
183
match value with
178
184
| Value v -> results.[ i] <- v
@@ -182,13 +188,15 @@ module AsyncVal =
182
188
| Failure f -> exceptions.Add f
183
189
match exceptions.Count with
184
190
| 1 -> AsyncVal.Failure ( exceptions.First ())
185
- | count when count > 1 -> AsyncVal.Failure ( AggregateException exceptions)
191
+ | count when count > 1 -> AsyncVal.Failure ( AggregateException ( exceptions.AsReadOnly ()) )
186
192
| _ ->
187
193
if indexes.Count = 0 then Value ( results)
188
194
else Async ( async {
189
195
let! vals = continuations |> Async.Parallel
190
196
for i = 0 to indexes.Count - 1 do
191
197
results.[ indexes.[ i]] <- vals.[ i]
198
+ indexes.Dispose()
199
+ continuations.Dispose()
192
200
return results
193
201
})
194
202
0 commit comments