Skip to content

Commit 8e276dd

Browse files
abelbraaksmacartermp
authored andcommitted
Improve performance of String.concat for seq of type array or list (#9483)
* Move `String.length` to the top of its module so that the `length` function is in scope * Improve performance of String.concat for arrays and lists * Make concatArray local to String.concat * Testing String.concat, make sure the new three paths are covered * Remove "foo", "bax", "bar" in tests, making them more readable Co-authored-by: Phillip Carter <pcarter@fastmail.com>
1 parent c435a71 commit 8e276dd

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/fsharp/FSharp.Core/string.fs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.FSharp.Core
88
open Microsoft.FSharp.Core.Operators
99
open Microsoft.FSharp.Core.Operators.Checked
1010
open Microsoft.FSharp.Collections
11+
open Microsoft.FSharp.Primitives.Basics
1112

1213
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
1314
[<RequireQualifiedAccess>]
@@ -22,7 +23,25 @@ namespace Microsoft.FSharp.Core
2223

2324
[<CompiledName("Concat")>]
2425
let concat sep (strings : seq<string>) =
25-
String.Join(sep, strings)
26+
27+
let concatArray sep (strings: string []) =
28+
match length sep with
29+
| 0 -> String.Concat strings
30+
// following line should be used when this overload becomes part of .NET Standard (it's only in .NET Core)
31+
//| 1 -> String.Join(sep.[0], strings, 0, strings.Length)
32+
| _ -> String.Join(sep, strings, 0, strings.Length)
33+
34+
match strings with
35+
| :? array<string> as arr ->
36+
concatArray sep arr
37+
38+
| :? list<string> as lst ->
39+
lst
40+
|> List.toArray
41+
|> concatArray sep
42+
43+
| _ ->
44+
String.Join(sep, strings)
2645

2746
[<CompiledName("Iterate")>]
2847
let iter (action : (char -> unit)) (str:string) =

0 commit comments

Comments
 (0)