2019-12-22 22:42:04 +00:00
|
|
|
use crate::stable_hasher::{HashStable, StableHasher};
|
2018-12-03 00:14:35 +00:00
|
|
|
|
2021-04-03 02:56:18 +00:00
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
2021-09-05 04:08:34 +00:00
|
|
|
/// A vector type optimized for cases where this size is usually 0 (cf. `SmallVec`).
|
2016-06-18 04:01:57 +00:00
|
|
|
/// The `Option<Box<..>>` wrapping allows us to represent a zero sized vector with `None`,
|
|
|
|
/// which uses only a single (null) pointer.
|
2021-12-31 10:10:27 +00:00
|
|
|
#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq)]
|
2016-06-18 04:01:57 +00:00
|
|
|
pub struct ThinVec<T>(Option<Box<Vec<T>>>);
|
|
|
|
|
|
|
|
impl<T> ThinVec<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ThinVec(None)
|
|
|
|
}
|
2021-04-03 02:56:18 +00:00
|
|
|
|
|
|
|
pub fn iter(&self) -> std::slice::Iter<'_, T> {
|
|
|
|
self.into_iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
|
|
|
|
self.into_iter()
|
|
|
|
}
|
2021-12-31 10:10:27 +00:00
|
|
|
|
|
|
|
pub fn push(&mut self, item: T) {
|
|
|
|
match *self {
|
|
|
|
ThinVec(Some(ref mut vec)) => vec.push(item),
|
|
|
|
ThinVec(None) => *self = vec![item].into(),
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 04:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<Vec<T>> for ThinVec<T> {
|
|
|
|
fn from(vec: Vec<T>) -> Self {
|
2019-12-22 22:42:04 +00:00
|
|
|
if vec.is_empty() { ThinVec(None) } else { ThinVec(Some(Box::new(vec))) }
|
2016-06-18 04:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 04:20:55 +00: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:56:18 +00:00
|
|
|
impl<T> FromIterator<T> for ThinVec<T> {
|
|
|
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
|
|
|
// `Vec::from_iter()` should not allocate if the iterator is empty.
|
|
|
|
let vec: Vec<_> = iter.into_iter().collect();
|
|
|
|
if vec.is_empty() { ThinVec(None) } else { ThinVec(Some(Box::new(vec))) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoIterator for ThinVec<T> {
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = std::vec::IntoIter<T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
// This is still performant because `Vec::new()` does not allocate.
|
|
|
|
self.0.map_or_else(Vec::new, |ptr| *ptr).into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> IntoIterator for &'a ThinVec<T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
type IntoIter = std::slice::Iter<'a, T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.as_ref().iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> IntoIterator for &'a mut ThinVec<T> {
|
|
|
|
type Item = &'a mut T;
|
|
|
|
type IntoIter = std::slice::IterMut<'a, T>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.as_mut().iter_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 04:01:57 +00: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(),
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 03:09:55 +00:00
|
|
|
|
|
|
|
fn extend_one(&mut self, item: T) {
|
2021-12-31 10:10:27 +00:00
|
|
|
self.push(item)
|
2020-05-13 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extend_reserve(&mut self, additional: usize) {
|
|
|
|
match *self {
|
|
|
|
ThinVec(Some(ref mut vec)) => vec.reserve(additional),
|
|
|
|
ThinVec(None) => *self = Vec::with_capacity(additional).into(),
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 04:01:57 +00:00
|
|
|
}
|
2018-12-03 00:14:35 +00:00
|
|
|
|
|
|
|
impl<T: HashStable<CTX>, CTX> HashStable<CTX> for ThinVec<T> {
|
2019-09-26 22:54:39 +00:00
|
|
|
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
|
2018-12-03 00:14:35 +00:00
|
|
|
(**self).hash_stable(hcx, hasher)
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 10:58:40 +00:00
|
|
|
|
|
|
|
impl<T> Default for ThinVec<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(None)
|
|
|
|
}
|
|
|
|
}
|
2021-04-03 02:56:18 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|