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

Move query accessor code into functions #107802

Closed
wants to merge 1 commit into from
Closed

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Feb 8, 2023

This reduces the amount of code generated by the macros, which I think is the last item on #96524.

It also improves performance.

BenchmarkBeforeAfter
TimeTime%
🟣 clap:check1.8271s1.8163s -0.59%
🟣 hyper:check0.2611s0.2597s -0.57%
🟣 regex:check1.0206s1.0124s -0.80%
🟣 syn:check1.6329s1.6158s💚 -1.05%
🟣 syntex_syntax:check6.3299s6.2816s -0.76%
Total11.0717s10.9858s -0.78%
Summary1.0000s0.9925s -0.75%

r? @cjgillot

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) 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 Feb 8, 2023
@cjgillot
Copy link
Contributor

cjgillot commented Feb 8, 2023

@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 8, 2023
@bors
Copy link
Contributor

bors commented Feb 8, 2023

⌛ Trying commit 1fb91b54f117c61f166856ecaf0caca83f84bd03 with merge 203f4baf05d15e06f81a0776f7b923a3f6a84c41...

@bors
Copy link
Contributor

bors commented Feb 8, 2023

☀️ Try build successful - checks-actions
Build commit: 203f4baf05d15e06f81a0776f7b923a3f6a84c41 (203f4baf05d15e06f81a0776f7b923a3f6a84c41)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (203f4baf05d15e06f81a0776f7b923a3f6a84c41): comparison URL.

Overall result: ❌✅ regressions and improvements - 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.

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.2%, 0.8%] 8
Regressions ❌
(secondary)
5.1% [1.2%, 6.2%] 7
Improvements ✅
(primary)
-1.4% [-1.9%, -0.5%] 3
Improvements ✅
(secondary)
-3.7% [-4.2%, -3.2%] 6
All ❌✅ (primary) -0.1% [-1.9%, 0.8%] 11

Max RSS (memory usage)

Results

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

Cycles

Results

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)
11.3% [10.3%, 12.3%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 8, 2023
@cjgillot
Copy link
Contributor

cjgillot commented Feb 9, 2023

Both benchmarks and bootstrap timing show a regression. Do you have an idea why? How can this be avoided?

@Zoxc
Copy link
Contributor Author

Zoxc commented Feb 9, 2023

There's a 0.28% increase in code size which is likely LLVM inlining more and regressing the compile time of the rustc crates. The match-stress benchmark does seem to be quite flaky. I don't think that one is meaningful. It seems to regress when compiling with 1 CGU too.

}

#[inline(always)]
fn query_ensure<'tcx, Cache, K>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be merged with query_get_at by adding a QueryMode parameter and returning an Option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change was a regression:

BenchmarkBeforeAfter
TimeTime%
🟣 clap:check1.7607s1.7700s 0.52%
🟣 hyper:check0.2565s0.2576s 0.44%
🟣 regex:check0.9807s0.9862s 0.56%
🟣 syn:check1.5808s1.5889s 0.51%
🟣 syntex_syntax:check6.1378s6.1567s 0.31%
Total10.7165s10.7594s 0.40%
Summary1.0000s1.0047s 0.47%

#[inline(always)]
fn query_get_at<'tcx, Cache, K>(
tcx: TyCtxt<'tcx>,
execute_query: fn(
Copy link
Contributor

Choose a reason for hiding this comment

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

The regression probably comes from here: an indirect call (fn pointer) to an indirect call (dyn QueryEngine).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's only 1 indirect call, same as before.

Copy link
Contributor

Choose a reason for hiding this comment

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

How?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually you're right. LLVM will inline it anyway though as query_get_at is #[inline(always)] so it will discover the function it points to.

Cache::Stored: Copy,
Cache: QueryCache,
{
let key = key.into_query_param();
Copy link
Contributor

Choose a reason for hiding this comment

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

Moving this to the caller will make query_get_at a bit less generic, hopefully improving compile time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change was performance neutral and rustc_driver size increased by 0.03%, which is not a good sign for compile time.

BenchmarkBeforeAfter
TimeTime%
🟣 clap:check1.7610s1.7607s -0.02%
🟣 hyper:check0.2569s0.2570s 0.07%
🟣 regex:check0.9808s0.9815s 0.07%
🟣 syn:check1.5819s1.5814s -0.03%
🟣 syntex_syntax:check6.1439s6.1434s -0.01%
Total10.7246s10.7241s -0.00%
Summary1.0000s1.0002s 0.02%

I've pushed it so we can see what perf says.

@cjgillot
Copy link
Contributor

@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 10, 2023
@bors
Copy link
Contributor

bors commented Feb 10, 2023

⌛ Trying commit e046b4ca1ef602fd8379bb93a75f7864fb297400 with merge 8bdd2e2179ccbdd8546274274ab39ff27d50aef2...

@bors
Copy link
Contributor

bors commented Feb 11, 2023

☀️ Try build successful - checks-actions
Build commit: 8bdd2e2179ccbdd8546274274ab39ff27d50aef2 (8bdd2e2179ccbdd8546274274ab39ff27d50aef2)

1 similar comment
@bors
Copy link
Contributor

bors commented Feb 11, 2023

☀️ Try build successful - checks-actions
Build commit: 8bdd2e2179ccbdd8546274274ab39ff27d50aef2 (8bdd2e2179ccbdd8546274274ab39ff27d50aef2)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8bdd2e2179ccbdd8546274274ab39ff27d50aef2): comparison URL.

Overall result: ❌✅ regressions and improvements - 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.

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.6% [0.3%, 0.9%] 7
Regressions ❌
(secondary)
5.8% [5.4%, 6.2%] 6
Improvements ✅
(primary)
-0.4% [-0.5%, -0.3%] 3
Improvements ✅
(secondary)
-0.5% [-0.5%, -0.4%] 4
All ❌✅ (primary) 0.3% [-0.5%, 0.9%] 10

Max RSS (memory usage)

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

Cycles

Results

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)
8.0% [3.5%, 11.3%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 11, 2023
@Zoxc
Copy link
Contributor Author

Zoxc commented Feb 11, 2023

I changed the code to use self.at again as it was a performance win:

BenchmarkBeforeAfter
TimeTime%
🟣 clap:check1.7640s1.7568s -0.41%
🟣 hyper:check0.2574s0.2562s -0.44%
🟣 regex:check0.9856s0.9826s -0.31%
🟣 syn:check1.5816s1.5752s -0.41%
🟣 syntex_syntax:check6.1438s6.1225s -0.35%
Total10.7325s10.6933s -0.36%
Summary1.0000s0.9962s -0.38%

The overall change to this PR seems to improve bitmaps locally:

BenchmarkBeforeAfter
TimeTime%
🟣 bitmaps:check0.8432s0.8145s💚 -3.40%
Total0.8432s0.8145s💚 -3.40%
Summary1.0000s0.9660s💚 -3.40%

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 9, 2023
@bors
Copy link
Contributor

bors commented Mar 12, 2023

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

@Zoxc Zoxc mentioned this pull request Mar 25, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 29, 2023
Remove `QueryEngine` trait

This removes the `QueryEngine` trait and `Queries` from `rustc_query_impl` and replaced them with function pointers and fields in `QuerySystem`. As a side effect `OnDiskCache` is moved back into `rustc_middle` and the `OnDiskCache` trait is also removed.

This has a couple of benefits.
- `TyCtxt` is used in the query system instead of the removed `QueryCtxt` which is larger.
- Function pointers are more flexible to work with. A variant of rust-lang#107802 is included which avoids the double indirection. For rust-lang#108938 we can name entry point `__rust_end_short_backtrace` to avoid some overhead. For rust-lang#108062 it avoids the duplicate `QueryEngine` structs.
- `QueryContext` now implements `DepContext` which avoids many `dep_context()` calls in `rustc_query_system`.
- The `rustc_driver` size is reduced by 0.33%, hopefully that means some bootstrap improvements.
- This avoids the unsafe code around the `QueryEngine` trait.

r? `@cjgillot`
@Zoxc
Copy link
Contributor Author

Zoxc commented Apr 30, 2023

This was mostly merged as part of #109611. The remaining attributes could use a perf run.

@cjgillot
Copy link
Contributor

@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 Apr 30, 2023
@bors
Copy link
Contributor

bors commented Apr 30, 2023

⌛ Trying commit b6720ad with merge 2aa83d2eb45fdf777462706ce8ac5e6e0407f4a4...

@bors
Copy link
Contributor

bors commented Apr 30, 2023

☀️ Try build successful - checks-actions
Build commit: 2aa83d2eb45fdf777462706ce8ac5e6e0407f4a4 (2aa83d2eb45fdf777462706ce8ac5e6e0407f4a4)

1 similar comment
@bors
Copy link
Contributor

bors commented Apr 30, 2023

☀️ Try build successful - checks-actions
Build commit: 2aa83d2eb45fdf777462706ce8ac5e6e0407f4a4 (2aa83d2eb45fdf777462706ce8ac5e6e0407f4a4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (2aa83d2eb45fdf777462706ce8ac5e6e0407f4a4): comparison URL.

Overall result: ✅ improvements - 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 is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.5% [0.5%, 0.5%] 1
Improvements ✅
(primary)
-0.5% [-2.8%, -0.2%] 31
Improvements ✅
(secondary)
-1.4% [-7.3%, -0.3%] 36
All ❌✅ (primary) -0.5% [-2.8%, -0.2%] 31

Max RSS (memory usage)

Results

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)
1.4% [0.5%, 3.0%] 8
Regressions ❌
(secondary)
2.5% [1.8%, 3.8%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.4% [0.5%, 3.0%] 8

Cycles

Results

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)
1.9% [1.8%, 2.1%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.9% [-3.1%, -2.6%] 2
All ❌✅ (primary) 1.9% [1.8%, 2.1%] 3

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Apr 30, 2023
@cjgillot
Copy link
Contributor

r=me with PR title updated

@Zoxc
Copy link
Contributor Author

Zoxc commented Apr 30, 2023

This no longer seems to be an improvement locally and the bootstrap cost is quite high, so I think I'll close this for now.

BenchmarkBeforeAfterBeforeAfter
TimeTime%MemoryMemory%
🟣 clap:check1.6276s1.6328s 0.32%86.36 MiB86.57 MiB 0.24%
🟣 hyper:check0.2446s0.2444s -0.08%63.39 MiB63.70 MiB 0.49%
🟣 regex:check0.9089s0.9124s 0.38%100.40 MiB100.73 MiB 0.32%
🟣 syn:check1.4859s1.4850s -0.06%122.92 MiB123.23 MiB 0.26%
🟣 syntex_syntax:check5.7065s5.7227s 0.28%329.77 MiB329.48 MiB -0.09%
Total9.9734s9.9971s 0.24%702.84 MiB703.71 MiB 0.12%
Summary1.0000s1.0017s 0.17%1 byte1.00 bytes 0.24%

@Zoxc Zoxc closed this Apr 30, 2023
@Zoxc Zoxc deleted the unmacro branch April 30, 2023 14:13
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Apr 30, 2023
Remove `QueryEngine` trait

This removes the `QueryEngine` trait and `Queries` from `rustc_query_impl` and replaced them with function pointers and fields in `QuerySystem`. As a side effect `OnDiskCache` is moved back into `rustc_middle` and the `OnDiskCache` trait is also removed.

This has a couple of benefits.
- `TyCtxt` is used in the query system instead of the removed `QueryCtxt` which is larger.
- Function pointers are more flexible to work with. A variant of rust-lang/rust#107802 is included which avoids the double indirection. For rust-lang/rust#108938 we can name entry point `__rust_end_short_backtrace` to avoid some overhead. For rust-lang/rust#108062 it avoids the duplicate `QueryEngine` structs.
- `QueryContext` now implements `DepContext` which avoids many `dep_context()` calls in `rustc_query_system`.
- The `rustc_driver` size is reduced by 0.33%, hopefully that means some bootstrap improvements.
- This avoids the unsafe code around the `QueryEngine` trait.

r? `@cjgillot`
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 20, 2024
Remove `QueryEngine` trait

This removes the `QueryEngine` trait and `Queries` from `rustc_query_impl` and replaced them with function pointers and fields in `QuerySystem`. As a side effect `OnDiskCache` is moved back into `rustc_middle` and the `OnDiskCache` trait is also removed.

This has a couple of benefits.
- `TyCtxt` is used in the query system instead of the removed `QueryCtxt` which is larger.
- Function pointers are more flexible to work with. A variant of rust-lang/rust#107802 is included which avoids the double indirection. For rust-lang/rust#108938 we can name entry point `__rust_end_short_backtrace` to avoid some overhead. For rust-lang/rust#108062 it avoids the duplicate `QueryEngine` structs.
- `QueryContext` now implements `DepContext` which avoids many `dep_context()` calls in `rustc_query_system`.
- The `rustc_driver` size is reduced by 0.33%, hopefully that means some bootstrap improvements.
- This avoids the unsafe code around the `QueryEngine` trait.

r? `@cjgillot`
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
Remove `QueryEngine` trait

This removes the `QueryEngine` trait and `Queries` from `rustc_query_impl` and replaced them with function pointers and fields in `QuerySystem`. As a side effect `OnDiskCache` is moved back into `rustc_middle` and the `OnDiskCache` trait is also removed.

This has a couple of benefits.
- `TyCtxt` is used in the query system instead of the removed `QueryCtxt` which is larger.
- Function pointers are more flexible to work with. A variant of rust-lang/rust#107802 is included which avoids the double indirection. For rust-lang/rust#108938 we can name entry point `__rust_end_short_backtrace` to avoid some overhead. For rust-lang/rust#108062 it avoids the duplicate `QueryEngine` structs.
- `QueryContext` now implements `DepContext` which avoids many `dep_context()` calls in `rustc_query_system`.
- The `rustc_driver` size is reduced by 0.33%, hopefully that means some bootstrap improvements.
- This avoids the unsafe code around the `QueryEngine` trait.

r? `@cjgillot`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-blocked Status: Blocked on something else such as an RFC or other implementation work. 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