dotnet / dotnet/runtime

[API Proposal]: Array GetValue SetValue ROS indices

Open
#125,325 7 comments 0 reactions 0 assignees View on GitHub
api-approved area-System.Runtime
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Background and motivation

This would help us to use pooled arrays or fixed size stackallocs, which may not have equal length to the array's rank.

Internally GetFlattenedIndex already uses ROS, so the work would purely be a public api addition.

### API Proposal

```csharp
namespace System;

public class Array
{
public object? GetValue(params ReadOnlySpan indices);
public void SetValue(object? value, params ReadOnlySpan indices);
}
```

### API Usage

```csharp
Array values = ...
var indicesArray = ArrayPool.Shared.Rent(dimensions);
var indices = indicesArray.AsSpan(0, dimensions);
indices.Clear();
do
{
var value = values.GetValue(indices);
...
} (/*increment indices*/);
ArrayPool.Shared.Return(indicesArray);
```

Note: rent follows return in the same method here. In Npgsql we do have uses across methods (some are async as well), where stackalloc cannot be used.

### Alternative Designs

_No response_

### Risks

_No response_

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.