CommunityToolkit / CommunityToolkit/dotnet

Add an overload to StringPool.GetOrAdd method which accepts an interpolated string

Open
#756 6 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

It would be really nice if there is an overload for the StringPool.GetOrAdd method which accepts a custom InterpolatedStringHandler. This InterpolatedStringHandler would create a ReadOnlySpanand use this to get the string from the pool without any string allocation (if available in the pool).

### API breakdown

```csharp
namespace CommunityToolkit.HighPerformance.Buffers;

class StringPool {

public string GetOrAdd(CustomInterpolatedStringHandler value);
}
```

### Usage example

```csharp
using CommunityToolkit.HighPerformance.Buffers;

for (int i = 0; i < 100; i++) {
var array = Foo();
// some code using the array;
}

static Employee[] Foo() {
var result = new Employee[100];
for (int i = 0; i < result.Length; i++) {
string name = StringPool.Shared.GetOrAdd($"Jim {i}"); // this would use the new overload

result[i] = new Employee(name, i);
}
return result;
}

record class Employee(string Name, decimal Salary);
```

### Breaking change?

No

### Alternatives

-

### Additional context

_No response_

### Help us help you

Yes, but only if others can assist

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.