Description
In my teams code base we are occasionally appending long segments to a stringbuilder and line splitting them via +.
I noticed that when concatting an interpolated string and non-interpolated string as the input to Append that it falls back to String.Concat. This can mean that an small change to the string builder append input can result losing the performance of AppendInterpolatedStringHandler.
This uses AppendInterpolatedStringHandler:
var sb = new StringBuilder();
sb.Append($"Key {key} = " +
$"{value}");
This falls back to string.Concat
var sb = new StringBuilder();
sb.Append($"Key {key} = " +
value});
Configuration
.Net 6.0.2, windows 11, x64
Regression?
No
Data
https://sharplab.io/#v2:EYLgtghglgdgPgAQEwEYCwAoBAGABAlAOgBUBTADwBcBuTTBAZnyVwGFcBvTXH/JhACy4AsgAoCeANakAngBpcAZUoAnWAHNcANwgAbAK6kAlJ269zOlbgDOwXAF5cMUgHclqjQCF9UXQBNSFVEjWgxzc1tCAEEABxjSGD9RABIAIgBpWU5pGQBfB1xU3ABqXDNwivC0jh0DUlzUkPLcXObGfCFhJHEUKVkFZTUYTVrDEy4wyssbO0dnN0GvH39A4NDKmei4hKS0zJls2XzHItLmjYrR43XeVoxcoA==
Analysis
The following three snippets all compile down to the same code so there must be something in roslyn detecting that the full string is interpolated and using AppendInterpolatedStringHandler in that case.
https://sharplab.io/#v2:EYLgtghglgdgPgAQEwEYCwAoBAGABAlAOgBUBTADwBcBuTTBAZnyVwGFcBvTXH/JhACy4AsgwAUBPAGtSATwA0uAMqUATrADmuAG4QANgFdSASk7deF3atwBnNbgC8uACQAiANJzOM2QF9HuK64ANS45hYRkS6uHLqGpL6utBgWvuF8+ELCAhIo0nKKKuowWnFGplwpUVa29k5unrLecv5OQaHpUdX6Rsmp6YyZIgCsAJK5+QrKapo6PSZmVZE1dtb1Hl4cPq2cZQlJ6WkYvkA==
Description
In my teams code base we are occasionally appending long segments to a stringbuilder and line splitting them via +.
I noticed that when concatting an interpolated string and non-interpolated string as the input to Append that it falls back to String.Concat. This can mean that an small change to the string builder append input can result losing the performance of AppendInterpolatedStringHandler.
This uses AppendInterpolatedStringHandler:
This falls back to string.Concat
Configuration
.Net 6.0.2, windows 11, x64
Regression?
No
Data
https://sharplab.io/#v2:EYLgtghglgdgPgAQEwEYCwAoBAGABAlAOgBUBTADwBcBuTTBAZnyVwGFcBvTXH/JhACy4AsgAoCeANakAngBpcAZUoAnWAHNcANwgAbAK6kAlJ269zOlbgDOwXAF5cMUgHclqjQCF9UXQBNSFVEjWgxzc1tCAEEABxjSGD9RABIAIgBpWU5pGQBfB1xU3ABqXDNwivC0jh0DUlzUkPLcXObGfCFhJHEUKVkFZTUYTVrDEy4wyssbO0dnN0GvH39A4NDKmei4hKS0zJls2XzHItLmjYrR43XeVoxcoA==
Analysis
The following three snippets all compile down to the same code so there must be something in roslyn detecting that the full string is interpolated and using AppendInterpolatedStringHandler in that case.
https://sharplab.io/#v2:EYLgtghglgdgPgAQEwEYCwAoBAGABAlAOgBUBTADwBcBuTTBAZnyVwGFcBvTXH/JhACy4AsgwAUBPAGtSATwA0uAMqUATrADmuAG4QANgFdSASk7deF3atwBnNbgC8uACQAiANJzOM2QF9HuK64ANS45hYRkS6uHLqGpL6utBgWvuF8+ELCAhIo0nKKKuowWnFGplwpUVa29k5unrLecv5OQaHpUdX6Rsmp6YyZIgCsAJK5+QrKapo6PSZmVZE1dtb1Hl4cPq2cZQlJ6WkYvkA==