microsoft / microsoft/CsWin32

Take ReadOnlySpan<char> instead of string in friendly overloads

Open
#1,543 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discussion needed
Dominant language
C#
Stars
2.5k
Forks
123
Avg merge
1d 3h
Merged PRs (30d)
9

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the friendly overloads for PInvoke.MsiGetComponentPath and reviewing how their current string parameters are generated. Determine the compatibility and marshaling implications of accepting ReadOnlySpan, then verify that span-based callers avoid the ToString allocations while existing string callers continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.