From 7c8284fd77ac6333616eed7c1298487cd3a2e12e Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 6 Jun 2019 09:32:09 +0100 Subject: [PATCH] Adjust inlining See #817 --- rand_core/CHANGELOG.md | 1 + rand_core/src/block.rs | 29 +++++++++++++++++++++++------ rand_hc/CHANGELOG.md | 1 + rand_hc/src/hc128.rs | 9 +++++++-- rand_isaac/CHANGELOG.md | 1 + rand_isaac/src/isaac.rs | 9 +++++++-- rand_isaac/src/isaac64.rs | 9 +++++++-- src/rngs/small.rs | 4 ++++ src/rngs/std.rs | 4 ++++ 9 files changed, 55 insertions(+), 12 deletions(-) diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index f30c36502a9..4542a3b6f9d 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.5.0] - 2019-06-06 - Enable testing with Miri and fix incorrect pointer usages (#779, #780, #781, #783, #784) - Rewrite `Error` type and adjust API (#800) +- Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64` ## [0.4.0] - 2019-01-24 - Disable the `std` feature by default (#702) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 6c5beca271a..e4f07e74bc7 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -131,6 +131,7 @@ impl fmt::Debug for BlockRng { impl BlockRng { /// Create a new `BlockRng` from an existing RNG implementing /// `BlockRngCore`. Results will be generated on first use. + #[inline] pub fn new(core: R) -> BlockRng{ let results_empty = R::Results::default(); BlockRng { @@ -145,18 +146,21 @@ impl BlockRng { /// If this is equal to or larger than the size of the result buffer then /// the buffer is "empty" and `generate()` must be called to produce new /// results. + #[inline(always)] pub fn index(&self) -> usize { self.index } /// Reset the number of available results. /// This will force a new set of results to be generated on next use. + #[inline] pub fn reset(&mut self) { self.index = self.results.as_ref().len(); } /// Generate a new set of results immediately, setting the index to the /// given value. + #[inline] pub fn generate_and_set(&mut self, index: usize) { assert!(index < self.results.as_ref().len()); self.core.generate(&mut self.results); @@ -167,7 +171,7 @@ impl BlockRng { impl> RngCore for BlockRng where ::Results: AsRef<[u32]> + AsMut<[u32]> { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { if self.index >= self.results.as_ref().len() { self.generate_and_set(0); @@ -178,7 +182,7 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> value } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { let read_u64 = |results: &[u32], index| { if cfg!(any(target_endian = "little")) { @@ -210,6 +214,7 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> } } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { let mut read_len = 0; while read_len < dest.len() { @@ -225,23 +230,26 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> } } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { - self.fill_bytes(dest); - Ok(()) + Ok(self.fill_bytes(dest)) } } impl SeedableRng for BlockRng { type Seed = R::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { Self::new(R::from_seed(seed)) } + #[inline(always)] fn seed_from_u64(seed: u64) -> Self { Self::new(R::seed_from_u64(seed)) } + #[inline(always)] fn from_rng(rng: S) -> Result { Ok(Self::new(R::from_rng(rng)?)) } @@ -296,6 +304,7 @@ impl fmt::Debug for BlockRng64 { impl BlockRng64 { /// Create a new `BlockRng` from an existing RNG implementing /// `BlockRngCore`. Results will be generated on first use. + #[inline] pub fn new(core: R) -> BlockRng64{ let results_empty = R::Results::default(); BlockRng64 { @@ -311,12 +320,14 @@ impl BlockRng64 { /// If this is equal to or larger than the size of the result buffer then /// the buffer is "empty" and `generate()` must be called to produce new /// results. + #[inline(always)] pub fn index(&self) -> usize { self.index } /// Reset the number of available results. /// This will force a new set of results to be generated on next use. + #[inline] pub fn reset(&mut self) { self.index = self.results.as_ref().len(); self.half_used = false; @@ -324,6 +335,7 @@ impl BlockRng64 { /// Generate a new set of results immediately, setting the index to the /// given value. + #[inline] pub fn generate_and_set(&mut self, index: usize) { assert!(index < self.results.as_ref().len()); self.core.generate(&mut self.results); @@ -335,7 +347,7 @@ impl BlockRng64 { impl> RngCore for BlockRng64 where ::Results: AsRef<[u64]> + AsMut<[u64]> { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { let mut index = self.index * 2 - self.half_used as usize; if index >= self.results.as_ref().len() * 2 { @@ -361,7 +373,7 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> } } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { if self.index >= self.results.as_ref().len() { self.core.generate(&mut self.results); @@ -374,6 +386,7 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> value } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { let mut read_len = 0; self.half_used = false; @@ -392,6 +405,7 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> } } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { Ok(self.fill_bytes(dest)) } @@ -400,14 +414,17 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> impl SeedableRng for BlockRng64 { type Seed = R::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { Self::new(R::from_seed(seed)) } + #[inline(always)] fn seed_from_u64(seed: u64) -> Self { Self::new(R::seed_from_u64(seed)) } + #[inline(always)] fn from_rng(rng: S) -> Result { Ok(Self::new(R::from_rng(rng)?)) } diff --git a/rand_hc/CHANGELOG.md b/rand_hc/CHANGELOG.md index 406b77de76e..2820275e1c7 100644 --- a/rand_hc/CHANGELOG.md +++ b/rand_hc/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.1.1] - 2019-06-06 - Bump `rand_core` version +- Adjust usage of `#[inline]` ## [0.1.0] - 2018-10-17 - Pulled out of the Rand crate diff --git a/rand_hc/src/hc128.rs b/rand_hc/src/hc128.rs index 6c55ec029f4..a320f48f441 100644 --- a/rand_hc/src/hc128.rs +++ b/rand_hc/src/hc128.rs @@ -67,20 +67,22 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv pub struct Hc128Rng(BlockRng); impl RngCore for Hc128Rng { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { self.0.next_u32() } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { self.0.next_u64() } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) } + #[inline] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -89,10 +91,12 @@ impl RngCore for Hc128Rng { impl SeedableRng for Hc128Rng { type Seed = ::Seed; + #[inline] fn from_seed(seed: Self::Seed) -> Self { Hc128Rng(BlockRng::::from_seed(seed)) } + #[inline] fn from_rng(rng: R) -> Result { BlockRng::::from_rng(rng).map(Hc128Rng) } @@ -268,6 +272,7 @@ impl Hc128Core { // Initialize an HC-128 random number generator. The seed has to be // 256 bits in length (`[u32; 8]`), matching the 128 bit `key` followed by // 128 bit `iv` when HC-128 where to be used as a stream cipher. + #[inline(always)] // single use: SeedableRng::from_seed fn init(seed: [u32; SEED_WORDS]) -> Self { #[inline] fn f1(x: u32) -> u32 { diff --git a/rand_isaac/CHANGELOG.md b/rand_isaac/CHANGELOG.md index 6333289e942..95de12a61f0 100644 --- a/rand_isaac/CHANGELOG.md +++ b/rand_isaac/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.1.2] - 2019-06-06 - Bump `rand_core` version - Remove deprecated code +- Adjust usage of `#[inline]` ## [0.1.1] - 2018-11-26 - Fix `rand_core` version requirement diff --git a/rand_isaac/src/isaac.rs b/rand_isaac/src/isaac.rs index d584a4fd3a0..f857737a599 100644 --- a/rand_isaac/src/isaac.rs +++ b/rand_isaac/src/isaac.rs @@ -92,20 +92,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; pub struct IsaacRng(BlockRng); impl RngCore for IsaacRng { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { self.0.next_u32() } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { self.0.next_u64() } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) } + #[inline] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -114,6 +116,7 @@ impl RngCore for IsaacRng { impl SeedableRng for IsaacRng { type Seed = ::Seed; + #[inline] fn from_seed(seed: Self::Seed) -> Self { IsaacRng(BlockRng::::from_seed(seed)) } @@ -121,10 +124,12 @@ impl SeedableRng for IsaacRng { /// Create an ISAAC random number generator using an `u64` as seed. /// If `seed == 0` this will produce the same stream of random numbers as /// the reference implementation when used unseeded. + #[inline] fn seed_from_u64(seed: u64) -> Self { IsaacRng(BlockRng::::seed_from_u64(seed)) } + #[inline] fn from_rng(rng: S) -> Result { BlockRng::::from_rng(rng).map(|rng| IsaacRng(rng)) } diff --git a/rand_isaac/src/isaac64.rs b/rand_isaac/src/isaac64.rs index f4cb3a0a1b3..902715ea934 100644 --- a/rand_isaac/src/isaac64.rs +++ b/rand_isaac/src/isaac64.rs @@ -83,20 +83,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; pub struct Isaac64Rng(BlockRng64); impl RngCore for Isaac64Rng { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { self.0.next_u32() } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { self.0.next_u64() } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) } + #[inline] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -105,6 +107,7 @@ impl RngCore for Isaac64Rng { impl SeedableRng for Isaac64Rng { type Seed = ::Seed; + #[inline] fn from_seed(seed: Self::Seed) -> Self { Isaac64Rng(BlockRng64::::from_seed(seed)) } @@ -112,10 +115,12 @@ impl SeedableRng for Isaac64Rng { /// Create an ISAAC random number generator using an `u64` as seed. /// If `seed == 0` this will produce the same stream of random numbers as /// the reference implementation when used unseeded. + #[inline] fn seed_from_u64(seed: u64) -> Self { Isaac64Rng(BlockRng64::::seed_from_u64(seed)) } + #[inline] fn from_rng(rng: S) -> Result { BlockRng64::::from_rng(rng).map(|rng| Isaac64Rng(rng)) } diff --git a/src/rngs/small.rs b/src/rngs/small.rs index 6a96338e802..1ffb2fbd8a8 100644 --- a/src/rngs/small.rs +++ b/src/rngs/small.rs @@ -89,10 +89,12 @@ impl RngCore for SmallRng { self.0.next_u64() } + #[inline(always)] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest); } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -101,10 +103,12 @@ impl RngCore for SmallRng { impl SeedableRng for SmallRng { type Seed = ::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { SmallRng(Rng::from_seed(seed)) } + #[inline(always)] fn from_rng(rng: R) -> Result { Rng::from_rng(rng).map(SmallRng) } diff --git a/src/rngs/std.rs b/src/rngs/std.rs index fe487786b1f..c9ea6ae84c0 100644 --- a/src/rngs/std.rs +++ b/src/rngs/std.rs @@ -44,10 +44,12 @@ impl RngCore for StdRng { self.0.next_u64() } + #[inline(always)] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest); } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -56,10 +58,12 @@ impl RngCore for StdRng { impl SeedableRng for StdRng { type Seed = ::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { StdRng(Rng::from_seed(seed)) } + #[inline(always)] fn from_rng(rng: R) -> Result { Rng::from_rng(rng).map(StdRng) }