Skip to content

Add hint::assert_unchecked(len <= CAP)#314

Open
barakugav wants to merge 2 commits into
bluss:masterfrom
barakugav:assume-len
Open

Add hint::assert_unchecked(len <= CAP)#314
barakugav wants to merge 2 commits into
bluss:masterfrom
barakugav:assume-len

Conversation

@barakugav

Copy link
Copy Markdown

First of all, love the library! use it all the time 😄

This PR add a std::hint::assert_unchecked(len <= CAP) call in ArrayVec::len().
We know this assumption, and telling the compiler about it let it generate better code and eliminate some bound checks.

It also include a small benchmark for ArrayVec::clone() that demonstrate the (massive) speed up.

# before
> cargo bench clone
running 2 tests
test clone_512 ... bench:         345 ns/iter (+/- 20) = 1484 MB/s
test clone_8   ... bench:           4 ns/iter (+/- 0) = 2000 MB/s

# after
> cargo bench clone
running 2 tests
test clone_512 ... bench:         205 ns/iter (+/- 15) = 2497 MB/s
test clone_8   ... bench:           2 ns/iter (+/- 0) = 4000 MB/s

Clone is my specific use case, but im sure other generated code is more optimized when the compiler knows this.

std::hint::assert_unchecked was stabilized in 1.81 (docs), so I added a few lines for detection in a build.rs, hope that acceptable.

Thanks for the support in advance ❤️

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.

1 participant