On Windows, setting ExactSpelling=true on the DllImport attribute prevents the runtime from looking for alternate function names (suffix based on CharSet), giving a slight performance benefit by avoiding this search.
There is a fair amount of nuance associated with the behaviour of ExactSpelling and under which scenarios there would actually be a performance benefit. In order to avoid noise, this rule can be scoped to check for a database of Win32 APIs that use the suffixes and provide a warning and fix for those cases where it matters.
// Flag ExactSpelling=false
[DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
private static extern bool SetEnvironmentVariableW(string name, string? value);
// OK - not in database of APIs
[DllImport("MyLibrary.dll", CharSet=CharSet.Unicode)]
private static extern void Foo();
Recommend:
[DllImport("kernel32.dll", CharSet=CharSet.Unicode, ExactSpelling = true)]
private static extern bool SetEnvironmentVariableW(string name, string? value);
Category: Performance
Default: Enabled
cc @terrajobst @stephentoub @AaronRobinsonMSFT @jkoritzinsky
On Windows, setting
ExactSpelling=trueon theDllImportattribute prevents the runtime from looking for alternate function names (suffix based onCharSet), giving a slight performance benefit by avoiding this search.There is a fair amount of nuance associated with the behaviour of
ExactSpellingand under which scenarios there would actually be a performance benefit. In order to avoid noise, this rule can be scoped to check for a database of Win32 APIs that use the suffixes and provide a warning and fix for those cases where it matters.Recommend:
Category: Performance
Default: Enabled
cc @terrajobst @stephentoub @AaronRobinsonMSFT @jkoritzinsky