Skip to content

Conversation

udaken
Copy link
Contributor

@udaken udaken commented Aug 1, 2020

StringBuilder.AppendFormat expands to a large code size.

Duplicate code is generated if the same type argument is used.

Large codes are probably not good for JITs.

Performance can be improved by reducing the code size. It can be up to 20% faster.
(Depends on the type arguments used and the number of type arguments.)

Please try running BenchmarkVsReleasedVersion project in your environment.

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
AMD Ryzen 7 3700X, 1 CPU, 16 logical and 8 physical cores
.NET Core SDK=3.1.302
  [Host]   : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
  ShortRun : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT

Job=ShortRun  IterationCount=1  LaunchCount=1  
WarmupCount=1  

MaximumArgsFormatBenchmark

Method Mean Error Ratio Gen 0 Gen 1 Gen 2 Allocated Code Size
Format_ 1,494.6 ns NA 1.00 0.0477 - - 400 B 13720 B
FormatN 1,232.2 ns NA 0.82 0.0477 - - 400 B 4564 B
CreatePreparedFormat_ 1,435.9 ns NA 1.00 0.6256 0.0095 - 5240 B 2796 B
CreatePreparedFormatN 1,413.7 ns NA 0.98 0.6256 0.0095 - 5240 B 2796 B
Utf16PreparedFormat_ 825.7 ns NA 1.00 0.0477 - - 400 B 15688 B
Utf16PreparedFormatN 830.9 ns NA 1.01 0.0467 - - 392 B 15791 B
Utf8PreparedFormat_ 908.3 ns NA 1.00 0.0477 - - 400 B 16485 B
Utf8PreparedFormatN 900.5 ns NA 0.99 0.0477 - - 400 B 16589 B
Utf16StringBuilderAppendFormat_ 1,479.1 ns NA 1.00 - - - - 17875 B
Utf16StringBuilderAppendFormatN 1,196.1 ns NA 0.81 - - - - 8681 B
Utf8StringBuilderAppendFormat_ 1,822.2 ns NA 1.00 - - - - 19491 B
Utf8StringBuilderAppendFormatN 1,430.5 ns NA 0.79 - - - - 9241 B

@udaken
Copy link
Contributor Author

udaken commented Aug 1, 2020

This branch has already merged #21 and #24 .

@neuecc neuecc merged commit ed2a994 into Cysharp:master Aug 3, 2020
@udaken udaken deleted the Improved-performance-of-StringBuilder.AppendFormat branch August 3, 2020 15:08
ThangwLee pushed a commit to WolffunGame/ZString that referenced this pull request May 26, 2023
…ingBuilder.AppendFormat

Improved performance of StringBuilder.AppendFormat
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.

2 participants