Skip to content
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

Use MaybeUninit in place of 0-initialized u8 arrays #146

Open
apoelstra opened this issue Jan 31, 2025 · 4 comments
Open

Use MaybeUninit in place of 0-initialized u8 arrays #146

apoelstra opened this issue Jan 31, 2025 · 4 comments

Comments

@apoelstra
Copy link
Member

Originally posted by @Kixunil (in a particular case) in #123 (comment)

There may be other cases -- typically with code Tobin or I touched we were not looking for unsafe optimizations.

@Kixunil
Copy link
Collaborator

Kixunil commented Jan 31, 2025

I often opted for ArrayVec to avoid unsafe but if it's shown in benchmarks to cause problems then I'm fine with MaybeUninit. Still, it's really weird that it happens at all.

@apoelstra
Copy link
Member Author

I suspect the perf hit comes from ArrayVec being generic being generic on T. I think if there were a u8-specific one (or maybe just T: Copy) (or even T: !Drop?) it would be faster.

It may also be that ArrayVec does length accounting which we don't need -- we always have fixed sizes that we overwrite completely (and, I think, have no unwinding branches between creating the object and overwriting it).

I dunno. Everything I just said, I'd have expected the compiler to take care of when using ArrayVec<u8>.

@apoelstra
Copy link
Member Author

Oh -- another thing we should consider is using core::array::from_fn! This is safe, allowed by MSRV since 1.63, and should encapsulate exactly the logic we want.

@Kixunil
Copy link
Collaborator

Kixunil commented Jan 31, 2025

Well, I'd expect post-mono/inlining optimizations to see that the lengths are the same and delete those branches/increments. I don't believe it has anything to do with it being generic.

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

No branches or pull requests

2 participants