Skip to content

[API Proposal]: Array GetValue SetValue ROS indices #125325

@NinoFloris

Description

@NinoFloris

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

namespace System;

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

API Usage

Array values = ...
var indicesArray = ArrayPool<int>.Shared.Rent(dimensions);
var indices = indicesArray.AsSpan(0, dimensions);
indices.Clear();
do
{
    var value = values.GetValue(indices);
    ...
} (/*increment indices*/);
ArrayPool<int>.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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions