2018-08-30 11:42:16 +02:00
|
|
|
use smallvec::{Array, SmallVec};
|
2019-12-22 17:42:04 -05:00
|
|
|
use std::ptr;
|
2016-11-02 22:33:35 -06:00
|
|
|
|
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 15:20:55 +11:00
|
|
|
pub trait MapInPlace<T>: Sized {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn map_in_place<F>(&mut self, mut f: F)
|
|
|
|
where
|
|
|
|
F: FnMut(T) -> T,
|
|
|
|
{
|
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 15:20:55 +11:00
|
|
|
self.flat_map_in_place(|e| Some(f(e)))
|
2015-11-15 21:19:53 +01:00
|
|
|
}
|
|
|
|
|
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 15:20:55 +11:00
|
|
|
fn flat_map_in_place<F, I>(&mut self, f: F)
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
F: FnMut(T) -> I,
|
|
|
|
I: IntoIterator<Item = T>;
|
2015-11-15 21:19:53 +01:00
|
|
|
}
|
|
|
|
|
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 15:20:55 +11:00
|
|
|
impl<T> MapInPlace<T> for Vec<T> {
|
|
|
|
fn flat_map_in_place<F, I>(&mut self, mut f: F)
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
F: FnMut(T) -> I,
|
|
|
|
I: IntoIterator<Item = T>,
|
2015-11-15 21:19:53 +01:00
|
|
|
{
|
|
|
|
let mut read_i = 0;
|
|
|
|
let mut write_i = 0;
|
|
|
|
unsafe {
|
|
|
|
let mut old_len = self.len();
|
|
|
|
self.set_len(0); // make sure we just leak elements in case of panic
|
|
|
|
|
|
|
|
while read_i < old_len {
|
|
|
|
// move the read_i'th item out of the vector and map it
|
|
|
|
// to an iterator
|
|
|
|
let e = ptr::read(self.get_unchecked(read_i));
|
2017-05-12 20:05:39 +02:00
|
|
|
let iter = f(e).into_iter();
|
2015-11-15 21:19:53 +01:00
|
|
|
read_i += 1;
|
|
|
|
|
2017-05-12 20:05:39 +02:00
|
|
|
for e in iter {
|
2015-11-15 21:19:53 +01:00
|
|
|
if write_i < read_i {
|
|
|
|
ptr::write(self.get_unchecked_mut(write_i), e);
|
|
|
|
write_i += 1;
|
|
|
|
} else {
|
|
|
|
// If this is reached we ran out of space
|
|
|
|
// in the middle of the vector.
|
|
|
|
// However, the vector is in a valid state here,
|
|
|
|
// so we just do a somewhat inefficient insert.
|
|
|
|
self.set_len(old_len);
|
|
|
|
self.insert(write_i, e);
|
|
|
|
|
|
|
|
old_len = self.len();
|
|
|
|
self.set_len(0);
|
|
|
|
|
|
|
|
read_i += 1;
|
|
|
|
write_i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_i tracks the number of actually written new items.
|
|
|
|
self.set_len(write_i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 22:33:35 -06:00
|
|
|
|
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 15:20:55 +11:00
|
|
|
impl<T, A: Array<Item = T>> MapInPlace<T> for SmallVec<A> {
|
|
|
|
fn flat_map_in_place<F, I>(&mut self, mut f: F)
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
F: FnMut(T) -> I,
|
|
|
|
I: IntoIterator<Item = T>,
|
2016-11-02 22:33:35 -06:00
|
|
|
{
|
|
|
|
let mut read_i = 0;
|
|
|
|
let mut write_i = 0;
|
|
|
|
unsafe {
|
|
|
|
let mut old_len = self.len();
|
|
|
|
self.set_len(0); // make sure we just leak elements in case of panic
|
|
|
|
|
|
|
|
while read_i < old_len {
|
|
|
|
// move the read_i'th item out of the vector and map it
|
|
|
|
// to an iterator
|
|
|
|
let e = ptr::read(self.get_unchecked(read_i));
|
2017-05-12 20:05:39 +02:00
|
|
|
let iter = f(e).into_iter();
|
2016-11-02 22:33:35 -06:00
|
|
|
read_i += 1;
|
|
|
|
|
2017-05-12 20:05:39 +02:00
|
|
|
for e in iter {
|
2016-11-02 22:33:35 -06:00
|
|
|
if write_i < read_i {
|
|
|
|
ptr::write(self.get_unchecked_mut(write_i), e);
|
|
|
|
write_i += 1;
|
|
|
|
} else {
|
|
|
|
// If this is reached we ran out of space
|
|
|
|
// in the middle of the vector.
|
|
|
|
// However, the vector is in a valid state here,
|
|
|
|
// so we just do a somewhat inefficient insert.
|
|
|
|
self.set_len(old_len);
|
|
|
|
self.insert(write_i, e);
|
|
|
|
|
|
|
|
old_len = self.len();
|
|
|
|
self.set_len(0);
|
|
|
|
|
|
|
|
read_i += 1;
|
|
|
|
write_i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_i tracks the number of actually written new items.
|
|
|
|
self.set_len(write_i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|