Skip to content

Avoid StringBuilder parameters for P/Invokes #35693

Closed

Description

@elinor-fung

Marshalling for StringBuilder always creates a native buffer copy and can thus be very inefficient. Consider using a char buffer from ArrayPool instead.

// Flag StringBuilder parameter
[DllImport("MyLibrary")]
private static extern void Foo(StringBuilder buffer);

Category: Performance
Default: Disabled

Since each instance of this violation would require user intervention to determine if it makes sense to address and how to address it, this would be off-by-default and up to the user to enable for code where perf is a concern.

cc @terrajobst @stephentoub @AaronRobinsonMSFT @jkoritzinsky

Activity

  1. added
    api-suggestion
    code-analyzer
    on May 1, 2020
  2. added
    untriaged
    on May 1, 2020
  3. stephentoub commented on May 1, 2020

    @stephentoub
    Member

    I'm no fan of using StringBuilder in P/Invokes, and previously did a sweep through dotnet/runtime to remove almost all of it, but we do still have a few cases left, in particular where StringBuilders are passed [In] and with CharSet.Unicode; in such situations, if you already have a StringBuilder, it's a reasonable thing to use in a P/Invoke. As such I'm a little concerned this could be fairly noisy, though I agree it's a valuable thing to warn about in general in perf-sensitive code bases (maybe an IdeSuggestion, maybe off by default and folks can turn it in if desired). For code that doesn't care as much about absolute throughput, this could just be annoying.

  4. GrabYourPitchforks commented on May 1, 2020

    @GrabYourPitchforks
    Member

    I get the spirit of the rule, but I'm concerned that adherence to this rule significantly increases the concept count over what the original code looked like. If this rule triggers over your code and you try to address it, then in addition to understanding how StringBuilder behaves across a marshaling boundary, you also need to understand:

    1. How the array pool works, including best practices for using it
    2. How to move data between StringBuilder and a rented array
    3. Null terminator behavior when marshaling strings

    And you'll need to account for these effects in your code manually.

  5. elinor-fung commented on May 1, 2020

    @elinor-fung
    MemberAuthor

    Yeah, I think it would fall under the IdeSuggestion level.

    There would definitely be some additional concepts tied to addressing a violation. I think it still makes sense to provide a suggestion about things like that, since it gives an immediate practical application for those concepts. Do you think documentation for a new rule pointing out and explaining (or linking to other resources) all those would not be enough help?

  6. stephentoub commented on May 1, 2020

    @stephentoub
    Member

    I think part of the problem here is we can't reasonably provide an auto-fix. So we'd be saying "Consider alternatives to StringBuilder", but without a) knowing whether that actually makes sense for how they're using it, and b) with potentially non-trivial re-work required for them to address it.

  7. GrabYourPitchforks commented on May 1, 2020

    @GrabYourPitchforks
    Member

    Do you think documentation for a new rule pointing out and explaining (or linking to other resources) all those would not be enough help?

    There is something already along these lines at https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#string-parameters. It mentions the downsides of using StringBuilder but doesn't demonstrate how to work around them in your own code. If there were samples there showing best practice, we could link to it.

  8. elinor-fung commented on May 1, 2020

    @elinor-fung
    MemberAuthor

    So we'd be saying "Consider alternatives to StringBuilder", but without a) knowing whether that actually makes sense for how they're using it, and b) with potentially non-trivial re-work required for them to address it.

    Perhaps mitigated if it were off by default, so people would have had to choose to be warned about this specific rule (or performance rules)?

  9. stephentoub commented on May 1, 2020

    @stephentoub
    Member

    Yup.

  10. removed
    untriaged
    on Jun 8, 2020
  11. added this to the 5.0 milestone on Jun 8, 2020
  12. added and removed
    api-suggestion
    on Jun 24, 2020
  13. added
    api-ready-for-review
    and removed on Jul 6, 2020
  14. added
    api-approved
    and removed
    api-ready-for-review
    on Jul 16, 2020
  15. terrajobst commented on Jul 16, 2020

    @terrajobst
    Contributor

    Video

    Shipping this as a warning and off by default makes sense. Setting the severity as suggestion would likely mean that nobody will ever see this.

  16. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
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

    api-approvedarea-System.Runtime.InteropServicescode-analyzer

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions