Skip to content

Use String.concat to concatenate command line arguments - #9297

Merged
cartermp merged 2 commits into
dotnet:masterfrom
saul:string-concat-commandline-args
May 26, 2020
Merged

Use String.concat to concatenate command line arguments#9297
cartermp merged 2 commits into
dotnet:masterfrom
saul:string-concat-commandline-args

Conversation

@saul

@saulsaul commented May 26, 2020

Copy link
Copy Markdown
Contributor

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
saulforce-pushed the string-concat-commandline-args branch from d813884 to ac4a13bCompareMay 26, 2020 16:04
@KevinRansom

Copy link
Copy Markdown
Contributor

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

@cartermp

Copy link
Copy Markdown
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

saul commented May 26, 2020

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
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/fsharpopenSystemopenBenchmarkDotNet.AttributesopenBenchmarkDotNet.Running[<MemoryDiagnoser>]typeConcatBench()=letstrings=[for x in0..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>]letmain argv =letsummary= BenchmarkRunner.Run<ConcatBench>()
printfn "%A" summary
0// return an integer exit code

.NET Framework:

MethodMeanErrorStdDevRatioGen 0Gen 1Gen 2Allocated
SeqFold17.498 μs0.3491 μs0.9136 μs1.0051.9714--213.04 KB
FSharpCoreStringConcat2.837 μs0.0562 μs0.1161 μs0.163.1357--12.86 KB

On .NET 5:

MethodMeanErrorStdDevRatioGen 0Gen 1Gen 2Allocated
SeqFold18.209 μs0.3946 μs1.1195 μs1.0051.8494--211.91 KB
FSharpCoreStringConcat2.205 μs0.0381 μs0.0423 μs0.121.0223--4.19 KB

@cartermp
cartermp merged commit 79cdd6b into dotnet:masterMay 26, 2020
@saul

saul commented May 26, 2020

Copy link
Copy Markdown
ContributorAuthor

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) <codecutter.fsharp@hotmail.com>
Sign up for freeto 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

@saul@KevinRansom@cartermp