-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Use smaller discriminants for generators #69837
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,13 +86,13 @@ async fn await3_level5() -> u8 { | |
|
||
fn main() { | ||
assert_eq!(2, std::mem::size_of_val(&base())); | ||
assert_eq!(8, std::mem::size_of_val(&await1_level1())); | ||
assert_eq!(12, std::mem::size_of_val(&await2_level1())); | ||
assert_eq!(12, std::mem::size_of_val(&await3_level1())); | ||
assert_eq!(24, std::mem::size_of_val(&await3_level2())); | ||
assert_eq!(36, std::mem::size_of_val(&await3_level3())); | ||
assert_eq!(48, std::mem::size_of_val(&await3_level4())); | ||
assert_eq!(60, std::mem::size_of_val(&await3_level5())); | ||
assert_eq!(3, std::mem::size_of_val(&await1_level1())); | ||
assert_eq!(4, std::mem::size_of_val(&await2_level1())); | ||
assert_eq!(5, std::mem::size_of_val(&await3_level1())); | ||
assert_eq!(8, std::mem::size_of_val(&await3_level2())); | ||
assert_eq!(11, std::mem::size_of_val(&await3_level3())); | ||
assert_eq!(14, std::mem::size_of_val(&await3_level4())); | ||
assert_eq!(17, std::mem::size_of_val(&await3_level5())); | ||
Comment on lines
+89
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much, much better :D |
||
|
||
assert_eq!(1, wait(base())); | ||
assert_eq!(1, wait(await1_level1())); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//! Tests that generator discriminant sizes and ranges are chosen optimally and that they are | ||
//! reflected in the output of `mem::discriminant`. | ||
|
||
// run-pass | ||
|
||
#![feature(generators, generator_trait, core_intrinsics)] | ||
|
||
use std::intrinsics::discriminant_value; | ||
use std::marker::Unpin; | ||
use std::mem::size_of_val; | ||
use std::{cmp, ops::*}; | ||
|
||
macro_rules! yield25 { | ||
($e:expr) => { | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
|
||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
|
||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
|
||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
|
||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
yield $e; | ||
}; | ||
} | ||
|
||
/// Yields 250 times. | ||
macro_rules! yield250 { | ||
() => { | ||
yield250!(()) | ||
}; | ||
|
||
($e:expr) => { | ||
yield25!($e); | ||
yield25!($e); | ||
yield25!($e); | ||
yield25!($e); | ||
yield25!($e); | ||
|
||
yield25!($e); | ||
yield25!($e); | ||
yield25!($e); | ||
yield25!($e); | ||
yield25!($e); | ||
}; | ||
} | ||
|
||
fn cycle(gen: impl Generator<()> + Unpin, expected_max_discr: u64) { | ||
let mut gen = Box::pin(gen); | ||
let mut max_discr = 0; | ||
loop { | ||
max_discr = cmp::max(max_discr, discriminant_value(gen.as_mut().get_mut())); | ||
match gen.as_mut().resume(()) { | ||
GeneratorState::Yielded(_) => {} | ||
GeneratorState::Complete(_) => { | ||
assert_eq!(max_discr, expected_max_discr); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
// Has only one invalid discr. value. | ||
let gen_u8_tiny_niche = || { | ||
|| { | ||
// 3 reserved variants | ||
|
||
yield250!(); // 253 variants | ||
|
||
yield; // 254 | ||
yield; // 255 | ||
} | ||
}; | ||
|
||
// Uses all values in the u8 discriminant. | ||
let gen_u8_full = || { | ||
|| { | ||
// 3 reserved variants | ||
|
||
yield250!(); // 253 variants | ||
|
||
yield; // 254 | ||
yield; // 255 | ||
yield; // 256 | ||
} | ||
}; | ||
|
||
// Barely needs a u16 discriminant. | ||
let gen_u16 = || { | ||
|| { | ||
// 3 reserved variants | ||
|
||
yield250!(); // 253 variants | ||
|
||
yield; // 254 | ||
yield; // 255 | ||
yield; // 256 | ||
yield; // 257 | ||
} | ||
}; | ||
|
||
assert_eq!(size_of_val(&gen_u8_tiny_niche()), 1); | ||
assert_eq!(size_of_val(&Some(gen_u8_tiny_niche())), 1); // uses niche | ||
assert_eq!(size_of_val(&Some(Some(gen_u8_tiny_niche()))), 2); // cannot use niche anymore | ||
assert_eq!(size_of_val(&gen_u8_full()), 1); | ||
assert_eq!(size_of_val(&Some(gen_u8_full())), 2); // cannot use niche | ||
assert_eq!(size_of_val(&gen_u16()), 2); | ||
assert_eq!(size_of_val(&Some(gen_u16())), 2); // uses niche | ||
|
||
cycle(gen_u8_tiny_niche(), 254); | ||
cycle(gen_u8_full(), 255); | ||
cycle(gen_u16(), 256); | ||
Comment on lines
+123
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great test. The discriminant checks are dependent on implementation details of the meaning of each value, but that's fine - we can update the test if those change. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,6 @@ fn main() { | |
|
||
// Neither of these generators have the resume arg live across the `yield`, so they should be | ||
// 4 Bytes in size (only storing the discriminant) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonas-schievink Looks like you forgot to update the comment here. |
||
assert_eq!(size_of_val(&gen_copy), 4); | ||
assert_eq!(size_of_val(&gen_move), 4); | ||
assert_eq!(size_of_val(&gen_copy), 1); | ||
assert_eq!(size_of_val(&gen_move), 1); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.