Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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]: stackalloc inside ref struct #6631

Closed
CodingMadness opened this issue Oct 28, 2022 · 7 comments
Closed

[API Proposal]: stackalloc inside ref struct #6631

CodingMadness opened this issue Oct 28, 2022 · 7 comments

Comments

@CodingMadness
Copy link

Background and motivation

You can do a lot of things with it when you can store arrays inside the restricts which are stack allocated and have no need to exist longer than they have to.

API Proposal

namespace System.Collections.Generic;

public ref struct CustomIterator<T> where T: unmanaged
{
      private Span<T> data = stackalloc T[<const size >]
}

API Usage

// use it inside the ref struct "CustomIterator()"
public bool MoveNext()
{
      for(int i = 0; i < data.Length; I++)
         data[I] = GetRnd_T();  //whatever here..
}

Alternative Designs

No response

Risks

Not AFAIK

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged label Oct 28, 2022
@ovska
Copy link

ovska commented Oct 29, 2022

Your example of a constant-sized stackalloc can already be achieved like this:

public ref struct CustomIterator<T> where T: unmanaged
{
    private Span<T> _data;

    public CustomIterator(Span<T> data)
    {
        _data = data;
    }
}
public static void Something()
{
    var iterator = new CustomIterator<byte>(stackalloc byte[128]);
}

@svick
Copy link
Contributor

svick commented Oct 29, 2022

This belongs to dotnet/csharplang, where this is effectively a part of #1502.

@CodingMadness
Copy link
Author

This is partially a solution but you dont always want to pass that stackalloc from the outside, cause you may need it for specific calculations and you know exactly the length at compile time inside the ref-struct. RIght now i have such an instance where i dont want outsiders to define what and how large the storage shall be and even then allow that thing outsiders to even utilize.

Its nice that you even have the chance to do that, but its really not an all case scenario solution.

I would think that, since we can have Span<byte> data = stackalloc byte[xy] and we already have ref-structs with ref fields and so, that it would not be far off to allow this type aswell be usable inside the ref struct itself.

@CodingMadness
Copy link
Author

CodingMadness commented Oct 30, 2022

Your example of a constant-sized stackalloc can already be achieved like this:

public ref struct CustomIterator<T> where T: unmanaged
{
    private Span<T> _data;

    public CustomIterator(Span<T> data)
    {
        _data = data;
    }
}
public static void Something()
{
    var iterator = new CustomIterator<byte>(stackalloc byte[128]);
}

Another flaw with your design is that you would allow potentially evrything to be passed, which can be dangerous. So this is prevented with my api-choice aswell.

PS: I shall remove the where T: unmanaged constraint because i need a 3-tuple insidethat enumerator; So i need a fixed-size pool of that, which is sadly not possible with the usual "fixed T[10]" cause it only can contain primitive datatypes.

@jeffschwMSFT jeffschwMSFT transferred this issue from dotnet/runtime Oct 30, 2022
@IS4Code
Copy link

IS4Code commented Nov 7, 2022

It would seem to me that a better solution would be to make this possible then:

public ref struct CustomIterator<T> where T: unmanaged
{
      private fixed T data[<const size >];
}

Plus to be able to treat data as Span<T> without unsafe (not sure if that is possible already).

@CodingMadness
Copy link
Author

It would seem to me that a better solution would be to make this possible then:

public ref struct CustomIterator<T> where T: unmanaged
{
      private fixed T data[<const size >];
}

Plus to be able to treat data as Span<T> without unsafe (not sure if that is possible already).

yea fixed does all that, except like u cant use ValueTuples with it

@dotnet dotnet locked and limited conversation to collaborators Nov 28, 2022
@333fred 333fred converted this issue into discussion #6753 Nov 28, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants