Update: I wrote this before I knew this PR was updated recently: https://github.com/rust-lang/rust/pull/34164. I’ll look at that PR and revise this if needed.
I’m planning to implement one simple instance of the several optimizations discussed in https://github.com/rust-lang/rust/issues/32966.
I’m calling this “move up propagation.” For example, we want to replace:
tmp0 = ... // statements that do not use tmp0 tmp1 = tmp0
tmp1 = ... // statements that do not use tmp0
The definition of “statements that do not use tmp0” is a little tricky. For my first step, I’m planning on considering any reference as potentially aliasing tmp0. Possible refinements are considering any particular reference to only alias things that have already been borrowed. Even better refinements are definitely possible in the future.
Another potential issue I discussed with @nikomatsakis was threading. For example:
let x = ...1; let p = &mut x; send(p); // some other thread may mutate x through ref let tmp0 = ...2; block(); // thread ends x = tmp0;
A bad optimization would be:
let x = ...1; let p = &mut x; send(p); // some other thread may mutate x through ref x = ..2; block(); // thread ends
The current plan to avoid this problem is not apply the optimization when the statements between the definition and use include function call(s). Some refinement to this restriction is probably possible in the future (again).
I plan to implement this and measure how beneficial it is (or isn’t) and then think about the refinements based on that data.
I’m interested in any feedback people have on this specific optimization. I’m planning on holding off on the other ideas we discussed here (or generalizing) until later.