Skip to content

Take ReadOnlySpan<char> instead of string in friendly overloads #1543

@jnm2

Description

@jnm2

Strings can always be passed as ReadOnlySpan<char>, but if what you have is a span, that can't be passed as a string without allocating. For example, here is a scenario where a previous API call populated some spans:

var productCode = (stackalloc char[39]);
var componentCode = (stackalloc char[39]);
if (PInvoke.MsiGetShortcutTarget(shortcutPath, productCode, null, componentCode) != 0)
{
    componentPath = null;
    return false;
}

const int bufferSize = 1024;
var buffer = (stackalloc char[bufferSize]);
uint length = bufferSize;

var result = PInvoke.MsiGetComponentPath(productCode.ToString(), componentCode.ToString(), buffer, ref length);

In order to avoid those two ToString allocations, I would now have to introduce an unsafe block for the first time, and use three fixed statements to pin the three spans being passed to MsiGetComponentPath.

Alternatively, the friendly overloads could always take ReadOnlySpan<char> instead of string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussion neededWe need more feedback or responses from ABI experts

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions