Skip to content

[nll] optimize redundant reborrows #53176

Closed

Description

@nikomatsakis

When you have a borrow like this:

let x = &*y

where y: &T, then there is really no reason for the borrow checker to "record" the borrow or figure out its scope. The reason is that there is no action that one can do with y that would invalidate x. (Of course we still want to record the outlives relationship that forces y's lifetime to outlive x's lifetime.)

The old AST borrow checker did this same optimization. It ought to have a huge effect on html5ever: my profile measurements suggest that basically all of its time is spent with these very large borrow sets.

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-NLLArea: Non-lexical lifetimes (NLL)
    NLL-performantWorking towards the "performance is good" goal
    on Aug 7, 2018
  2. added this to the Rust 2018 RC milestone on Aug 7, 2018
  3. changed the title [-][nll] avoid creating borrows for reborrows of shared references[/-] [+][nll] optimize redundant reborrows[/+] on Aug 7, 2018
  4. nikomatsakis commented on Aug 7, 2018

    @nikomatsakis
    ContributorAuthor

    Let me leave a few notes here. I'm not 100% sure on what changes are needed, but this is the general vicinity.

    We compute the "set of all borrows" into this BorrowSet data structure:

    crate struct BorrowSet<'tcx> {

    In that data structure, we currently walk over the MIR and identify assignments of borrows (e.g., X = &<place>). For each of those, we allocate a borrow index... well, almost each of them. For some borrows, we don't bother -- in particular, borrows of "unsafe places":

    if borrowed_place.is_unsafe_place(self.tcx, self.mir) {
    return;
    }

    An 'unsafe place' is something like the deref of a raw pointer:

    https://github.com/rust-lang/rust/blob/master/src/librustc_mir/borrow_check/place_ext.rs#L27

    I think we basically want to rename is_unsafe_place to something like ignore_borrow_of, and update the command like so:

    /// Returns true if we can safely ignore borrows of this place.
    /// This is true whenever there is no action that the user can do
    /// to the place `self` that would invalidate the borrow. This is true
    /// for borrows of raw pointer dereferents as well as shared references.
    fn ignore_borrow_of(&self) -> bool {
        ...
    }

    then we will add [TyRef](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/enum.TypeVariants.html#variant.TyRef to this match here:

    match ty.sty {
    ty::TyRawPtr(..) => true,
    _ => proj.base.is_unsafe_place(tcx, mir),
    }

    Specifically, TyRef when the Mutability is MutImmutable.

  5. nikomatsakis commented on Aug 7, 2018

    @nikomatsakis
    ContributorAuthor

    So, I couldnt' help myself. I had a few minutes and that changed seemed so easy that I gave it a spin.

  6. self-assigned this
    on Aug 8, 2018
  7. lqd commented on Aug 16, 2018

    @lqd
    Member

    This was completed in #53177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)NLL-performantWorking towards the "performance is good" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions