Skip to content
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

Array.choose with less memory use #1461

Merged
merged 1 commit into from Aug 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 45 additions & 10 deletions src/fsharp/FSharp.Core/array.fs
Expand Up @@ -472,16 +472,51 @@ namespace Microsoft.FSharp.Collections
loop 0

[<CompiledName("Choose")>]
let choose f (array: _[]) =
checkNonNull "array" array
let res = Array.zeroCreateUnchecked array.Length
let mutable count = 0
for x in array do
match f x with
| None -> ()
| Some b -> res.[count] <- b
count <- count + 1
Array.subUnchecked 0 count res
let choose (f: 'T -> 'U Option) (array: 'T[]) =
checkNonNull "array" array

let mutable i = 0
let mutable first = Unchecked.defaultof<'U>
let mutable found = false
while i < array.Length && not found do
let element = array.[i]
match f element with
| None -> i <- i + 1
| Some b -> first <- b; found <- true

if i <> array.Length then

let chunk1 : 'U[] = Array.zeroCreateUnchecked ((array.Length >>> 2) + 1)
chunk1.[0] <- first
let mutable count = 1
i <- i + 1
while count < chunk1.Length && i < array.Length do
let element = array.[i]
match f element with
| None -> ()
| Some b -> chunk1.[count] <- b
count <- count + 1
i <- i + 1

if i < array.Length then
let chunk2 : 'U[] = Array.zeroCreateUnchecked (array.Length-i)
count <- 0
while i < array.Length do
let element = array.[i]
match f element with
| None -> ()
| Some b -> chunk2.[count] <- b
count <- count + 1
i <- i + 1

let res : 'U[] = Array.zeroCreateUnchecked (chunk1.Length + count)
Array.Copy(chunk1,res,chunk1.Length)
Array.Copy(chunk2,0,res,chunk1.Length,count)
res
else
Array.subUnchecked 0 count chunk1
else
empty

[<CompiledName("Filter")>]
let filter f (array: _[]) =
Expand Down