Packing non-pointer optionals
ziggit.dev·4d
📏Linear Types
Preview
Report Post

January 7, 2026, 5:29pm 1

This may have been discussed before, and if so feel free to point me to that discussion. I couldn’t find it though.

I’ve been playing with optionals for value types (i.e. not pointers), and I’m I little surprised that the implementation seems a bit basic. If I have a ?u32, that looks something like this (Not literally, but from a memory layout point of view):

const OptionalU32 = struct {
valid : bool,
value : u32,
};

The size of it is 8-bytes as it’s aligned to 4-bytes. This is understandable. For a single instance of this, there aren’t many options. However, If I have an array of 4 of them, that array is 32-bytes in size. It could apply the “structure of arrays” pattern and produce this (again, from a memory layout view):

cons...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help