-
Notifications
You must be signed in to change notification settings - Fork 13.3k
transmute: Mark edges by byte sets, not byte values #140168
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
Conversation
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
transmute: Mark edges by byte sets, not byte values This leads to drastic performance improvements. For example, on the author's 2024 MacBook Pro, the time to convert the `Tree` representation of a `u64` to its equivalent DFA representation drops from ~8.5ms to ~1us, a reduction of ~8,500x. See `bench_dfa_from_tree`. Similarly, the time to execute a transmutability query from `u64` to `u64` drops from ~35us to ~1.7us, a reduction of ~20x. See `bench_transmute`. r? `@jswrenn`
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
transmute: Mark edges by byte sets, not byte values This leads to drastic performance improvements. For example, on the author's 2024 MacBook Pro, the time to convert the `Tree` representation of a `u64` to its equivalent DFA representation drops from ~8.5ms to ~1us, a reduction of ~8,500x. See `bench_dfa_from_tree`. Similarly, the time to execute a transmutability query from `u64` to `u64` drops from ~35us to ~1.7us, a reduction of ~20x. See `bench_transmute`. r? `@jswrenn`
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (a0d017e): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking 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 @bors rollup=never Instruction countThis 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.
Max RSS (memory usage)Results (primary 0.0%, secondary 0.2%)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.
CyclesResults (secondary -2.3%)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.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 774.578s -> 775.487s (0.12%) |
} | ||
|
||
pub(crate) fn refs_from(&self, start: State) -> Option<&Map<R, State>> { | ||
Some(&self.transitions.get(&start)?.ref_transitions) | ||
pub(crate) fn iter_bytes_from(&self, start: State) -> impl Iterator<Item = (Byte, State)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would lightly prefer dropping the iter_
prefix, so long as there's no conflict between this and a hypothetical bytes_from
method that doesn't return an iter:
pub(crate) fn iter_bytes_from(&self, start: State) -> impl Iterator<Item = (Byte, State)> { | |
pub(crate) fn iter_bytes_from(&self, start: State) -> impl Iterator<Item = (Byte, State)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
use crate::layout::Byte; | ||
|
||
#[derive(Eq, PartialEq, Copy, Clone, Debug)] | ||
pub(super) struct Run<S> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Self { start: *range.start(), end: *range.end(), dst } | ||
} | ||
|
||
pub(super) fn from_closed_open(range: Range<u16>, dst: S) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from_inclusive_exclusive
, perhaps? that nomenclature is consistent with https://doc.rust-lang.org/stable/std/ops/enum.Bound.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
idx >= u16::from(self.start) && idx <= u16::from(self.end) | ||
} | ||
|
||
pub(super) fn as_closed_open(&self) -> (u16, u16) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same naming note here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
self.uninit | ||
} | ||
|
||
pub(crate) fn map<SS>(self, mut f: impl FnMut(S) -> SS) -> EdgeSet<SS> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_states
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -62,6 +101,21 @@ impl Ref for ! { | |||
} | |||
} | |||
|
|||
#[cfg(test)] | |||
impl<const N: usize> Ref for [u8; N] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this [(); N]
, since the bytes do not currently encode anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, alternatively, do what we do for Def
: define a named type, Ref
, that stands in for references in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went with the [(); N]
approach.
These commits modify the If this was unintentional then you should revert the changes before this PR is merged. |
Thanks! @matthiaskrgr these changes are limited to the performance of the experimental @bors r+ |
This comment has been minimized.
This comment has been minimized.
In the `Tree` and `Dfa` representations of a type's layout, store byte ranges rather than needing to separately store each byte value. This permits us to, for example, represent a `u8` using a single 0..=255 edge in the DFA rather than using 256 separate edges. This leads to drastic performance improvements. For example, on the author's 2024 MacBook Pro, the time to convert the `Tree` representation of a `u64` to its equivalent DFA representation drops from ~8.5ms to ~1us, a reduction of ~8,500x. See `bench_dfa_from_tree`. Similarly, the time to execute a transmutability query from `u64` to `u64` drops from ~35us to ~1.7us, a reduction of ~20x. See `bench_transmute`.
@bors r+ |
Scheduling: Prefer rollup=never PRs over non-rolled-up iffy PRs. @bors p=1 |
Regressions from the perf run are all real, so this doesn't seem to inluence only |
☀️ Test successful - checks-actions |
What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing c02a4f0 (parent) -> 7f69523 (this PR) Test differencesShow 7 test diffsStage 1
Job group index Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard 7f695232a80fa1833e2282f2577c5e1ff066bf39 --output-dir test-dashboard And then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
Finished benchmarking commit (7f69523): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 0.1%, secondary 3.3%)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.
CyclesResults (primary -0.4%)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.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 774.94s -> 776.02s (0.14%) |
Looks like the regression is gone now, so nevermind then. |
// - If `assume.validity`, then we should | ||
// succeed because the user is responsible for | ||
// ensuring that the *specific* byte value | ||
// appearing at runtime is valid for the | ||
// destination type. When `assume.validity`, | ||
// `src_quantifier == | ||
// Quantifier::ThereExists`, so adding an | ||
// `Answer::Yes` has the effect of ensuring | ||
// that the "there exists" is always | ||
// satisfied. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, when assuming validity, query nevertheless ensured that there exists a value that is valid for both src and dst:
#![feature(transmutability)]
use std::mem::{Assume, TransmuteFrom};
pub fn is_transmutable<Src, Dst>()
where
Dst: TransmuteFrom<Src, { Assume::VALIDITY }>,
{
}
pub union MaybeUninitByte {
pub uninit: (),
pub byte: u8,
}
fn main() {
is_transmutable::<MaybeUninitByte, [u8; 2]>(); // previously rejected, now accepted
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect this to hit the Answer::No(Reason::DstIsTooBig)
branch here:
rust/compiler/rustc_transmute/src/maybe_transmutable/mod.rs
Lines 180 to 187 in 4326a44
} else if src_state == self.src.accept { | |
// extension: `size_of(Src) <= size_of(Dst)` | |
if let Some(dst_state_prime) = self.dst.get_uninit_edge_dst(dst_state) { | |
self.answer_memo(cache, src_state, dst_state_prime) | |
} else { | |
Answer::No(Reason::DstIsTooBig) | |
} | |
} else { |
...but that's clearly not happening. cc @joshlf
transmutability: uninit transition matches unit byte only The previous implementation was inconsistent about transitions that apply for an init byte. For example, when answering a query, an init byte could use corresponding init transition. Init byte could also use uninit transition, but only when the corresponding init transition was absent. This behaviour was incompatible with DFA union construction. Define an uninit transition to match an uninit byte only and update implementation accordingly. To describe that `Tree::uninit` is valid for any value, build an automaton that accepts any byte value. Additionally, represent byte ranges uniformly as a pair of integers to avoid special case for uninit byte. Fixes rust-lang#140337. Fixes rust-lang#140168 (comment). r? `@jswrenn` `@joshlf`
transmutability: uninit transition matches unit byte only The previous implementation was inconsistent about transitions that apply for an init byte. For example, when answering a query, an init byte could use corresponding init transition. Init byte could also use uninit transition, but only when the corresponding init transition was absent. This behaviour was incompatible with DFA union construction. Define an uninit transition to match an uninit byte only and update implementation accordingly. To describe that `Tree::uninit` is valid for any value, build an automaton that accepts any byte value. Additionally, represent byte ranges uniformly as a pair of integers to avoid special case for uninit byte. Fixes rust-lang#140337. Fixes rust-lang#140168 (comment). r? ``@jswrenn`` ``@joshlf``
transmutability: uninit transition matches unit byte only The previous implementation was inconsistent about transitions that apply for an init byte. For example, when answering a query, an init byte could use corresponding init transition. Init byte could also use uninit transition, but only when the corresponding init transition was absent. This behaviour was incompatible with DFA union construction. Define an uninit transition to match an uninit byte only and update implementation accordingly. To describe that `Tree::uninit` is valid for any value, build an automaton that accepts any byte value. Additionally, represent byte ranges uniformly as a pair of integers to avoid special case for uninit byte. Fixes rust-lang#140337. Fixes rust-lang#140168 (comment). r? ```@jswrenn``` ```@joshlf```
This leads to drastic performance improvements. For example, on the author's 2024 MacBook Pro, the time to convert the
Tree
representation of au64
to its equivalent DFA representation drops from ~8.5ms to ~1us, a reduction of ~8,500x. Seebench_dfa_from_tree
.Similarly, the time to execute a transmutability query from
u64
tou64
drops from ~35us to ~1.7us, a reduction of ~20x. Seebench_transmute
.r? @jswrenn