diff options
| author | bors <bors@rust-lang.org> | 2016-10-26 03:47:55 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-10-26 03:47:55 -0700 |
| commit | a6b3b01b5f7f5a9d7d340dacf7dbf72be29e2c07 (patch) | |
| tree | f4a65ba88f6c9fe3c352be38c78790f58579ad1f /src/librustc_data_structures/array_vec.rs | |
| parent | 586a9883130bb7f8a1e8ea183b5019a56fb49426 (diff) | |
| parent | 989eba79a33187de03d8cdadb4994bb6b52200b0 (diff) | |
| download | rust-a6b3b01b5f7f5a9d7d340dacf7dbf72be29e2c07.tar.gz rust-a6b3b01b5f7f5a9d7d340dacf7dbf72be29e2c07.zip | |
Auto merge of #37270 - Mark-Simulacrum:smallvec-optimized-arenas, r=eddyb
Add ArrayVec and AccumulateVec to reduce heap allocations during interning of slices Updates `mk_tup`, `mk_type_list`, and `mk_substs` to allow interning directly from iterators. The previous PR, #37220, changed some of the calls to pass a borrowed slice from `Vec` instead of directly passing the iterator, and these changes further optimize that to avoid the allocation entirely. This change yields 50% less malloc calls in [some cases](https://pastebin.mozilla.org/8921686). It also yields decent, though not amazing, performance improvements: ``` futures-rs-test 4.091s vs 4.021s --> 1.017x faster (variance: 1.004x, 1.004x) helloworld 0.219s vs 0.220s --> 0.993x faster (variance: 1.010x, 1.018x) html5ever-2016- 3.805s vs 3.736s --> 1.018x faster (variance: 1.003x, 1.009x) hyper.0.5.0 4.609s vs 4.571s --> 1.008x faster (variance: 1.015x, 1.017x) inflate-0.1.0 3.864s vs 3.883s --> 0.995x faster (variance: 1.232x, 1.005x) issue-32062-equ 0.309s vs 0.299s --> 1.033x faster (variance: 1.014x, 1.003x) issue-32278-big 1.614s vs 1.594s --> 1.013x faster (variance: 1.007x, 1.004x) jld-day15-parse 1.390s vs 1.326s --> 1.049x faster (variance: 1.006x, 1.009x) piston-image-0. 10.930s vs 10.675s --> 1.024x faster (variance: 1.006x, 1.010x) reddit-stress 2.302s vs 2.261s --> 1.019x faster (variance: 1.010x, 1.026x) regex.0.1.30 2.250s vs 2.240s --> 1.005x faster (variance: 1.087x, 1.011x) rust-encoding-0 1.895s vs 1.887s --> 1.005x faster (variance: 1.005x, 1.018x) syntex-0.42.2 29.045s vs 28.663s --> 1.013x faster (variance: 1.004x, 1.006x) syntex-0.42.2-i 13.925s vs 13.868s --> 1.004x faster (variance: 1.022x, 1.007x) ``` We implement a small-size optimized vector, intended to be used primarily for collection of presumed to be short iterators. This vector cannot be "upsized/reallocated" into a heap-allocated vector, since that would require (slow) branching logic, but during the initial collection from an iterator heap-allocation is possible. We make the new `AccumulateVec` and `ArrayVec` generic over implementors of the `Array` trait, of which there is currently one, `[T; 8]`. In the future, this is likely to expand to other values of N. Huge thanks to @nnethercote for collecting the performance and other statistics mentioned above.
Diffstat (limited to 'src/librustc_data_structures/array_vec.rs')
| -rw-r--r-- | src/librustc_data_structures/array_vec.rs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/librustc_data_structures/array_vec.rs b/src/librustc_data_structures/array_vec.rs new file mode 100644 index 00000000000..f87426cee59 --- /dev/null +++ b/src/librustc_data_structures/array_vec.rs @@ -0,0 +1,106 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! A stack-allocated vector, allowing storage of N elements on the stack. +//! +//! Currently, only the N = 8 case is supported (due to Array only being impl-ed for [T; 8]). + +use std::marker::Unsize; +use std::iter::Extend; +use std::ptr::drop_in_place; +use std::ops::{Deref, DerefMut}; +use std::slice; +use std::fmt; + +pub unsafe trait Array { + type Element; + type PartialStorage: Default + Unsize<[ManuallyDrop<Self::Element>]>; + const LEN: usize; +} + +unsafe impl<T> Array for [T; 8] { + type Element = T; + type PartialStorage = [ManuallyDrop<T>; 8]; + const LEN: usize = 8; +} + +pub struct ArrayVec<A: Array> { + count: usize, + values: A::PartialStorage +} + +impl<A: Array> ArrayVec<A> { + pub fn new() -> Self { + ArrayVec { + count: 0, + values: Default::default(), + } + } +} + +impl<A> fmt::Debug for ArrayVec<A> + where A: Array, + A::Element: fmt::Debug { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self[..].fmt(f) + } +} + +impl<A: Array> Deref for ArrayVec<A> { + type Target = [A::Element]; + fn deref(&self) -> &Self::Target { + unsafe { + slice::from_raw_parts(&self.values as *const _ as *const A::Element, self.count) + } + } +} + +impl<A: Array> DerefMut for ArrayVec<A> { + fn deref_mut(&mut self) -> &mut [A::Element] { + unsafe { + slice::from_raw_parts_mut(&mut self.values as *mut _ as *mut A::Element, self.count) + } + } +} + +impl<A: Array> Drop for ArrayVec<A> { + fn drop(&mut self) { + unsafe { + drop_in_place(&mut self[..]) + } + } +} + +impl<A: Array> Extend<A::Element> for ArrayVec<A> { + fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=A::Element> { + for el in iter { + unsafe { + let arr = &mut self.values as &mut [ManuallyDrop<_>]; + arr[self.count].value = el; + } + self.count += 1; + } + } +} + +// FIXME: This should use repr(transparent) from rust-lang/rfcs#1758. +#[allow(unions_with_drop_fields)] +pub union ManuallyDrop<T> { + value: T, + #[allow(dead_code)] + empty: (), +} + +impl<T> Default for ManuallyDrop<T> { + fn default() -> Self { + ManuallyDrop { empty: () } + } +} + |
