Take ReadOnlySpan<char> instead of string in friendly overloads
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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