Skip to content

Remove allocation for ZSTs - #92

Open
Baptistemontan wants to merge 4 commits into
mozilla:mainfrom
Baptistemontan:no_alloc_zst
Open

Remove allocation for ZSTs#92
Baptistemontan wants to merge 4 commits into
mozilla:mainfrom
Baptistemontan:no_alloc_zst

Conversation

@Baptistemontan

Copy link
Copy Markdown

Store the length as a NonZero<usize> inside the NonNull<Header>, the pointer is actually never read for ZSTs, when one is needed just create a dangling one.
The only problem is that now the length is off by one (as it can't be 0), this makes the maximum capacity usize::MAX - 1 instead, this might be a breaking change ?
Did'nt enabled those optimizations for feature = "gecko-ffi", as it can't be turned into nsTArray, tho from my understanding ZSTs are undefined in C++ so it would be UB to turn a ThinVec of ZSTs into a nsTArray anyway, but I prefer to get confirmation first

@emilio emilio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Curious, can you elaborate on what's the rationale for this? It seems somewhat complicated and having an array of ZSTs is already pretty edgecasey.

In general, I'd rather keep the differences between the gecko-ffi path and the other as small as possible.

@Baptistemontan

Baptistemontan commented Sep 1, 2026

Copy link
Copy Markdown
Author

Deliberately constructing a Vec of ZSTs is very niche, but it's actually pretty common when generics comes into play, deeply nested in data structures. This lib might be a bridge for Gecko but it's used a lot outside of that space by other people, and removing allocations for ZSTs is basically free.
Also, the crate doc says

ThinVec currently doesn’t bother to not-allocate for Zero Sized Types (e.g. ThinVec<()>), but it could be done if someone cared enough to implement it.

Well I'm that someone lol

And for keeping the same paths, it could be enabled on both since ZSTs should'nt be passed to C++ anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants