Skip to content

Conversation

saul
Copy link
Contributor

@saul saul commented May 26, 2020

While profiling design time builds I noticed that the Seq.fold here is responsible for most of the Fsc task's run time:

pastedImage (3)

@saul saul force-pushed the string-concat-commandline-args branch from d813884 to ac4a13b Compare May 26, 2020 16:04
@KevinRansom
Copy link
Contributor

@saul, I think the test failed because it expected an additional newline.

@cartermp
Copy link
Contributor

I'm not sure how to read that chart, but isn't this saying that the 4.3s is coming from String.Concat anyways? That is, how much of this is due to the fold vs. how much if it is due to the String.Concat call made as a result of the + operator on strings?

@saul
Copy link
Contributor Author

saul commented May 26, 2020

Yes all of the Seq.fold slowness is due to String.Concat (the BCL method). I replaced it with String.concat (the F# wrapper that passes to String.Join) as that uses StringBuilder under the hood. Sorry if my description was confusing!

@cartermp
Copy link
Contributor

Thanks, looks good. I did a quick benchmark and it likely corroborates your own findings 🙂

// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp

open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running

[<MemoryDiagnoser>]
type ConcatBench() =

    let strings = [ for x in 0..100 -> sprintf "--commandlinearg:%d" x ]

    [<Benchmark(Baseline=true)>]
    member _.SeqFold() =
        strings
        |> Seq.fold (fun acc f -> acc + f + Environment.NewLine) ""
        |> ignore

    [<Benchmark>]
    member _.FSharpCoreStringConcat() =
        strings
        |> String.concat Environment.NewLine
        |> ignore


[<EntryPoint>]
let main argv =
    let summary = BenchmarkRunner.Run<ConcatBench>()
    printfn "%A" summary
    0 // return an integer exit code

.NET Framework:

Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
SeqFold 17.498 μs 0.3491 μs 0.9136 μs 1.00 51.9714 - - 213.04 KB
FSharpCoreStringConcat 2.837 μs 0.0562 μs 0.1161 μs 0.16 3.1357 - - 12.86 KB

On .NET 5:

Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
SeqFold 18.209 μs 0.3946 μs 1.1195 μs 1.00 51.8494 - - 211.91 KB
FSharpCoreStringConcat 2.205 μs 0.0381 μs 0.0423 μs 0.12 1.0223 - - 4.19 KB

@cartermp cartermp merged commit 79cdd6b into dotnet:master May 26, 2020
@saul
Copy link
Contributor Author

saul commented May 26, 2020

Really appreciate the quick turnaround, thanks both :)

nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Feb 23, 2021
* Use `String.concat` to concatenate command line arguments

* Fix newline at end of cmd

Co-authored-by: Kevin Ransom (msft) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants