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

InlineArray design spec/doc #108044

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

InlineArray design spec/doc #108044

wants to merge 12 commits into from

Conversation

VSadov
Copy link
Member

@VSadov VSadov commented Sep 20, 2024

Fixes: #91012

  • Convert the proposal into a document and move to appropriate place.
  • Add some info about layout of the inline array instance (i.e. pointer to the entire struct and to the first element are equal,...)
  • Add a note on readonly element. This has no special semantics and as such unsupported, not recommended.
  • Add a note on the max size of an instance.

@VSadov VSadov added the documentation Documentation bug or enhancement, does not impact product or test code label Sep 20, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 20, 2024
@VSadov
Copy link
Member Author

VSadov commented Sep 20, 2024

CC: @mangod9 @jkotas @MichalStrehovsky @lambdageek - since you've had interest or contributed to this in the past

There were multiple proposals and ideas in the past asking for a no-indirection primitive for inline data. [Example1(inline strings)](https://github.com/dotnet/csharplang/issues/2099), [Example2(inline arrays)](https://github.com/dotnet/runtime/issues/12320), [Example3(generalized fixed-buffers)](https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md#safe-fixed-size-buffers)
Our preexisting offering in this area – [unsafe fixed-sized buffers](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code#fixed-size-buffers) has multiple constraints, in particular it works only with blittable value types and provides no overrun/type safety, which considerably limits its use.

*The InlineArrayAttribute is a building block to allow efficient, type-safe, overrun-safe indexable/sliceable inline data.*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InlineArrayAttribute alone does not address overrun-safety.

Related to #105586.

Copy link
Member Author

@VSadov VSadov Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By itself InlineArray provides only storage. Thus I call it a "building block", not "a type-safe, overrun-safe indexable data". It is not that.

Since the type does not know its length or how to index itself, the indexing and range checking is the responsibility of the higher level implementation (hand-made or compiler-generated) that wraps the storage.

In a way the C#-provided indexing is still safe as one cannot do arr[5] on an array with [InlineArray(length:3)], it is just enforced by the layer that is above IL/typesystem.

This is one tradeoff that we took when we did not go the InlineArray<T, Length> path.
That would have indexing and range checking at the runtime level - without wrappers/helpers.

I think "building block" is vague enough to hint at the purpose of the feature.
We do not need to prescribe here how one can actually build indexable things with this, but including a link to C# feature design/spec would be a good example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

including a link to C# feature design/spec would be a good example.

Added.

@jkotas
Copy link
Member

jkotas commented Sep 20, 2024

Include a link to the matching Roslyn proposal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation bug or enhancement, does not impact product or test code needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InlineArray documentation tracking
3 participants