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

Allow arena_cache queries to return Option<&'tcx T> #135911

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

Zalathar
Copy link
Contributor

Currently, arena_cache queries always have to return &'tcx T1. This means that if an arena-cached query wants to return an optional value, it has to return &'tcx Option<T>, which has a few negative consequences:

  • It goes against normal Rust style, where Option<&T> is preferred over &Option<T>.
  • Callers that actually want an Option<&T> have to manually call .as_ref() on the query result.
  • When the query result is None, a full-sized Option<T> still needs to be stored in the arena.

This PR solves that problem by introducing a helper trait ArenaCached that is implemented for both &T and Option<&T>, and takes care of bridging between the provided type, the arena-allocated type, and the declared query return type.


To demonstrate that this works, I have converted the two existing arena-cached queries that currently return &Option<T>: mir_coroutine_witnesses and diagnostic_hir_wf_check. Only the query declarations need to be modified; existing providers and callers continue to work with the new query return type.

(My real goal is to apply this to coverage_ids_info, which will return Option as of #135873, but that PR hasn't landed yet.)

Footnotes

  1. Technically they could return other types that implement Deref, but it's hard to imagine this working well with anything other than &T.

@rustbot
Copy link
Collaborator

rustbot commented Jan 23, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
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. labels Jan 23, 2025
@rust-log-analyzer

This comment has been minimized.

@Zalathar
Copy link
Contributor Author

I don't expect to see a measurable reduction in memory use, but I'm curious.

@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 Jan 23, 2025
@bors
Copy link
Contributor

bors commented Jan 23, 2025

⌛ Trying commit 4c448d5 with merge e9e5291...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 23, 2025
Allow `arena_cache` queries to return `Option<&'tcx T>`

Currently, `arena_cache` queries always have to return `&'tcx T`[^deref]. This means that if an arena-cached query wants to return an optional value, it has to return `&'tcx Option<T>`, which has a few negative consequences:

- It goes against normal Rust style, where `Option<&T>` is preferred over `&Option<T>`.
- Callers that actually want an `Option<&T>` have to manually call `.as_ref()` on the query result.
- When the query result is `None`, a full-sized `Option<T>` still needs to be stored in the arena.

This PR solves that problem by introducing a helper trait `ArenaCached` that is implemented for both `&T` and `Option<&T>`, and takes care of bridging between the provided type, the arena-allocated type, and the declared query return type.

---

To demonstrate that this works, I have converted the two existing arena-cached queries that currently return `&Option<T>`: `mir_coroutine_witnesses` and `diagnostic_hir_wf_check`. Only the query declarations need to be modified; existing providers and callers continue to work with the new query return type.

(My real goal is to apply this to `coverage_ids_info`, which will return Option as of rust-lang#135873, but that PR hasn't landed yet.)

[^deref]: Technically they could return other types that implement `Deref`, but it's hard to imagine this working well with anything other than `&T`.
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Cool(TM)

r=me if perf is good

@bors
Copy link
Contributor

bors commented Jan 23, 2025

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e9e5291): comparison URL.

Overall result: no relevant changes - no action needed

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.

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

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 2.6%)

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)
2.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.6% [2.6%, 2.6%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 777.336s -> 777.188s (-0.02%)
Artifact size: 325.94 MiB -> 325.94 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 23, 2025
@Zalathar
Copy link
Contributor Author

As expected, no measurable impact other than noise, so I'll allow rollup.

@bors r=compiler-errors rollup=maybe

@bors
Copy link
Contributor

bors commented Jan 23, 2025

📌 Commit 4c448d5 has been approved by compiler-errors

It is now in the queue for this repository.

@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 Jan 23, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 24, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#135073 (Implement `ByteStr` and `ByteString` types)
 - rust-lang#135492 (Add missing check for async body when suggesting await on futures.)
 - rust-lang#135766 (handle global trait bounds defining assoc types)
 - rust-lang#135880 (Get rid of RunCompiler)
 - rust-lang#135908 (rustc_codegen_llvm: remove outdated asm-to-obj codegen note)
 - rust-lang#135911 (Allow `arena_cache` queries to return `Option<&'tcx T>`)
 - rust-lang#135920 (simplify parse_format::Parser::ws by using next_if)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ce2a316 into rust-lang:master Jan 24, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 24, 2025
@Zalathar Zalathar deleted the arena-cache-option branch January 24, 2025 05:05
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants