Skip to content

Commit 53a235e

Browse files
committed
Improve performance of String.concat for arrays and lists
1 parent c777c6a commit 53a235e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/fsharp/FSharp.Core/string.fs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,34 @@ 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>]
1415
module String =
1516
[<CompiledName("Length")>]
1617
let length (str:string) = if isNull str then 0 else str.Length
1718

19+
let private concatArray sep (strings: string []) =
20+
match length sep with
21+
| 0 -> String.Concat strings
22+
// following line should be used when this overload becomes part of .NET Standard (it's only in .NET Core)
23+
//| 1 -> String.Join(sep.[0], strings, 0, strings.Length)
24+
| _ -> String.Join(sep, strings, 0, strings.Length)
25+
1826
[<CompiledName("Concat")>]
1927
let concat sep (strings : seq<string>) =
20-
String.Join(sep, strings)
28+
match strings with
29+
| :? array<string> as arr ->
30+
concatArray sep arr
31+
32+
| :? list<string> as lst ->
33+
lst
34+
|> List.toArray
35+
|> concatArray sep
36+
37+
| _ ->
38+
String.Join(sep, strings)
2139

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

0 commit comments

Comments
 (0)