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

BTreeMap: reuse BoxedNode instances directly instead of their contents #77408

Closed
wants to merge 1 commit into from

Conversation

ssomers
Copy link
Contributor

@ssomers ssomers commented Oct 1, 2020

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 1, 2020
@ssomers
Copy link
Contributor Author

ssomers commented Oct 1, 2020

Benchmarks pretend to be a bit faster, despite push/pop being relatively rare.

 name                                         old ns/iter  new ns/iter  diff ns/iter  diff %  speedup
 btree::map::clone_fat_100_and_into_iter      73,040       70,605             -2,435  -3.33%   x 1.03
 btree::map::insert_rand_10_000               33           31                     -2  -6.06%   x 1.06
 btree::map::insert_seq_10_000                96           90                     -6  -6.25%   x 1.07
 btree::map::iteration_mut_20                 69           66                     -3  -4.35%   x 1.05
 btree::set::clone_100_and_clear              1,833        1,776                 -57  -3.11%   x 1.03
 btree::set::clone_100_and_into_iter          1,820        1,764                 -56  -3.08%   x 1.03
 btree::set::clone_10k_and_remove_half        447,705      429,720           -17,985  -4.02%   x 1.04
 btree::set::intersection_random_10k_vs_100   2,153        2,085                 -68  -3.16%   x 1.03

@camelid camelid added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 1, 2020
library/alloc/src/collections/btree/node.rs Show resolved Hide resolved
BoxedNode::from_ptr(
self.node_as_mut().cast_unchecked::<marker::Internal>().first_edge().descend().node,
)
mem::replace(&mut internal_node.edges[0], MaybeUninit::uninit()).assume_init()
Copy link
Member

Choose a reason for hiding this comment

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

This feels like we're (potentially) doing needless work -- this looks equivalent to internal_node.edges[0].assume_init_read() which avoids the spurious write to edges -- can we use that instead?

(I suspect in practice LLVM probably ignores writing uninitialized data to some region of memory, but not sure).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed and I did the logical equivalent ptr::read on the other side.

@Mark-Simulacrum Mark-Simulacrum 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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 4, 2020
@ssomers
Copy link
Contributor Author

ssomers commented Oct 4, 2020

Swapped the 2 commits here and based on #77471 to avoid a benign conflict

@ssomers ssomers marked this pull request as draft October 5, 2020 11:24
@ssomers
Copy link
Contributor Author

ssomers commented Oct 5, 2020

Nothing wrong, just easier to follow if I do the first commit separately.

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 13, 2020
…ulacrum

BTreeMap: type-specific variants of node_as_mut and cast_unchecked

Improves debug checking and shortens some expressions. Extracted from rust-lang#77408
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 13, 2020
…ulacrum

BTreeMap: type-specific variants of node_as_mut and cast_unchecked

Improves debug checking and shortens some expressions. Extracted from rust-lang#77408
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 13, 2020
…ulacrum

BTreeMap: type-specific variants of node_as_mut and cast_unchecked

Improves debug checking and shortens some expressions. Extracted from rust-lang#77408
@ssomers ssomers changed the title BTreeMap: avoid sneaky clones of BoxedNode BTreeMap: reuse BoxedNode instances directly instead of their contents Oct 14, 2020
@ssomers ssomers marked this pull request as ready for review October 14, 2020 10:29
@ssomers
Copy link
Contributor Author

ssomers commented Oct 14, 2020

Still showing the same improvement, but now with the opposite on sets.

 name                                         old ns/iter  new ns/iter  diff ns/iter   diff %  speedup
 btree::map::clone_slim_100                   2,043        2,106                  63    3.08%   x 0.97
 btree::map::clone_slim_100_and_clear         2,551        2,065                -486  -19.05%   x 1.24
 btree::map::clone_slim_100_and_drain_all     3,891        3,441                -450  -11.57%   x 1.13
 btree::map::clone_slim_100_and_drain_half    3,351        2,844                -507  -15.13%   x 1.18
 btree::map::clone_slim_100_and_into_iter     2,587        2,045                -542  -20.95%   x 1.27
 btree::map::clone_slim_100_and_pop_all       3,684        3,189                -495  -13.44%   x 1.16
 btree::map::clone_slim_100_and_remove_all    4,515        4,044                -471  -10.43%   x 1.12
 btree::map::clone_slim_100_and_remove_half   3,388        2,823                -565  -16.68%   x 1.20
 btree::set::clone_100_and_remove_all         3,397        3,792                 395   11.63%   x 0.90
 btree::set::clone_10k_and_remove_all         400,900      436,475            35,575    8.87%   x 0.92

@ssomers
Copy link
Contributor Author

ssomers commented Oct 16, 2020

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 16, 2020
@Mark-Simulacrum
Copy link
Member

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Oct 19, 2020

📌 Commit 184735a has been approved by Mark-Simulacrum

@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, 2020
@Mark-Simulacrum
Copy link
Member

Oh, hm, I just saw that #78104 lists itself as a better alternative to this. @bors r-

@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 Oct 20, 2020
@ssomers
Copy link
Contributor Author

ssomers commented Oct 20, 2020

Well I actually rebased #77244 on this anticipating it would arrive soon, so you approved it again…

It's mostly a logical alternative, explicitly not reusing rather than explicitly reusing, but it's easy enough to let #78104 overwrite this little change.

@Mark-Simulacrum
Copy link
Member

Okay. I don't mind the extra PR. @bors r+

@bors
Copy link
Contributor

bors commented Oct 20, 2020

📌 Commit 184735a has been approved by Mark-Simulacrum

@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 Oct 20, 2020
@bors
Copy link
Contributor

bors commented Oct 21, 2020

⌛ Testing commit 184735a with merge 8682c9fe0f1a3307f310ee5e5b4a66f93c5f9466...

@ssomers
Copy link
Contributor Author

ssomers commented Oct 21, 2020

#77244 was rebased on the same changes and made it through first. I wondered what bors would do and as far as I can tell it just turned away in disgust. The commit in #77244 is a different SHA-1 though, because it was rebased too.

@ssomers ssomers closed this Oct 21, 2020
@ssomers ssomers deleted the btree_cleanup_6 branch October 22, 2020 08:13
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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants