Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Proposal: ValueStack<T> and other stack-allocated collection types #61456

Open
alrz opened this issue Nov 11, 2021 · 8 comments
Open

API Proposal: ValueStack<T> and other stack-allocated collection types #61456

alrz opened this issue Nov 11, 2021 · 8 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections wishlist Issue we would like to prioritize, but we can't commit we will get to it yet
Milestone

Comments

@alrz
Copy link
Member

alrz commented Nov 11, 2021

Background and motivation

Fixed-size stack-allocated collection types possibly backed by the given Span<T> for local use.

API Proposal

namespace System.Collections.Generic
{
    public ref struct ValueStack<T>
    {
        public ValueStack(Span<T> storage); // implies a fixed size
        public void Push(T value);
        public T Pop();
        public T Peek();
        // other applicable members from Stack<T>
    }
}

API Usage

var stack = new ValueStack<int>(stackalloc int[10]);
stack.Push(42);

Variants

Other variants such as ValueQueue<T> or ValueDeque<T> are most likely desirable to add.

One that .NET is currently lacking is a "circular-buffer" type which make sense to have a span-backed implementation for, due to the inherent fixed-size.

Risks

No response

@alrz alrz added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 11, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Collections untriaged New issue has not been triaged by the area owner labels Nov 11, 2021
@ghost
Copy link

ghost commented Nov 11, 2021

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Fixed-size stack-allocated collection types possibly backed by the given Span<T> for local use.

API Proposal

namespace System.Collections.Generic
{
    public ref struct ValueStack<T>
    {
        public void ValueStack(Span<T> storage); // implies a fixed size
        public void Push(T value);
        public T Pop();
        public T Peek();
        // other applicable members from Stack<T>
    }
}

API Usage

var stack = new ValueStack<int>(stackalloc int[10]);
stack.Push(42);

Alternative Designs

Not exactly "alternatives", but other variables such as ValueQueue or ValueDeque are most likely desirable to add.
One that .NET is currently lacking is a "circular-buffer" type which make sense to have a span-backed implementation for, due to the inherent fixed-size.

Risks

No response

Author: alrz
Assignees: -
Labels:

api-suggestion, area-System.Collections, untriaged

Milestone: -

@Sergio0694
Copy link
Contributor

This sounds like it would suffer from the same issue as ValueStringBuilder, that is, the fact that we don't have moveonly struct-s yet would make this type very error prone for users, especially in such a common namespace as System.Collections.Generic 🤔

@eiriktsarpalis
Copy link
Member

Related to #25587 and #30650. Like ValueStringBuilder we likely wouldn't be able to add this before we can get some form of sanity check from the compiler.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Nov 11, 2021
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Nov 11, 2021
@eiriktsarpalis eiriktsarpalis added the wishlist Issue we would like to prioritize, but we can't commit we will get to it yet label Nov 11, 2021
@alrz
Copy link
Member Author

alrz commented Nov 11, 2021

Is there a discussion/issue about the compiler's side of things you can link to?

@Sergio0694
Copy link
Contributor

It seems to mostly just be a discussion so far: dotnet/csharplang#859.

I'd imagine we should write a proper proposal and try to get some traction if we wanted to unblock this.
Of course, this specific feature is unfortunately pretty complex to implement, it's not a trivial addition.

@alrz
Copy link
Member Author

alrz commented Apr 21, 2022

This sounds like it would suffer from the same issue as ValueStringBuilder, that is, the fact that we don't have moveonly struct-s yet

So much happening with span safety https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md I wonder if any of that could address this issue? Really looking forward for a proposal on "move-only structs"

@Joe4evr
Copy link
Contributor

Joe4evr commented Apr 21, 2022

Would the work on ref fields be a mitigation for the (current) lack of move-only enforcement?

@tannergooding
Copy link
Member

Likely not and on top of that ValueX is typically a struct equivalent of X and is not significantly different in implementation (ValueStringBuilder is an internal exception).

My TerraFX project provides a ValueStack<T> (https://source.terrafx.dev/#TerraFX/Collections/ValueStack%25601.cs,b03392691a1e5a61) and UnmanagedValueStack<T> (https://source.terrafx.dev/#TerraFX/Collections/UnmanagedValueStack%25601.cs,27e7b6562f2908dd)

  • as well as corresponding ValueX and UnmanagedValueX equivalents for many of the various collection types

These types are largely meant for scenarios where you need something like a Stack or Queue or List but its only ever an internal implementation detail and so you'd like to remove an indirection/allocation for perf-oriented scenarios.

UnmanagedValueStack tracks an actual pointer (well UnmanagedArray<T>) and so with a slight tweak initial backing data could come from the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections wishlist Issue we would like to prioritize, but we can't commit we will get to it yet
Projects
None yet
Development

No branches or pull requests

5 participants