Skip to content

Reduce allocations in some Array.Parallel funcs #17505

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

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2179,9 +2179,8 @@ module Array =
// Not exists $condition <==> (opposite of $condition is true forall)
exists (predicate >> not) array |> not

[<CompiledName("TryFindIndex")>]
let tryFindIndex predicate (array: _ array) =
checkNonNull "array" array
let inline tryFindIndexAux predicate (array: _ array) =
checkNonNull (nameof array) array

let pResult =
Parallel.For(
Expand All @@ -2192,16 +2191,24 @@ module Array =
pState.Break())
)

pResult.LowestBreakIteration |> Option.ofNullable |> Option.map int
pResult.LowestBreakIteration

[<CompiledName("TryFindIndex")>]
let tryFindIndex predicate (array: _ array) =
let i = tryFindIndexAux predicate array
if i.HasValue then Some (int (i.GetValueOrDefault()))
else None

[<CompiledName("TryFind")>]
let tryFind predicate (array: _ array) =
array |> tryFindIndex predicate |> Option.map (fun i -> array[i])
let i = tryFindIndexAux predicate array
if i.HasValue then Some array[int (i.GetValueOrDefault())]
else None

[<CompiledName("TryPick")>]
let tryPick chooser (array: _ array) =
checkNonNull "array" array
let allChosen = new System.Collections.Concurrent.ConcurrentDictionary<_, _>()
let allChosen = System.Collections.Concurrent.ConcurrentDictionary()

let pResult =
Parallel.For(
Expand All @@ -2215,9 +2222,8 @@ module Array =
pState.Break())
)

pResult.LowestBreakIteration
|> Option.ofNullable
|> Option.bind (fun i -> allChosen[int i])
if pResult.LowestBreakIteration.HasValue then allChosen[int (pResult.LowestBreakIteration.GetValueOrDefault())]
else None

[<CompiledName("Choose")>]
let choose chooser (array: 'T array) =
Expand Down
Loading