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

Make Rc<T>::deref and Arc<T>::deref zero-cost #132553

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

EFanZh
Copy link
Contributor

@EFanZh EFanZh commented Nov 3, 2024

Currently, Rc<T> and Arc<T> store pointers to RcInner<T> and ArcInner<T>. This PR changes the pointers so that they point to T directly instead.

This is based on the assumption that we access the T value more frequently than accessing reference counts. With this change, accessing the data can be done without offsetting pointers from RcInner<T> and ArcInner<T> to their contained data. This change might also enables some possibly useful future optimizations, such as:

  • Convert &[Rc<T>] into &[&T] within O(1) time.
  • Convert &[Rc<T>] into Vec<&T> utilizing memcpy.
  • Convert &Option<Rc<T>> into Option<&T> without branching.
  • Make Rc<T> and Arc<T> FFI compatible types where T: Sized.

@rustbot
Copy link
Collaborator

rustbot commented Nov 3, 2024

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 3, 2024
@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from b283c44 to ae36f44 Compare November 3, 2024 09:14
@rust-log-analyzer

This comment has been minimized.

@marmeladema
Copy link
Contributor

Would it potentially enable those types to have an ffi compatible ABI? So that they could be returned and passed directly from /to ffi function, like Box?

@rust-log-analyzer

This comment has been minimized.

@EFanZh
Copy link
Contributor Author

EFanZh commented Nov 3, 2024

Would it potentially enable those types to have an ffi compatible ABI? So that they could be returned and passed directly from /to ffi function, like Box?

I think in theory it is possible, at least for sized types, but I am not familiar with how to formally make it so.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from ae36f44 to 0d6165f Compare November 3, 2024 11:21
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 0d6165f to 98edd5b Compare November 3, 2024 13:06
@rust-log-analyzer

This comment has been minimized.

@jhpratt
Copy link
Member

jhpratt commented Nov 3, 2024

r? libs

@rustbot rustbot assigned joboet and unassigned jhpratt Nov 3, 2024
@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 98edd5b to 8beb51d Compare November 4, 2024 16:29
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 8beb51d to d7879fa Compare November 4, 2024 17:26
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from d7879fa to 317aa0e Compare November 4, 2024 18:40
@joboet
Copy link
Member

joboet commented Nov 7, 2024

@EFanZh Is this ready for review? If so, please un-draft the PR.

@EFanZh
Copy link
Contributor Author

EFanZh commented Nov 7, 2024

@joboet: The source code part is mostly done, but I haven’t finished updating LLDB and CDB pretty printers. The CI doesn’t seem to run those tests.

@joboet
Copy link
Member

joboet commented Nov 8, 2024

No worries! I just didn't want to keep you waiting in case you had forgotten to change the state.
@rustbot author

@rustbot rustbot 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 Nov 8, 2024
@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch 3 times, most recently from f243654 to 1308bf6 Compare November 11, 2024 18:35
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 873f27a to 454eccf Compare January 6, 2025 15:57
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch 2 times, most recently from e37e38b to 10a7b54 Compare January 10, 2025 13:33
@bors
Copy link
Contributor

bors commented Jan 11, 2025

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

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 10a7b54 to 5cf72c8 Compare January 11, 2025 13:45
@bors
Copy link
Contributor

bors commented Jan 12, 2025

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

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 5cf72c8 to 8591208 Compare January 19, 2025 11:04
@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 8591208 to 9c7e4ba Compare February 5, 2025 15:44
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 9c7e4ba to e072242 Compare February 5, 2025 18:02
@rust-log-analyzer

This comment has been minimized.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from e072242 to 5f9be42 Compare February 5, 2025 19:03
@EFanZh
Copy link
Contributor Author

EFanZh commented Feb 6, 2025

Hi @joboet: Can I request a perf run to see the impact of this change?

@joboet
Copy link
Member

joboet commented Feb 6, 2025

Sure thing!
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 6, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2025
Make `Rc<T>::deref` and `Arc<T>::deref` zero-cost

Currently, `Rc<T>` and `Arc<T>` store pointers to `RcInner<T>` and `ArcInner<T>`. This PR changes the pointers so that they point to `T` directly instead.

This is based on the assumption that we access the `T` value more frequently than accessing reference counts. With this change, accessing the data can be done without offsetting pointers from `RcInner<T>` and `ArcInner<T>` to their contained data. This change might also enables some possibly useful future optimizations, such as:

- Convert `&[Rc<T>]` into `&[&T]` within O(1) time.
- Convert `&[Rc<T>]` into `Vec<&T>` utilizing `memcpy`.
- Convert `&Option<Rc<T>>` into `Option<&T>` without branching.
- Make `Rc<T>` and `Arc<T>` FFI compatible types where `T: Sized`.
@bors
Copy link
Contributor

bors commented Feb 6, 2025

⌛ Trying commit 5f9be42 with merge 00d0a00...

@bors
Copy link
Contributor

bors commented Feb 6, 2025

☀️ Try build successful - checks-actions
Build commit: 00d0a00 (00d0a00da6ba18a6278cac0a20bafe752a181ad4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (00d0a00): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.5% [0.2%, 1.1%] 22
Regressions ❌
(secondary)
1.8% [0.3%, 4.6%] 25
Improvements ✅
(primary)
-1.0% [-2.2%, -0.4%] 3
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) 0.3% [-2.2%, 1.1%] 25

Max RSS (memory usage)

Results (primary 0.7%, secondary -0.5%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.5% [1.5%, 5.6%] 7
Regressions ❌
(secondary)
2.8% [2.0%, 4.8%] 4
Improvements ✅
(primary)
-3.3% [-8.4%, -1.0%] 5
Improvements ✅
(secondary)
-2.6% [-4.0%, -1.1%] 6
All ❌✅ (primary) 0.7% [-8.4%, 5.6%] 12

Cycles

Results (primary -2.1%, secondary 3.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.0% [1.7%, 4.1%] 16
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.1% [-2.1%, -2.1%] 1

Binary size

Results (primary 0.4%, secondary 1.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.0%, 1.8%] 88
Regressions ❌
(secondary)
1.1% [0.3%, 9.1%] 82
Improvements ✅
(primary)
-0.5% [-1.3%, -0.2%] 5
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) 0.4% [-1.3%, 1.8%] 93

Bootstrap: 778.16s -> 777.103s (-0.14%)
Artifact size: 329.00 MiB -> 329.08 MiB (0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 6, 2025
@scottmcm
Copy link
Member

scottmcm commented Feb 9, 2025

Hmm, odd to see both debug and opt binary sizes are bigger with this. Maybe try to find the cause there? Might just be a missing #[inline] somewhere...

@EFanZh
Copy link
Contributor Author

EFanZh commented Feb 9, 2025

@scottmcm: I’m working on it, but it may take some time since it’s being done in my free time.

@EFanZh EFanZh force-pushed the zero-cost-rc-arc-deref branch from 5f9be42 to 88a2d49 Compare February 9, 2025 08:08
@bors
Copy link
Contributor

bors commented Feb 9, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.