Commit Graph

125 Commits

Author SHA1 Message Date
Nicholas Nethercote c290293cf2 Derive Rustc{En,De}codable for TokenStream.
`TokenStream` used to be a complex type, but it is now just a newtype
around a `Lrc<Vec<TreeAndJoint>>`. Currently it uses custom encoding
that discards the `IsJoint` and custom decoding that adds `NonJoint`
back in for every token tree. This requires building intermediate
`Vec<TokenTree>`s.

This commit makes `TokenStream` derive `Rustc{En,De}codable`. This
simplifies the code, and avoids the creation of the intermediate
vectors, saving up to 3% on various benchmarks. It also changes the AST
JSON output in one test.
2019-10-21 06:54:41 +11:00
Nicholas Nethercote e4ec4a6da3 Change MetaItem::tokens() to MetaItem::token_trees_and_joints().
Likewise for `NestedMetaItem::tokens()`. Also, add
`MetaItemKind::token_trees_and_joints()`, which `MetaItemKind::tokens()`
now calls.

This avoids some unnecessary `TokenTree` to `TokenStream` conversions,
and removes the need for the clumsy
`TokenStream::append_to_tree_and_joint_vec()`.
2019-10-18 13:25:17 +11:00
Nicholas Nethercote a6eef299d3 Make TokenStream::from_iter less general and more efficient.
The current code has this impl:
```
impl<T: Into<TokenStream>> iter::FromIterator<T> for TokenStream
```
If given an `IntoIterator<Item = TokenTree>`, it will convert each individual
`TokenTree` to a `TokenStream` (at the cost of two allocations: a `Vec`
and an `Lrc`). It will then merge those `TokenStream`s into a single
`TokenStream`. This is inefficient.

This commit changes the impl to this less general one:
```
impl iter::FromIterator<TokenTree> for TokenStream
```
It collects the `TokenTree`s into a single `Vec` first and then converts that
to a `TokenStream` by wrapping it in a single `Lrc`. The previous generality
was unnecessary; no other code needs changing.

This change speeds up several benchmarks by up to 4%.
2019-10-18 13:25:17 +11:00
Mazdak Farrokhzad d420d719c4 move syntax::ext to new crate syntax_expand 2019-10-16 10:59:53 +02:00
Tyler Mandry 6d28ed1ae6 Rollup merge of #65261 - nnethercote:rm-Option-from-TokenStream, r=petrochenkov
Remove `Option` from `TokenStream`

A code simplification.

r? @petrochenkov
2019-10-14 17:52:35 -07:00
Nicholas Nethercote 5c93492da9 Remove the Option in TokenStream.
It means an allocation is required to create an empty `TokenStream`, but
all other operations are simpler and marginally faster due to not having
to check for `None`. Overall it simplifies the code for a negligible
performance effect.

The commit also removes `TokenStream::empty` by implementing `Default`,
which is now possible.
2019-10-14 09:14:39 +11:00
Mazdak Farrokhzad ab8105ee97 tokenstream: don't depend on pprust 2019-10-13 07:00:32 +02:00
Nicholas Nethercote 75e0078a17 Optimize TokenStreamBuilder::push.
Currently, when two tokens must be glued together, this function duplicates
large chunks of the existing streams. This can cause quadratic behaviour.

This commit changes the function so that it overwrites the last token with a
glued token, which avoids the quadratic behaviour. This removes the need for
`TokenStreamBuilder::push_all_but_{first,last}_tree`.

The commit also restructures `push` somewhat, by removing
`TokenStream::{first_tree_and_joint,last_tree_if_joint}` in favour of more
pattern matching and some comments. This makes the code shorter, and in my
opinion, more readable.
2019-10-08 16:57:29 +11:00
Nicholas Nethercote 3832a634d3 Optimize TokenStream::from_streams.
Currently, this function creates a new empty stream, and then appends
the elements from each given stream onto that stream. This can cause
quadratic behaviour.

This commit changes the function so that it modifies the first stream
(which can be long) by extending it with the elements from the
subsequent streams (which are almost always short), which avoids the
quadratic behaviour.
2019-10-08 16:57:07 +11:00
Aleksey Kladov 983569732d push TokenTree::parse down 2019-09-22 20:38:02 +03:00
Aleksey Kladov 9fd75f5287 pull mbe token tree definition up 2019-09-22 20:37:59 +03:00
Aleksey Kladov 49f849cdb7 rename tt -> mbe, part 2 2019-09-22 20:30:00 +03:00
Aleksey Kladov 645cdca9ba reduce visibility of a bunch of stuff in ext::tt 2019-09-22 20:29:31 +03:00
Alexander Regueiro 49d2fd1725 Aggregation of cosmetic changes made during work on REPL PRs: libsyntax 2019-09-07 16:29:04 +01:00
Aleksey Kladov fa893a3225 use TokenStream rather than &[TokenTree] for built-in macros
That way, we don't loose the jointness info
2019-09-03 21:15:45 +03:00
Vadim Petrochenkov cf9db76454 hygiene: Require passing transparency explicitly to apply_mark 2019-08-23 01:44:33 +03:00
Aleksey Kladov 914e1f4564 glue tokens when building token stream 2019-08-19 21:59:33 +03:00
Vadim Petrochenkov 310b9fc760 libsyntax: Unconfigure tests during normal build 2019-08-02 01:59:01 +03:00
Vadim Petrochenkov 614037171b cleanup: Remove extern crate serialize as rustc_serializes 2019-07-23 19:20:16 +03:00
Vadim Petrochenkov 09703e3843 Adjust other names after the Mark renaming 2019-07-19 12:01:49 +03:00
Vadim Petrochenkov 31e10aec83 libsyntax: Remove Mark into ExpnId 2019-07-19 12:01:48 +03:00
Mark Rousskov 8a7dded1a2 Switch master to 1.38 2019-07-04 11:26:57 -04:00
Felix S Klock II 0baa9258dd put back the workarounds for #60846
based on https://github.com/rust-lang/rust/pull/61754#issuecomment-501743750 I am adding `bootstrap` to the cfg-preconditions for the two manual `unsafe impls`'s of `Send` and `Sync` for `TokenTree`.
2019-06-14 12:19:26 +02:00
Niko Matsakis 6fdcc8281a remove hacks that are no longer needed 2019-06-12 13:56:29 -04:00
Vadim Petrochenkov 25b05147b3 syntax: Remove Deref impl from Token 2019-06-08 22:38:23 +03:00
Vadim Petrochenkov 0ca3c2f881 syntax: Move most of the TokenKind methods to Token 2019-06-08 22:38:12 +03:00
Vadim Petrochenkov ff40e37b98 Some code cleanup and tidy/test fixes 2019-06-06 14:04:02 +03:00
Vadim Petrochenkov 67ce3f4589 syntax: Switch function parameter order in TokenTree::token 2019-06-06 14:04:02 +03:00
Vadim Petrochenkov f745e5f9b6 syntax: Remove duplicate span from token::Ident 2019-06-06 14:04:02 +03:00
Vadim Petrochenkov 5e693531ff syntax: Add some helper methods to Token 2019-06-06 14:04:02 +03:00
Vadim Petrochenkov e0127dbf81 syntax: Use Token in TokenTree::Token 2019-06-06 14:03:15 +03:00
Vadim Petrochenkov a3425edb46 syntax: Rename TokenAndSpan into Token 2019-06-06 14:03:15 +03:00
Vadim Petrochenkov 99b27d749c syntax: Rename Token into TokenKind 2019-06-06 14:03:14 +03:00
Vadim Petrochenkov eac3846b65 Always use token kinds through token module rather than Token type 2019-06-06 14:01:57 +03:00
John Kåre Alsaker 3ed05613ee Short circuit Send and Sync impls for TokenTree 2019-05-27 08:58:05 +02:00
John Kåre Alsaker a1f2dceaeb Move edition outside the hygiene lock and avoid accessing it 2019-05-21 18:17:05 +02:00
Vadim Petrochenkov 88fa5c6a45 Improve type size assertions
Now they
- Tell what the new size is, when it changes
- Do not require passing an identifier
2019-05-19 13:59:44 +03:00
Esteban Küber 0e505d427a Add guard for missing comma in macro call suggestion 2019-04-24 16:45:29 -07:00
Nicholas Nethercote 17a8aff20a Use SmallVec in TokenStreamBuilder.
This reduces by 12% the number of allocations done for a "clean
incremental" of `webrender_api`, which reduces the instruction count by
about 0.5%.

It also reduces instruction counts by up to 1.4% across a range of
rustc-perf benchmark runs.
2019-03-29 09:32:58 +11:00
Esteban Küber 27abd52170 Fix operator precedence 2019-03-13 00:10:16 -07:00
Esteban Küber b9d12edd6c Be more discerning on when to attempt suggesting a comma in a macro invocation 2019-03-11 15:07:07 -07:00
Vadim Petrochenkov 8e1b5d897a Restrict value in key-value attributes to literals 2019-02-25 22:40:38 +03:00
Nicholas Nethercote 82ad4f1f45 Make interpolated_to_tokenstream a method on Nonterminal. 2019-02-18 10:06:26 +11:00
Alexander Regueiro c3e182cf43 rustc: doc comments 2019-02-10 23:42:32 +00:00
Taiki Endo 7bb082d27f libsyntax => 2018 2019-02-07 02:33:01 +09:00
bors 2596bc1368 Auto merge of #58061 - nnethercote:overhaul-syntax-Folder, r=petrochenkov
Overhaul `syntax::fold::Folder`.

This PR 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`.

This makes the code faster and more concise.
2019-02-06 06:01:37 +00:00
Nicholas Nethercote 9fcb1658ab 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-06 09:06:27 +11:00
Felix S. Klock II 1a18336808 proc_macro: make TokenStream::from_streams pre-allocate its vector.
This requires a pre-pass over the input streams. But that is cheap
compared to the quadratic blowup associated with reallocating the
accumulating vector on-the-fly.
2019-01-30 15:12:41 +01:00
Nicholas Nethercote 7285724401 Make TokenStream use Option.
Because that's the more typical way of representing an all-or-nothing
type.
2019-01-14 11:05:56 +11:00
Nicholas Nethercote ce0d9949b8 Remove ThinTokenStream.
`TokenStream` is now almost identical to `ThinTokenStream`. This commit
removes the latter, replacing it with the former.
2019-01-14 09:10:26 +11:00