You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Debug Diagnostics always allocates StringBuilder with new optimized String Interpolation #57526
That code is not for string.Format. That's the code for Debug.Assert, which is only compiled in for #define DEBUG, and specifically on the path where Debug.Assert is given a false condition, which means it's failing an assert and by default is about to crash the process. Why is a StringBuilder allocation there concerning?
Lets try again old-style-interpolated-strings would convert into string.Format, and string.Format would make use of StringBuilder-pool.
Also it is not only Debug.Assert that have received additional springles. The same also applies for Debug.WriteIf and Debug.WriteLineIf that hopefully doesn't crash the process.
Why not use DefaultInterpolatedStringHandler instead of StringBuilder.AppendInterpolatedStringHandler in Debug ?
Why not use DefaultInterpolatedStringHandler instead of StringBuilder.AppendInterpolatedStringHandler in Debug ?
Because DefaultInterpolatedStringHandler is a ref struct.
The same also applies for Debug.WriteIf and Debug.WriteLineIf that hopefully doesn't crash the process.
Yes, they will allocate a StringBuilder if #if DEBUG is set and the condition dictates they should write a message, which is typically a rare occurrence for which an extra StringBuilder won't matter. It does not seem like a good tradeoff to either a) duplicate a lot of code to avoid that or b) make this a ref struct and thereby limit how it can be used (e.g. awaits in format holes).
Just strange that it is promoted as a performance improvement with less-allocation, but then becomes a knife in your back (when the condition is true)
Anyway I just looked for some example code of how to use the new optimization, but I guess I have to continue looking (Yes it is a little annoying everyone have to carbon-copy DefaultInterpolatedStringHandler to make their own correct handler)
I think it makes good sense for Debug.Assert(). I was just curious why this decision was made for Debug.WriteIf (And now you have told that it is because DefaultInterpolatedStringHandler is impossible to reuse).
Now I just have to figure out why Microsoft have implemented an optimization, that only can be used for Logging just before crashing the process (And hard to reuse anywhere else without inferring a performance hit because of StringBuilder-allocations).
Now I just have to figure out why Microsoft have implemented an optimization, that only can be used for Logging just before crashing the process (And hard to reuse anywhere else without inferring a performance hit because of StringBuilder-allocations).
Lets say I have another place where I want to check a condition, before doing the string-interpolation (Just like Debug.WriteIf)
How would I do that, since I cannot use DefaultInterpolatedStringHandler ? And I don't want to use AppendInterpolatedStringHandler because of the StringBuilder-allocation-hit ?
Yep thank you for confirming that implementing own StringBuilder-pool is only way forward. Maybe update the The handler pattern with concrete example that uses AppendInterpolatedStringHandler with a note about optional StringBuilder-pool (To avoid higher allocation rate than old string-format).
And also explain that DefaultInterpolatedStringHandler cannot be wrapped in a custom-handler because it is very sensitive about its lifetime (ref struct).
So how to I get a the formatted-string using a custom-handler without allocating the StringBuilder, and not doing carbon-copy of DefaultInterpolatedStringHandler?
Thank you for the answers. Though I still don't understand the choice to opt-in for allocations, when it is just a matter of changing to ref struct to fix it. But I have what I need.
I still don't understand the choice to opt-in for allocations, when it is just a matter of changing to ref struct to fix it
If a handler is a ref struct, you can't do $"...{await Foo()}..." with awaits inside of formatting holes. In our experience, that's a very rare need, but technically it is a functional limitation. So you trade that off against other costs. If you believe that additional overhead isn't a big deal when passing true to Debug.WriteIf, because that's typically only done in debug builds when you're gathering additional information about something that's not working correctly, then it's better to accomodate even that rare need than to avoid costs that don't matter. If you believe that those costs do matter in these situations, then you make a different tradeoff, e.g. adding additional code to implement appending yourself, or pooling StringBuilders to use the existing logic there, and so on.
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
stephentoub commentedon Aug 16, 2021
That code is not for string.Format. That's the code for Debug.Assert, which is only compiled in for
#define DEBUG, and specifically on the path where Debug.Assert is given a false condition, which means it's failing an assert and by default is about to crash the process. Why is aStringBuilderallocation there concerning?snakefoot commentedon Aug 16, 2021
Lets try again old-style-interpolated-strings would convert into string.Format, and string.Format would make use of StringBuilder-pool.
Also it is not only
Debug.Assertthat have received additional springles. The same also applies forDebug.WriteIfandDebug.WriteLineIfthat hopefully doesn't crash the process.Why not use
DefaultInterpolatedStringHandlerinstead ofStringBuilder.AppendInterpolatedStringHandlerinDebug?stephentoub commentedon Aug 16, 2021
Because DefaultInterpolatedStringHandler is a ref struct.
Yes, they will allocate a StringBuilder if
#if DEBUGis set and the condition dictates they should write a message, which is typically a rare occurrence for which an extra StringBuilder won't matter. It does not seem like a good tradeoff to either a) duplicate a lot of code to avoid that or b) make this a ref struct and thereby limit how it can be used (e.g. awaits in format holes).snakefoot commentedon Aug 16, 2021
Just strange that it is promoted as a performance improvement with less-allocation, but then becomes a knife in your back (when the condition is true)
Anyway I just looked for some example code of how to use the new optimization, but I guess I have to continue looking (Yes it is a little annoying everyone have to carbon-copy
DefaultInterpolatedStringHandlerto make their own correct handler)stephentoub commentedon Aug 16, 2021
Can you please share concrete examples of where you're using Debug.WriteIf(true, $"...") on code paths where the extra StringBuilder matters?
If they exist, it's fixable with more code. It simply seems to me right now like the wrong tradeoff to add more code for this.
Thanks.
snakefoot commentedon Aug 16, 2021
I think it makes good sense for
Debug.Assert(). I was just curious why this decision was made forDebug.WriteIf(And now you have told that it is becauseDefaultInterpolatedStringHandleris impossible to reuse).Now I just have to figure out why Microsoft have implemented an optimization, that only can be used for Logging just before crashing the process (And hard to reuse anywhere else without inferring a performance hit because of StringBuilder-allocations).
stephentoub commentedon Aug 16, 2021
Huh? I don't understand your question / comment.
snakefoot commentedon Aug 16, 2021
Lets say I have another place where I want to check a condition, before doing the string-interpolation (Just like
Debug.WriteIf)How would I do that, since I cannot use
DefaultInterpolatedStringHandler? And I don't want to useAppendInterpolatedStringHandlerbecause of the StringBuilder-allocation-hit ?stephentoub commentedon Aug 16, 2021
Pool StringBuilders, or implement appends yourself using whatever mechanism you want, or create your handler as a ref struct, or...
snakefoot commentedon Aug 16, 2021
Yep thank you for confirming that implementing own StringBuilder-pool is only way forward. Maybe update the The handler pattern with concrete example that uses
AppendInterpolatedStringHandlerwith a note about optional StringBuilder-pool (To avoid higher allocation rate than old string-format).And also explain that
DefaultInterpolatedStringHandlercannot be wrapped in a custom-handler because it is very sensitive about its lifetime (ref struct).stephentoub commentedon Aug 16, 2021
It is not the only way.
But thank you for the feedback.
snakefoot commentedon Aug 16, 2021
So how to I get a the formatted-string using a custom-handler without allocating the StringBuilder, and not doing carbon-copy of
DefaultInterpolatedStringHandler?stephentoub commentedon Aug 16, 2021
Make your handler a ref struct.
snakefoot commentedon Aug 16, 2021
Why did you not decide not to make
AssertInterpolatedStringHandleraref structand re-useDefaultInterpolatedStringHandler?stephentoub commentedon Aug 16, 2021
Based on the stated belief that allocations in those situations do not matter.
snakefoot commentedon Aug 16, 2021
Thank you for the answers. Though I still don't understand the choice to opt-in for allocations, when it is just a matter of changing to
ref structto fix it. But I have what I need.stephentoub commentedon Aug 16, 2021
If a handler is a ref struct, you can't do
$"...{await Foo()}..."with awaits inside of formatting holes. In our experience, that's a very rare need, but technically it is a functional limitation. So you trade that off against other costs. If you believe that additional overhead isn't a big deal when passing true to Debug.WriteIf, because that's typically only done in debug builds when you're gathering additional information about something that's not working correctly, then it's better to accomodate even that rare need than to avoid costs that don't matter. If you believe that those costs do matter in these situations, then you make a different tradeoff, e.g. adding additional code to implement appending yourself, or pooling StringBuilders to use the existing logic there, and so on.