-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Current situation
At the moment string interpolation is usually compiled into a String.Format() call like this:
string s = $"[{TimeStamp}] {Level} - {Logger} - {Message}";->
string s = string.Format("[{0}] {1} - {2} - {3}", new object[] {
this.TimeStamp,
this.Level,
this.Logger,
this.Message
});This leads to allocating an array of arguments (if there are more than 3 of them), boxing all primitive types and parsing the format string, which all seems like a waste of time, because the compiler already has all the information to emit the efficient code to build the string.
Proposition
The idea is to emit the code that manipulates StringBuilder directly instead of String.Format() call. For the example that I've mentioned it would look something like this:
// The same capacity estimation that String.Format() does can be used here,
// but with the value calculated during the compilation
StringBuilder _builder_s = StringBuilderCache.Acquire( 21 + 4 * 8 );
_builder_s.Append( "[" );
_builder_s.Append( this.TimeStamp );
_builder_s.Append( "] " );
_builder_s.Append( this.Level );
_builder_s.Append( " - " );
_builder_s.Append( this.Logger );
_builder_s.Append( " - " );
_builder_s.Append( this.Message );
string s = StringBuilderCache.GetStringAndRelease( _builder_s );This seems like a fairly straightforward optimization to implement. The only problem that I see at the first glance is that StringBuilderCache is internal; however, there's probably a way to work around that for the code generated by compiler that I'm simply not aware of.