-
Notifications
You must be signed in to change notification settings - Fork 150
[WIP] Use a union to reduce the size of SmallVec #92
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
Hmm there seems to be a regression in the benchmark results. I will look into this.
|
unsafe fn heap(&self) -> (*mut A::Item, usize) { | ||
match *self { | ||
SmallVecData::Heap(data) => data, | ||
_ => unreachable!(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug_unreachable may be worth in these.
☔ The latest upstream changes (presumably #93) made this pull request unmergeable. Please resolve the merge conflicts. |
You might want to recommit this without rustfmt. Ideally, the rustfmt would be a seperate PR. Also, is the change from an |
I'm going to close this since I haven't had time to work on it lately. |
Use a union to reduce the size of SmallVec [v2] Building on top of #92 by @Amanieu I introduced `triple()` and `triple_mut()` which removed almost all of the runtime overhead. Performance is very comparable. ``` name master:: ns/iter union:: ns/iter diff ns/iter diff % speedup bench_extend 45 47 2 4.44% x 0.96 bench_extend_from_slice 45 43 -2 -4.44% x 1.05 bench_from_slice 45 44 -1 -2.22% x 1.02 bench_insert 615 622 7 1.14% x 0.99 bench_insert_from_slice 101 99 -2 -1.98% x 1.02 bench_insert_many 309 266 -43 -13.92% x 1.16 bench_macro_from_elem 41 37 -4 -9.76% x 1.11 bench_macro_from_list 40 42 2 5.00% x 0.95 bench_push 381 370 -11 -2.89% x 1.03 bench_pushpop 404 420 16 3.96% x 0.96 bench_remove 458 436 -22 -4.80% x 1.05 ``` <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/94) <!-- Reviewable:end -->
Uses a union to eliminate the space used by the
enum
determinant. The new layout looks like this:The
capacity
field is used to determine which of the two union variants is active:capacity <= A::size()
then the inline variant is used andcapacity
holds the current length of the vector (number of elements actually in use).capacity > A::size()
then the heap variant is used andcapacity
holds the size of the memory allocation.Since unions with drop are still currently unstable, this is kept behind a "union" cargo feature, which falls back to an implementation using an enum if disabled.
This change is