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
Show file tree
Hide file tree
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
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 over a regular for loop on an older processor, more on a modern processor
let mutable i = 0
while i < len - len % 8 do
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
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 over a regular for loop on an older processor, more on a modern processor
let mutable i = 0
while i < len - len % 8 do
f.Invoke(i, str.[i])
f.Invoke(i + 1, str.[i + 1])
f.Invoke(i + 2, str.[i + 2])
f.Invoke(i + 3, str.[i + 3])
f.Invoke(i + 4, str.[i + 4])
f.Invoke(i + 5, str.[i + 5])
f.Invoke(i + 6, str.[i + 6])
f.Invoke(i + 7, str.[i + 7])
i <- i + 8

// The remainder
while i < len do
f.Invoke(i, str.[i])
i <- i + 1

[<CompiledName("Map")>]
let map (mapping: char -> char) (str:string) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FSharp.Core.UnitTests.Collections

open System
open System.Text
open NUnit.Framework

open FSharp.Core.UnitTests.LibraryTestFx
Expand Down Expand Up @@ -59,6 +60,28 @@ type StringModule() =
do String.iter (fun c -> result := !result + (int c)) null
Assert.AreEqual(0, !result)

// These tests test the unrolling cut-off points
let mutable x = 0
do String.iter (fun _ -> x <- x + 1) "1"
Assert.AreEqual(x, 1)

let mutable x = 0
do String.iter (fun _ -> x <- x + 1) "1234567"
Assert.AreEqual(x, 7)

let mutable x = 0
do String.iter (fun _ -> x <- x + 1) "12345678"
Assert.AreEqual(x, 8)

let mutable x = 0
do String.iter (fun _ -> x <- x + 1) "123456789"
Assert.AreEqual(x, 9)

// Test order of execution
let sb = StringBuilder()
do String.iter (fun c -> sb.Append c |> ignore) "abcdefghijklmnopqrstuvwxyz"
Assert.AreEqual(sb.ToString(), "abcdefghijklmnopqrstuvwxyz")

[<Test>]
member this.IterI() =
let result = ref 0
Expand All @@ -69,6 +92,28 @@ type StringModule() =
do String.iteri(fun i c -> result := !result + (i*(int c))) null
Assert.AreEqual(0, !result)

// These tests test the unrolling cut-off points
let mutable x = 42
do String.iteri (fun i _ -> x <- x + i) "1"
Assert.AreEqual(x, 42)

let mutable x = 0
do String.iteri (fun i _ -> x <- x + i) "1234567"
Assert.AreEqual(x, 21)

let mutable x = 0
do String.iteri (fun i _ -> x <- x + i) "12345678"
Assert.AreEqual(x, 28)

let mutable x = 0
do String.iteri (fun i _ -> x <- x + i) "123456789"
Assert.AreEqual(x, 36)

// Test order of execution
let sb = StringBuilder()
do String.iteri (fun i c -> sb.Append [|char i; c|] |> ignore) "abcdefghijklmnopqrstuvwxyz"
Assert.AreEqual(sb.ToString(), "\u0000a\u0001b\u0002c\u0003d\u0004e\u0005f\u0006g\u0007h\u0008i\u0009j\u000Ak\u000Bl\u000Cm\u000Dn\u000Eo\u000Fp\u0010q\u0011r\u0012s\u0013t\u0014u\u0015v\u0016w\u0017x\u0018y\u0019z")

[<Test>]
member this.Map() =
let e1 = String.map (fun c -> c) "foo"
Expand Down