[Proposal]: Explicit Field Layout Improvements #4652
Replies: 7 comments 1 reply
-
I'm not sure this matters to the language at all? Perhaps this should be an issue for dotnet/runtime |
Beta Was this translation helpful? Give feedback.
-
Hm, you may be right. If so, can someone move this issue? |
Beta Was this translation helpful? Give feedback.
-
It would need support from both the runtime and the language. [StructLayout(LayoutKind.Explicit)]
public struct Foo {
[FieldOffset(4)]
public int x;
}
|
Beta Was this translation helpful? Give feedback.
-
Why not adding a constant |
Beta Was this translation helpful? Give feedback.
-
Because these things are not compile-time constants. |
Beta Was this translation helpful? Give feedback.
-
It is a compile-time constant. But I think we can't express it in IL. |
Beta Was this translation helpful? Give feedback.
-
ℹ We ask in the issue template that you start new proposals as discussions unless a team member has invited you to create an issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Explicit Field Layout Improvements
Summary
Expand explicit field layouts to allow native word-size offsets and automatic offsets for unknown field size.
Motivation
Currently, because
FieldOffsetAttribute
only allows you to specify the offset in bytes, if you have more than 1 reference field, you have to choose between only supporting 32-bit runtime by offsetting reference fields by multiples of 4 bytes, always offset reference fields by multiples of 8 bytes, wasting space in 32-bit runtimes, or don't use explicit layout.The C# 9.0 feature for native sized integers
nint
/nuint
further creates this need for explicit layout improvements to be as efficient as possible.Additionally, explicit layouts are currently impossible with generic fields where the generics can be structs of unknown size (unless there's only 1 and it's placed after all other fields).
Detailed design
For offsets by native word-size, a new
FieldOffsetAttribute
constructor will be needed:public FieldOffsetAttribute(int offsetWords, int offsetBytes)
where the actual offset is determined by the runtime by calculatingIntPtr.Size() * offsetWords + offsetBytes
.sizeof(M)
should be 16 in 32-bit runtime and 24 in 64-bit runtime.--
For auto layout fields, a new
FieldOffsetAttribute
constructor could be made that accepts a new enum containing only a single option:public FieldOffsetAttribute(OffsetType offsetType)
. The runtime would decide how to offset these fields based on the type's size.sizeof(M<byte, nuint>)
should be 12 in 32-bit runtime and 24 in 64-bit runtime.sizeof(M<object, short>)
should be 16 in 32-bit runtime and 24 in 64-bit runtime.sizeof(M<nint, long>)
should be 20 in 32-bit runtime and 32 in 64-bit runtime.Drawbacks
None.
Alternatives
None.
Unresolved questions
Should we use
FieldOffset(OffsetType.Auto)
or a new attribute likeFieldOffsetAuto
?Design meetings
Beta Was this translation helpful? Give feedback.
All reactions