-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid StringBuilder parameters for P/Invokes #35693
Comments
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. |
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
And you'll need to account for these effects in your code manually. |
Yeah, I think it would fall under the 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? |
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. |
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 |
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)? |
Yup. |
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. |
Marshalling for
StringBuilder
always creates a native buffer copy and can thus be very inefficient. Consider using achar
buffer fromArrayPool
instead.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
The text was updated successfully, but these errors were encountered: