rust/src/librustc_traits
bors f686885a14 Auto merge of #52342 - nnethercote:CanonicalVar, r=nikomatsakis
Avoid most allocations in `Canonicalizer`.

Extra allocations are a significant cost of NLL, and the most common
ones come from within `Canonicalizer`. In particular, `canonical_var()`
contains this code:

    indices
	.entry(kind)
	.or_insert_with(|| {
	    let cvar1 = variables.push(info);
	    let cvar2 = var_values.push(kind);
	    assert_eq!(cvar1, cvar2);
	    cvar1
	})
	.clone()

`variables` and `var_values` are `Vec`s. `indices` is a `HashMap` used
to track what elements have been inserted into `var_values`. If `kind`
hasn't been seen before, `indices`, `variables` and `var_values` all get
a new element. (The number of elements in each container is always the
same.) This results in lots of allocations.

In practice, most of the time these containers only end up holding a few
elements. This PR changes them to avoid heap allocations in the common
case, by changing the `Vec`s to `SmallVec`s and only using `indices`
once enough elements are present. (When the number of elements is small,
a direct linear search of `var_values` is as good or better than a
hashmap lookup.)

The changes to `variables` are straightforward and contained within
`Canonicalizer`. The changes to `indices` are more complex but also
contained within `Canonicalizer`. The changes to `var_values` are more
intrusive because they require defining a new type
`SmallCanonicalVarValues` -- which is to `CanonicalVarValues` as
`SmallVec` is to `Vec -- and passing stack-allocated values of that type
in from outside.

All this speeds up a number of NLL "check" builds, the best by 2%.

r? @nikomatsakis
2018-07-18 00:45:57 +00:00
..
Cargo.toml implement the chalk traits, albeit with many placeholders 2018-05-24 12:01:27 -04:00
chalk_context.rs Avoid most allocations in Canonicalizer. 2018-07-17 13:42:11 +10:00
dropck_outlives.rs change the enter_canonical_trait_query method to give a fulfill cx 2018-06-27 16:04:32 -04:00
evaluate_obligation.rs move into provide methods 2018-06-27 09:42:21 -04:00
lib.rs Deny bare trait objects in the rest of rust 2018-07-12 13:50:22 +02:00
lowering.rs Change wording 2018-07-09 21:20:26 +02:00
normalize_erasing_regions.rs move into provide methods 2018-06-27 09:42:21 -04:00
normalize_projection_ty.rs change the enter_canonical_trait_query method to give a fulfill cx 2018-06-27 16:04:32 -04:00
type_op.rs rustfmt various files 2018-06-27 16:30:38 -04:00