Skip to content

Conversation

ljedrz
Copy link
Contributor

@ljedrz ljedrz commented Oct 12, 2018

The contents of unwrap_or are evaluated eagerly, so it's not a good pick in case of function calls and allocations. This PR also changes a few unwrap_ors with unwrap_or_default.

An added bonus is that in some cases this change also reveals if the object it's called on is an Option or a Result (based on whether the closure takes an argument).

@rust-highfive
Copy link
Contributor

r? @matthewjasper

(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 Oct 12, 2018
@@ -544,7 +544,7 @@ pub fn rustc_cargo_env(builder: &Builder, cargo: &mut Command) {
.env("CFG_PREFIX", builder.config.prefix.clone().unwrap_or_default())
.env("CFG_CODEGEN_BACKENDS_DIR", &builder.config.rust_codegen_backends_dir);

let libdir_relative = builder.config.libdir_relative().unwrap_or(Path::new("lib"));
let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, ideally, using the _else variant could be a great indicator whether the computation is actually expensive. This change, for example, is not necessary: It is just a cast.

I'm fine with changing all function calls to _else for now, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah most of these _else are line noise with no positive impact. Many wrap functions like String::new, Vec::new, tuple struct constructors, etc. that are literally free.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rkruppe well, most are to_string(), which are not free.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair, I haven't actually counted, but I did see a significant number of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rkruppe you are 100% right about Vec/String::new, though; I knew they didn't allocate, but I didn't notice they had become const fn.

@ljedrz
Copy link
Contributor Author

ljedrz commented Oct 13, 2018

Comments addressed; I replaced calls to unwrap_or_else(|| Vec/String::new()) (and a few applicable others) with unwrap_or_default(), I also removed the accidental change to one tuple struct constructor.

@bors
Copy link
Collaborator

bors commented Oct 17, 2018

☔ The latest upstream changes (presumably #54941) made this pull request unmergeable. Please resolve the merge conflicts.

@ljedrz
Copy link
Contributor Author

ljedrz commented Oct 17, 2018

@matthewjasper rebased.

@bors
Copy link
Collaborator

bors commented Oct 18, 2018

☔ The latest upstream changes (presumably #55171) made this pull request unmergeable. Please resolve the merge conflicts.

@ljedrz
Copy link
Contributor Author

ljedrz commented Oct 18, 2018

Rebased.

@bors
Copy link
Collaborator

bors commented Oct 18, 2018

☔ The latest upstream changes (presumably #54976) made this pull request unmergeable. Please resolve the merge conflicts.

@ljedrz
Copy link
Contributor Author

ljedrz commented Oct 19, 2018

Re(eee)based. @kennytm these are trivial changes good for a rollup if they can be squeezed in; I won't be able to rebase for the next few days.

@matthewjasper
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 19, 2018

📌 Commit d28aed6 has been approved by matthewjasper

@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 Oct 19, 2018
@bors
Copy link
Collaborator

bors commented Oct 20, 2018

⌛ Testing commit d28aed6 with merge 398c664f3a4b3cfb83992c05604f87266a029e38...

@bors
Copy link
Collaborator

bors commented Oct 20, 2018

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 20, 2018
@matthewjasper
Copy link
Contributor

@bors retry

@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 Oct 20, 2018
@bors
Copy link
Collaborator

bors commented Oct 20, 2018

⌛ Testing commit d28aed6 with merge ca2639e...

bors added a commit that referenced this pull request Oct 20, 2018
Prefer unwrap_or_else to unwrap_or in case of function calls/allocations

The contents of `unwrap_or` are evaluated eagerly, so it's not a good pick in case of function calls and allocations. This PR also changes a few `unwrap_or`s with `unwrap_or_default`.

An added bonus is that in some cases this change also reveals if the object it's called on is an `Option` or a `Result` (based on whether the closure takes an argument).
@bors
Copy link
Collaborator

bors commented Oct 20, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: matthewjasper
Pushing ca2639e to master...

@bors bors merged commit d28aed6 into rust-lang:master Oct 20, 2018
@ljedrz ljedrz deleted the lazyboye_unwraps branch October 20, 2018 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

7 participants