Skip to content

Proposal: Use padding to optimize optional type sizes #5162

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

Closed
iansimonson opened this issue Apr 25, 2020 · 2 comments
Closed

Proposal: Use padding to optimize optional type sizes #5162

iansimonson opened this issue Apr 25, 2020 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@iansimonson
Copy link
Contributor

Currently in Zig optional pointer types get optimized to a regular pointer size. Effectively @sizeOf(*T) == @sizeOf(?*T). This is not the case for non-pointer types.

From some testing it seems the optional version of a type is the size of the type plus an extra field. More preciselyassert(@sizeOf(?T) == @sizeOf(T) + @alignOf(T)); For primitive types this is probably the best we can do unless there's something like the Explicit Sentinel Optionals proposed by @SpexGuy in #3806

However, for structs this can have some drastic effects depending on the alignment.

Example 0: Normal alignment optional:

const S = struct {
    a: u8,
};

test "Alignment" {
    std.debug.assert(@sizeOf(S) == 1);
    std.debug.assert(@sizeOf(?S) == 2);
}

Example 1: Large alignment optional:

const S = struct {
    a: u8 align(64),
};

test "Alignment" {
    std.debug.assert(@sizeOf(S) == 64);
    std.debug.assert(@sizeOf(?S) == 128);
    std.debug.assert(@sizeOf(?S) == @sizeOf(S) + @alignOf(S));
}

I'm not sure how feasible it is, but given Zig doesn't have a stable ABI for its own structs it should be possible to optimize an optional type's layout and size to use the generated padding to host the extra optional field.

This would effectively bring the @sizeOf(?S) in example 2 down to 64 and shouldn't have any issues with e.g. var set_of_s: [100]S = undefined; as the type S would stay properly aligned.

Of course it won't always be possible to do this optimization, in which case the optional type would have to add the additional field as normal.

@LemonBoy
Copy link
Contributor

I'd suggest you to add your suggestions to #104.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label May 30, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone May 30, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@iansimonson
Copy link
Contributor Author

Given the direction for the accepted #104 this can be closed as duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants