Skip to content

Commit a3a50c0

Browse files
This looks faster
1 parent 0b1b32d commit a3a50c0

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/FSharp.Core/array.fs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2208,22 +2208,26 @@ module Array =
22082208
[<CompiledName("TryPick")>]
22092209
let tryPick chooser (array: _ array) =
22102210
checkNonNull "array" array
2211-
let allChosen = System.Collections.Concurrent.ConcurrentDictionary()
22122211

2213-
let pResult =
2212+
let syncRoot = obj()
2213+
let mutable lo = Int32.MaxValue
2214+
let mutable chosen = None
2215+
2216+
let _pResult =
22142217
Parallel.For(
22152218
0,
22162219
array.Length,
22172220
(fun i pState ->
22182221
match chooser array[i] with
22192222
| None -> ()
22202223
| chosenElement ->
2221-
allChosen[i] <- chosenElement
2222-
pState.Break())
2223-
)
2224+
lock syncRoot (fun () ->
2225+
if i < lo then
2226+
lo <- i
2227+
chosen <- chosenElement)
2228+
pState.Break()))
22242229

2225-
if pResult.LowestBreakIteration.HasValue then allChosen[int (pResult.LowestBreakIteration.GetValueOrDefault())]
2226-
else None
2230+
chosen
22272231

22282232
[<CompiledName("Choose")>]
22292233
let choose chooser (array: 'T array) =

0 commit comments

Comments
 (0)