-
Notifications
You must be signed in to change notification settings - Fork 814
Array.Parallel - search functions (tryFindIndex,tryFind,tryPick) #14827
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
T-Gro
merged 11 commits into
dotnet:main
from
T-Gro:14217-arrayparallel-searchfunctions
Mar 14, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec2dd7d
Searching functions added
T-Gro 5f17d18
Search functions added to surface area
T-Gro 6c3355d
Formatting
T-Gro 0caaed6
Merge branch 'main' into 14217-arrayparallel-searchfunctions
T-Gro 6726460
Merge remote-tracking branch 'upstream/main' into 14217-arrayparallel…
T-Gro 765a913
Tests for parallel search functions
T-Gro dae90b0
Adding [<Experimental("Experimental library feature, requires '--lang…
T-Gro e77751c
ups
T-Gro c00f95b
Apply suggestions from code review
T-Gro 987ec59
Update src/FSharp.Core/array.fsi
T-Gro 526ddcd
Merge branch 'main' into 14217-arrayparallel-searchfunctions
T-Gro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1934,6 +1934,46 @@ module Array = | |||||
module Parallel = | ||||||
open System.Threading.Tasks | ||||||
|
||||||
[<CompiledName("TryFindIndex")>] | ||||||
let tryFindIndex predicate (array: _[]) = | ||||||
checkNonNull "array" array | ||||||
|
||||||
let pResult = | ||||||
Parallel.For( | ||||||
0, | ||||||
array.Length, | ||||||
(fun i pState -> | ||||||
if predicate array[i] then | ||||||
pState.Break()) | ||||||
) | ||||||
|
||||||
pResult.LowestBreakIteration |> Option.ofNullable |> Option.map int | ||||||
|
||||||
[<CompiledName("TryFind")>] | ||||||
let tryFind predicate (array: _[]) = | ||||||
array |> tryFindIndex predicate |> Option.map (fun i -> array[i]) | ||||||
|
||||||
[<CompiledName("TryPick")>] | ||||||
let tryPick chooser (array: _[]) = | ||||||
checkNonNull "array" array | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Someday, we should add a
Suggested change
|
||||||
let allChosen = new System.Collections.Concurrent.ConcurrentDictionary<_, _>() | ||||||
|
||||||
let pResult = | ||||||
Parallel.For( | ||||||
0, | ||||||
array.Length, | ||||||
(fun i pState -> | ||||||
match chooser array[i] with | ||||||
| None -> () | ||||||
| chosenElement -> | ||||||
allChosen[i] <- chosenElement | ||||||
pState.Break()) | ||||||
) | ||||||
|
||||||
pResult.LowestBreakIteration | ||||||
|> Option.ofNullable | ||||||
|> Option.bind (fun i -> allChosen[int i]) | ||||||
|
||||||
[<CompiledName("Choose")>] | ||||||
let choose chooser (array: 'T[]) = | ||||||
checkNonNull "array" array | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it would matter most of the time (especially since this is
Array.Parallel
and not regularArray
, and allocations are probably gonna happen anyway), but given that this is standard library code that probably won't change very often, it might not hurt to avoid the extra allocations here123:Footnotes
2 for
tryFindIndex
: oneSome
fromOption.ofNullable
and another fromOption.map
. ↩3 for
tryFind
: oneSome
fromOption.ofNullable
, another from the firstOption.map
, and a third from the outerOption.map
. ↩Or maybe I've just read too many Stephen Toub PRs 🙃. ↩