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 · 4 comments

Comments

Projects
None yet
7 participants
@dtolnay
Copy link
Member

dtolnay 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

Mark-Simulacrum 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

eddyb 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

michaelwoerister commented Dec 4, 2017

Interesting find!

@jonhoo

This comment has been minimized.

Copy link
Contributor

jonhoo 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.

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