2019-09-26 17:54:39 -05:00
|
|
|
use crate::stable_hasher::{StableHasher, HashStable};
|
2018-12-02 18:14:35 -06:00
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
/// A vector type optimized for cases where this size is usually 0 (cf. `SmallVector`).
|
2016-06-17 23:01:57 -05:00
|
|
|
/// The `Option<Box<..>>` wrapping allows us to represent a zero sized vector with `None`,
|
|
|
|
/// which uses only a single (null) pointer.
|
|
|
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub struct ThinVec<T>(Option<Box<Vec<T>>>);
|
|
|
|
|
|
|
|
impl<T> ThinVec<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ThinVec(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<Vec<T>> for ThinVec<T> {
|
|
|
|
fn from(vec: Vec<T>) -> Self {
|
|
|
|
if vec.is_empty() {
|
|
|
|
ThinVec(None)
|
|
|
|
} else {
|
|
|
|
ThinVec(Some(Box::new(vec)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Into<Vec<T>> for ThinVec<T> {
|
|
|
|
fn into(self) -> Vec<T> {
|
|
|
|
match self {
|
|
|
|
ThinVec(None) => Vec::new(),
|
|
|
|
ThinVec(Some(vec)) => *vec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ::std::ops::Deref for ThinVec<T> {
|
|
|
|
type Target = [T];
|
|
|
|
fn deref(&self) -> &[T] {
|
|
|
|
match *self {
|
|
|
|
ThinVec(None) => &[],
|
|
|
|
ThinVec(Some(ref vec)) => vec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-04 22:20:55 -06:00
|
|
|
impl<T> ::std::ops::DerefMut for ThinVec<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut [T] {
|
|
|
|
match *self {
|
|
|
|
ThinVec(None) => &mut [],
|
|
|
|
ThinVec(Some(ref mut vec)) => vec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:01:57 -05:00
|
|
|
impl<T> Extend<T> for ThinVec<T> {
|
|
|
|
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
|
|
|
match *self {
|
|
|
|
ThinVec(Some(ref mut vec)) => vec.extend(iter),
|
|
|
|
ThinVec(None) => *self = iter.into_iter().collect::<Vec<_>>().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 18:14:35 -06:00
|
|
|
|
|
|
|
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for ThinVec<T> {
|
2019-09-26 17:54:39 -05:00
|
|
|
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
|
2018-12-02 18:14:35 -06:00
|
|
|
(**self).hash_stable(hcx, hasher)
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 05:58:40 -05:00
|
|
|
|
|
|
|
impl<T> Default for ThinVec<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(None)
|
|
|
|
}
|
|
|
|
}
|