Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate fewer copies of a closure inside a generic function #46477

Open
dtolnay opened this issue Dec 3, 2017 · 5 comments

Comments

@dtolnay
Copy link
Member

commented Dec 3, 2017

In serde-rs/json#386 we observed that a disproportionately large amount of serde_json lines of LLVM IR and compile time are due to a tiny closure inside a generic function. In fact this closure contributes more LLVM IR than all but 5 significantly larger functions.

The generic function needs to be instantiated lots of times, but the closure does not capture anything that would be affected by the type parameter.

Simplified example:

// cargo rustc -- --emit=llvm-ir
pub fn f() {
    g::<bool>();
    g::<usize>();
}

fn g<T>() -> usize {
    let n = 1;
    let closure = || n;
    closure()
}

This gives the expected 1 copy of f and 2 copies of g, but unexpectedly 2 copies of g::{{closure}} in the IR.

@Mark-Simulacrum

@Mark-Simulacrum

This comment has been minimized.

Copy link
Member

commented Dec 3, 2017

cc @michaelwoerister @eddyb @arielb1

Seems like this has potential for some fairly large wins across Rust.

@eddyb

This comment has been minimized.

Copy link
Member

commented Dec 3, 2017

This is a subset of being able to detect parameter dependence from MIR, and sharing instances on the monomorphization collector based on it.
Should be relatively straight-forward nowadays.

EDIT: in fact, I think all you need is to implement TypeVisitor::visit_ty and put MIR through it, accumulating a bitset of "does this type parameter appear", at least on the analysis side.

@michaelwoerister

This comment has been minimized.

Copy link
Contributor

commented Dec 4, 2017

Interesting find!

@jonhoo

This comment has been minimized.

Copy link
Contributor

commented Jan 18, 2018

Just to leave a breadcrumb for later, there are other good suggestions for similar kinds of optimizations that can be done in this internals thread.

@Eh2406

This comment has been minimized.

Copy link
Contributor

commented Sep 20, 2019

This came up in conversation at a meetup recently. Several of us thought it would be interesting to see how big in impact it makes. None of us have any experience working on the compiler. How hard is this for a new contributor? Is there mentorship available? Alternatively does someone want to do some kind of remote presentation for our meetup, guiding us on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.