Skip to content

Avoiding "the cost of wrapping a struct" :) #37

@Timovzl

Description

@Timovzl

Hi Sergey,

I have some good news: I noticed that you can avoid the per-field padding (i.e. alignment on pointer boundaries) for classes.

All you have to do is set the [StructLayout(LayoutKind.Sequential)] attribute on the class.

Although the attribute's name implies that it would only work on structs, the docs confirm that it "lets you control the physical layout of the data fields of a class or structure in memory".

Interestingly, the attribute's optional Pack field seems to have no effect on classes whatsoever, at least in the scenarios I've explored.

It stands to reason that, alternatively, the per-field padding could also be avoided with LayoutKind.Explicit, although I have not tested this.

Perhaps you could add some of this information to the corresponding section in your readme.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions