dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.62k stars 4.56k forks source link

Improve low level struct performance #46104

Open jaredpar opened 3 years ago

jaredpar commented 3 years ago

This is a "User Story" to track the proposed improvements to low level struct performance improvements.

C# Language Features

The feature proposal for both C# features is captured here

Related Runtime Requests:

Dotnet-GitSync-Bot commented 3 years ago

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.

benaadams commented 3 years ago

What about...?

Span<object> span = stackalloc object[5];

Which is sorta

struct Obj5
{
    object o0;
    object o1;
    object o2;
    object o3;
    object o4;
}

Obj5 objs = default;
Span<object> span = CreateSpan(ref objs.o0, 5);
jaredpar commented 3 years ago

The stackalloc primitive doesn't support reference types.

benaadams commented 3 years ago

localloc returning a byte* pointer is obv problematic for a reference type; however if it was limited to a Span<ref type> receiver and a fixed size rather than variable size then t could be translated to something closer to the safe fixed sized buffer proposal? e.g.

struct <Buffer>e__FixedBuffer_1024<T>
{
    private T _e0;
    private T _e1;
    // _e2 ... _e1023
    private T _e1024;

    public ref T this[int index] => ref (uint)index <= 1024u ?
                                         ref RefAdd<T>(ref _e0, index):
}

Not sure what the GC would make of referring only to an internal element of a stack struct (if it was sliced)

jkotas commented 3 years ago

I think https://github.com/dotnet/runtime/issues/25423 is the proposal we should follow to improve stackalloc.

mangod9 commented 3 years ago

@jaredpar this is unlikely for 6 due to statics in interfaces work. Ok to move out?

mangod9 commented 3 years ago

Moving this to future, since this is moved out of .net 6.

iSazonov commented 3 years ago

Fore reference https://github.com/dotnet/runtime/issues/38743