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

Simplify HashMap layout calculation by using Layout #51163

Merged
merged 1 commit into from
Jun 1, 2018

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented May 29, 2018

RawTable uses a single allocation to hold both the array of hashes and the array of key/value pairs. This PR changes RawTable to use Layout when calculating the amount of memory to allocate instead of performing the calculation manually.

r? @SimonSapin

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 29, 2018
@gnzlbg
Copy link
Contributor

gnzlbg commented May 29, 2018

LGTM!

@SimonSapin
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 29, 2018

📌 Commit 0fde52b has been approved by SimonSapin

@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 May 29, 2018
@bors
Copy link
Contributor

bors commented May 30, 2018

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

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 30, 2018
@Amanieu
Copy link
Member Author

Amanieu commented May 31, 2018

Rebased.

@SimonSapin
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 31, 2018

📌 Commit 01ba7cb has been approved by SimonSapin

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 31, 2018
@bors
Copy link
Contributor

bors commented Jun 1, 2018

🔒 Merge conflict

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 1, 2018
@Amanieu
Copy link
Member Author

Amanieu commented Jun 1, 2018

Rebased again.

@SimonSapin I think this may conflict with your stabilization PR since I added impl From<LayoutErr> for CollectionAllocErr to libcore/alloc.rs.

@SimonSapin
Copy link
Contributor

@Amanieu I don’t mind the conflict, I’ll deal with it in the other PR.

But that impl might be notable with respect to stability. If CollectionAllocErr stays unstable but LayoutErr is stabilized, will (x: LayoutErr).into() allow stable code to obtain a CollectionAllocErr, making it to some extent de-facto stable?

@SimonSapin
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jun 1, 2018

📌 Commit c6bebf4 has been approved by SimonSapin

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 1, 2018
@bors
Copy link
Contributor

bors commented Jun 1, 2018

⌛ Testing commit c6bebf4 with merge aa094a4...

bors added a commit that referenced this pull request Jun 1, 2018
Simplify HashMap layout calculation by using Layout

`RawTable` uses a single allocation to hold both the array of hashes and the array of key/value pairs. This PR changes `RawTable` to use `Layout` when calculating the amount of memory to allocate instead of performing the calculation manually.

r? @SimonSapin
@bors
Copy link
Contributor

bors commented Jun 1, 2018

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

@bors bors merged commit c6bebf4 into rust-lang:master Jun 1, 2018
@andjo403
Copy link
Contributor

andjo403 commented Jun 2, 2018

noticed that this PR have a lot of big red numbers in the perf run maybe there is something that can be improved?

@Amanieu
Copy link
Member Author

Amanieu commented Jun 2, 2018

Hmm, that's worrying. I can think of three ways to improve performance:

  • A lot of the Layout methods aren't implemented very efficiently, they could be improved.
  • A lot of the Layout methods aren't marked as #[inline], which hurts us when the inputs are compile-time constants (size_of, align_of).
  • We could cache the pointer to the (K, V) array in the RawTable rather than recalculating it every time.

@gnzlbg
Copy link
Contributor

gnzlbg commented Jun 2, 2018 via email

@Amanieu
Copy link
Member Author

Amanieu commented Jun 2, 2018

I think the main issue here is that Layout::extend needs to be marked as inlined.

I made some changes in a branch but I think that it would be easier if you just added those changes to your PR to avoid conflicts.

@gnzlbg
Copy link
Contributor

gnzlbg commented Jun 2, 2018

@Amanieu the last commit in #51226 contains the changes from your branch, could you triplecheck those?

@nnethercote
Copy link
Contributor

noticed that this PR have a lot of big red numbers in the perf run maybe there is something that can be improved?

I found this independently today and worked back to this PR.

There was a subsequent improvement here:

http://perf.rust-lang.org/compare.html?start=251ec62a134824c1b8c01671a2d05aa793839232&end=3575be60eab140e69e5a75fe5c3b4119c2a17179&stat=instructions:u

which points to these changes:

251ec62...3575be6

But the improvement wasn't enough to get back all the perf that was lost.

Can this PR be backed out? I'd say it has undone roughly 1/3 to 1/2 of the significant rustc perf improvement that had occurred in past few weeks, which is highly discouraging.

cc @Mark-Simulacrum

@nnethercote
Copy link
Contributor

Here is a Cachegrind diff comparing a rustc from May 27 against one from Jun 04. The numbers are instruction counts:

293,637,556  /home/njn/moz/rustN/src/libcore/alloc.rs:<std::collections::hash::map::HashMap<K, V, S>>::get
199,291,681  /home/njn/moz/rustN/src/libstd/collections/hash/map.rs:<std::collections::hash::map::HashMap<K, V, S>>::get
165,729,516  /home/njn/moz/rustN/src/libstd/collections/hash/table.rs:<std::collections::hash::map::HashMap<K, V, S>>::get
159,843,803  /home/njn/moz/rustN/src/libcore/num/mod.rs:<std::collections::hash::map::HashMap<K, V, S>>::get
 85,460,589  /home/njn/moz/rustN/src/libstd/collections/hash/set.rs:<std::collections::hash::set::HashSet<T, S>>::get
 83,626,730  /home/njn/moz/rustN/src/libcore/alloc.rs:rustc::ty::context::TypeckTables::node_id_to_type_opt
-82,546,355  /home/njn/moz/rustN/src/libstd/collections/hash/map.rs:rustc::middle::region::ScopeTree::temporary_scope
 71,081,752  /home/njn/moz/rustN/src/libstd/collections/hash/table.rs:<std::collections::hash::set::HashSet<T, S>>::get
 67,990,342  /home/njn/moz/rustN/src/libstd/collections/hash/map.rs:<std::collections::hash::set::HashSet<T, S>>::get
 63,276,945  /home/njn/moz/rustN/src/libcore/num/mod.rs:<std::collections::hash::set::HashSet<T, S>>::get
-62,391,972  /home/njn/moz/rustN/src/libsyntax/parse/parser.rs:syntax::parse::parser::Parser::check_keyword
 60,819,440  /home/njn/moz/rustN/src/libcore/num/mod.rs:rustc::ty::context::TypeckTables::node_id_to_type_opt
-60,538,623  /home/njn/moz/rustN/src/libcore/num/mod.rs:rustc::middle::region::ScopeTree::temporary_scope
 57,140,958  /home/njn/moz/rustN/src/libcore/alloc.rs:<std::collections::hash::map::HashMap<K, V, S>>::try_resize
-51,742,844  /home/njn/moz/rustN/src/libstd/collections/hash/table.rs:rustc::middle::region::ScopeTree::temporary_scope
 51,120,900  /home/njn/moz/rustN/src/librustc/middle/region.rs:rustc::middle::region::ScopeTree::temporary_scope
 49,415,795  /home/njn/moz/rustN/src/libstd/collections/hash/table.rs:rustc::ty::context::TypeckTables::node_id_to_type_opt

It's mostly additional hash table operations, perhaps involving node_id_to_type_opt.

bors added a commit that referenced this pull request Jun 4, 2018
Optimize layout calculations in HashMap

This now produces the same assembly code as the previous implementation.

cc #51163 @nnethercote @gnzlbg @andjo403
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.

None yet

7 participants