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

Improve perf or String.iter and String.iteri by 25% #9497

Closed
Closed
Changes from 1 commit
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
48 changes: 42 additions & 6 deletions src/fsharp/FSharp.Core/string.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,52 @@ namespace Microsoft.FSharp.Core

[<CompiledName("Iterate")>]
let iter (action : (char -> unit)) (str:string) =
if not (String.IsNullOrEmpty str) then
for i = 0 to str.Length - 1 do
action str.[i]
let len = length str

if len = 0 then ()
else
// unrolling gives a ~25% boost on older proc, more on modern proc
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
let mutable i = 0
while i < len - len % 8 do
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
action str.[i]
action str.[i + 1]
action str.[i + 2]
action str.[i + 3]
action str.[i + 4]
action str.[i + 5]
action str.[i + 6]
action str.[i + 7]
i <- i + 8

// the remainder
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
while i < len do
action str.[i]
i <- i + 1

[<CompiledName("IterateIndexed")>]
let iteri action (str:string) =
if not (String.IsNullOrEmpty str) then
let len = length str
if len = 0 then ()
else
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(action)
for i = 0 to str.Length - 1 do
f.Invoke(i, str.[i])

// unrolling gives a ~25% boost on older proc, more on modern proc
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
let mutable i = 0
while i < len - len % 8 do
f.Invoke(i, str.[i])
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
f.Invoke(i, str.[i + 1])
f.Invoke(i, str.[i + 2])
f.Invoke(i, str.[i + 3])
f.Invoke(i, str.[i + 4])
f.Invoke(i, str.[i + 5])
f.Invoke(i, str.[i + 6])
f.Invoke(i, str.[i + 7])
i <- i + 8

// the remainder
abelbraaksma marked this conversation as resolved.
Show resolved Hide resolved
while i < len do
f.Invoke(i, str.[i])
i <- i + 1

[<CompiledName("Map")>]
let map (mapping: char -> char) (str:string) =
Expand Down