Avoid StringBuilder parameters for P/Invokes (original) (raw)

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