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

Add comments to explain memory usage optimization #78887

Merged
merged 1 commit into from
Nov 11, 2020

Conversation

camelid
Copy link
Member

@camelid camelid commented Nov 8, 2020

Add explanatory comments so that people understand that it's just an optimization and doesn't affect behavior.

@camelid camelid added A-mir-opt Area: MIR optimizations C-cleanup Category: PRs that clean code up or issues documenting cleanup. labels Nov 8, 2020
@rust-highfive
Copy link
Collaborator

r? @lcnr

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 8, 2020
Comment on lines 216 to 215
state.clone_from(&entry_sets[bb]);
let mut state = entry_sets[bb].clone();
Copy link
Member Author

@camelid camelid Nov 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC state.clone_from(...) and state = (...).clone() should be equivalent in functionality, so this shouldn't change the behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have a domain which potentially reuses an allocation when using clone_from this could matter, but I don't really know if that can be the case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't BitSet already do that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just do a perf run to check?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not remove this optimization, even if it doesn't affect perf today

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I found this code confusing when I read through it since it makes it look like state is shared between each loop iteration, when in fact it's reinitialized for each iteration.

@lcnr
Copy link
Contributor

lcnr commented Nov 8, 2020

r? @jonas-schievink

@rust-highfive rust-highfive assigned jonas-schievink and unassigned lcnr Nov 8, 2020
@jonas-schievink
Copy link
Contributor

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Nov 8, 2020

⌛ Trying commit 172b60de3067c9b66bd111298fc898e739ebb8e6 with merge f79c7551c0ed89caa2987f63188f7a66b7eea242...

@bors
Copy link
Contributor

bors commented Nov 9, 2020

☀️ Try build successful - checks-actions
Build commit: f79c7551c0ed89caa2987f63188f7a66b7eea242 (f79c7551c0ed89caa2987f63188f7a66b7eea242)

@rust-timer
Copy link
Collaborator

Queued f79c7551c0ed89caa2987f63188f7a66b7eea242 with parent 1773f60, future comparison URL.

@camelid camelid added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 9, 2020
@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (f79c7551c0ed89caa2987f63188f7a66b7eea242): comparison url.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

@bors rollup=never
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-perf

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 9, 2020
@camelid
Copy link
Member Author

camelid commented Nov 9, 2020

The results seem pretty neutral. However, I've never analyzed perf results before, so take that with a grain of salt :)

@jonas-schievink
Copy link
Contributor

There are small but clear regressions in a lot of benchmarks though?

@camelid
Copy link
Member Author

camelid commented Nov 9, 2020

Looking at wall-clock time, it seems that there were some improvements and some regressions. Btw, are there docs on how to interpret perf results?

@jonas-schievink
Copy link
Contributor

Anything based on real-world time is going to be too noisy to be useful for a low-impact change like this. I don't think we have any docs on this at the moment (but many parts are not Rust-specific, they apply to interpreting profiles in general).

Regarding this PR, I don't think we want to land this as-is, but I'd be happy to r+ a patch that adds a short comment explaining why clone_from is used.

@Mark-Simulacrum
Copy link
Member

I agree that this is a small, but fairly clear, regression in instruction counts on multiple benchmarks. Wall times are indeed too noisy for looking at changes like this.

We do not have significant documentation on performance triaging, it probably makes sense to write something but I don't know if I can find the time to do so myself.

@camelid
Copy link
Member Author

camelid commented Nov 9, 2020

Okay, I'll change this so that it's just adding an explanatory comment :)

@camelid camelid changed the title Move state declaration into loop to make code clearer Add comments to explain memory usage optimization Nov 9, 2020
@@ -208,12 +208,19 @@ where
}
}

// `state` is not actually used between iterations;
// this is just an optimization to avoid reallocating
// every iteration.
let mut state = analysis.bottom_value(body);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an idea for a further optimization: change this to

Suggested change
let mut state = analysis.bottom_value(body);
let mut state = MaybeUninit::uninit();

since it's always initialized with clone_from below. Might not be worth the unsafety though 🤷

@camelid camelid added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools and removed C-cleanup Category: PRs that clean code up or issues documenting cleanup. labels Nov 9, 2020
@jonas-schievink
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Nov 9, 2020

📌 Commit 0242f96 has been approved by jonas-schievink

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 9, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 11, 2020
…as-schievink

Rollup of 14 pull requests

Successful merges:

 - rust-lang#76765 (Make it more clear what an about async fn's returns when referring to what it returns)
 - rust-lang#78574 (Use check-pass instead of build-pass in regions ui test suite)
 - rust-lang#78669 (Use check-pass instead of build-pass in some consts ui test suits)
 - rust-lang#78847 (Assert that a return place is not used for indexing during integration)
 - rust-lang#78854 (Workaround for "could not fully normalize" ICE )
 - rust-lang#78875 (rustc_target: Further cleanup use of target options)
 - rust-lang#78887 (Add comments to explain memory usage optimization)
 - rust-lang#78890 (comment attribution fix)
 - rust-lang#78896 (Clarified description of write! macro)
 - rust-lang#78897 (Add missing newline to error message of the default OOM hook)
 - rust-lang#78898 (add regression test for rust-lang#78892)
 - rust-lang#78908 ((rustdoc) [src] link for types defined by macros shows invocation, not defintion)
 - rust-lang#78910 (Fix links to stabilized versions of some intrinsics)
 - rust-lang#78912 (Add macro test for min-const-generics)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a08e7af into rust-lang:master Nov 11, 2020
@rustbot rustbot added this to the 1.49.0 milestone Nov 11, 2020
@camelid camelid deleted the dataflow-state-decl branch November 11, 2020 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-mir-opt Area: MIR optimizations S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants