about summary refs log tree commit diff
path: root/src/libsyntax/util
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2019-02-05 15:20:55 +1100
committerNicholas Nethercote <nnethercote@mozilla.com>2019-02-06 09:06:27 +1100
commit9fcb1658ab13a7f722e4747c5a4b691291e88a3b (patch)
tree8c9a307c14cd3fc9e45466f7acca8fd2397ffe28 /src/libsyntax/util
parent970b5d189af48dd6ec26e90bb8d6d236824edf4b (diff)
downloadrust-9fcb1658ab13a7f722e4747c5a4b691291e88a3b.tar.gz
rust-9fcb1658ab13a7f722e4747c5a4b691291e88a3b.zip
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.
Diffstat (limited to 'src/libsyntax/util')
-rw-r--r--src/libsyntax/util/map_in_place.rs (renamed from src/libsyntax/util/move_map.rs)29
1 files changed, 8 insertions, 21 deletions
diff --git a/src/libsyntax/util/move_map.rs b/src/libsyntax/util/map_in_place.rs
index a0f9d39ce89..5724b540a0d 100644
--- a/src/libsyntax/util/move_map.rs
+++ b/src/libsyntax/util/map_in_place.rs
@@ -1,18 +1,18 @@
 use std::ptr;
 use smallvec::{Array, SmallVec};
 
-pub trait MoveMap<T>: Sized {
-    fn move_map<F>(self, mut f: F) -> Self where F: FnMut(T) -> T {
-        self.move_flat_map(|e| Some(f(e)))
+pub trait MapInPlace<T>: Sized {
+    fn map_in_place<F>(&mut self, mut f: F) where F: FnMut(T) -> T {
+        self.flat_map_in_place(|e| Some(f(e)))
     }
 
-    fn move_flat_map<F, I>(self, f: F) -> Self
+    fn flat_map_in_place<F, I>(&mut self, f: F)
         where F: FnMut(T) -> I,
               I: IntoIterator<Item=T>;
 }
 
-impl<T> MoveMap<T> for Vec<T> {
-    fn move_flat_map<F, I>(mut self, mut f: F) -> Self
+impl<T> MapInPlace<T> for Vec<T> {
+    fn flat_map_in_place<F, I>(&mut self, mut f: F)
         where F: FnMut(T) -> I,
               I: IntoIterator<Item=T>
     {
@@ -53,22 +53,11 @@ impl<T> MoveMap<T> for Vec<T> {
             // write_i tracks the number of actually written new items.
             self.set_len(write_i);
         }
-
-        self
-    }
-}
-
-impl<T> MoveMap<T> for ::ptr::P<[T]> {
-    fn move_flat_map<F, I>(self, f: F) -> Self
-        where F: FnMut(T) -> I,
-              I: IntoIterator<Item=T>
-    {
-        ::ptr::P::from_vec(self.into_vec().move_flat_map(f))
     }
 }
 
-impl<T, A: Array<Item = T>> MoveMap<T> for SmallVec<A> {
-    fn move_flat_map<F, I>(mut self, mut f: F) -> Self
+impl<T, A: Array<Item = T>> MapInPlace<T> for SmallVec<A> {
+    fn flat_map_in_place<F, I>(&mut self, mut f: F)
         where F: FnMut(T) -> I,
               I: IntoIterator<Item=T>
     {
@@ -109,7 +98,5 @@ impl<T, A: Array<Item = T>> MoveMap<T> for SmallVec<A> {
             // write_i tracks the number of actually written new items.
             self.set_len(write_i);
         }
-
-        self
     }
 }