Skip to content

Debug Diagnostics always allocates StringBuilder with new optimized String Interpolation #57526

Closed

Description

@snakefoot

When using the old string.Format then it would make use of internal StringBuilder-pool.

Now it always allocates a new StringBuilder, and throws it away:

_stringBuilderHandler = new StringBuilder.AppendInterpolatedStringHandler(literalLength, formattedCount, new StringBuilder(DefaultInterpolatedStringHandler.GetDefaultLength(literalLength, formattedCount)));

Activity

  1. ghost added
    untriaged
    on Aug 16, 2021
  2. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    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?

  3. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    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 ?

  4. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    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).

  5. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    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)

  6. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    becomes a knife in your back (when the condition is true)

    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.

  7. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    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).

  8. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    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).

    Huh? I don't understand your question / comment.

  9. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    Huh? I don't understand your question / comment.

    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 ?

  10. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    Pool StringBuilders, or implement appends yourself using whatever mechanism you want, or create your handler as a ref struct, or...

  11. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    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).

  12. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    implementing own StringBuilder-pool is only way forward

    It is not the only way.

    But thank you for the feedback.

  13. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    It is not the only way.

    So how to I get a the formatted-string using a custom-handler without allocating the StringBuilder, and not doing carbon-copy of DefaultInterpolatedStringHandler?

  14. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    Make your handler a ref struct.

  15. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    Why did you not decide not to make AssertInterpolatedStringHandler a ref struct and re-use DefaultInterpolatedStringHandler ?

  16. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    Based on the stated belief that allocations in those situations do not matter.

  17. snakefoot commented on Aug 16, 2021

    @snakefoot
    ContributorAuthor

    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.

  18. stephentoub commented on Aug 16, 2021

    @stephentoub
    Member

    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.

  19. ghost added
    in-pr
    on Aug 17, 2021
  20. ghost removed
    in-pr
    on Aug 17, 2021
  21. ghost locked as resolved and limited conversation to collaborators on Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Diagnosticsuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions