Skip to content

Commit eda7ac5

Browse files
committed
Adjust inlining
See rust-random#817
1 parent 3bede5f commit eda7ac5

File tree

9 files changed

+55
-12
lines changed

9 files changed

+55
-12
lines changed

rand_core/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
- Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64`
78
## [0.4.0] - 2019-01-24
89
- Disable the `std` feature by default (#702)
910

rand_core/src/block.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
131131
impl<R: BlockRngCore> BlockRng<R> {
132132
/// Create a new `BlockRng` from an existing RNG implementing
133133
/// `BlockRngCore`. Results will be generated on first use.
134+
#[inline]
134135
pub fn new(core: R) -> BlockRng<R>{
135136
let results_empty = R::Results::default();
136137
BlockRng {
@@ -145,18 +146,21 @@ impl<R: BlockRngCore> BlockRng<R> {
145146
/// If this is equal to or larger than the size of the result buffer then
146147
/// the buffer is "empty" and `generate()` must be called to produce new
147148
/// results.
149+
#[inline(always)]
148150
pub fn index(&self) -> usize {
149151
self.index
150152
}
151153

152154
/// Reset the number of available results.
153155
/// This will force a new set of results to be generated on next use.
156+
#[inline]
154157
pub fn reset(&mut self) {
155158
self.index = self.results.as_ref().len();
156159
}
157160

158161
/// Generate a new set of results immediately, setting the index to the
159162
/// given value.
163+
#[inline]
160164
pub fn generate_and_set(&mut self, index: usize) {
161165
assert!(index < self.results.as_ref().len());
162166
self.core.generate(&mut self.results);
@@ -167,7 +171,7 @@ impl<R: BlockRngCore> BlockRng<R> {
167171
impl<R: BlockRngCore<Item=u32>> RngCore for BlockRng<R>
168172
where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
169173
{
170-
#[inline(always)]
174+
#[inline]
171175
fn next_u32(&mut self) -> u32 {
172176
if self.index >= self.results.as_ref().len() {
173177
self.generate_and_set(0);
@@ -178,7 +182,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
178182
value
179183
}
180184

181-
#[inline(always)]
185+
#[inline]
182186
fn next_u64(&mut self) -> u64 {
183187
let read_u64 = |results: &[u32], index| {
184188
if cfg!(any(target_endian = "little")) {
@@ -210,6 +214,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
210214
}
211215
}
212216

217+
#[inline]
213218
fn fill_bytes(&mut self, dest: &mut [u8]) {
214219
let mut read_len = 0;
215220
while read_len < dest.len() {
@@ -225,23 +230,26 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
225230
}
226231
}
227232

233+
#[inline(always)]
228234
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
229-
self.fill_bytes(dest);
230-
Ok(())
235+
Ok(self.fill_bytes(dest))
231236
}
232237
}
233238

234239
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
235240
type Seed = R::Seed;
236241

242+
#[inline(always)]
237243
fn from_seed(seed: Self::Seed) -> Self {
238244
Self::new(R::from_seed(seed))
239245
}
240246

247+
#[inline(always)]
241248
fn seed_from_u64(seed: u64) -> Self {
242249
Self::new(R::seed_from_u64(seed))
243250
}
244251

252+
#[inline(always)]
245253
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
246254
Ok(Self::new(R::from_rng(rng)?))
247255
}
@@ -296,6 +304,7 @@ impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
296304
impl<R: BlockRngCore> BlockRng64<R> {
297305
/// Create a new `BlockRng` from an existing RNG implementing
298306
/// `BlockRngCore`. Results will be generated on first use.
307+
#[inline]
299308
pub fn new(core: R) -> BlockRng64<R>{
300309
let results_empty = R::Results::default();
301310
BlockRng64 {
@@ -311,19 +320,22 @@ impl<R: BlockRngCore> BlockRng64<R> {
311320
/// If this is equal to or larger than the size of the result buffer then
312321
/// the buffer is "empty" and `generate()` must be called to produce new
313322
/// results.
323+
#[inline(always)]
314324
pub fn index(&self) -> usize {
315325
self.index
316326
}
317327

318328
/// Reset the number of available results.
319329
/// This will force a new set of results to be generated on next use.
330+
#[inline]
320331
pub fn reset(&mut self) {
321332
self.index = self.results.as_ref().len();
322333
self.half_used = false;
323334
}
324335

325336
/// Generate a new set of results immediately, setting the index to the
326337
/// given value.
338+
#[inline]
327339
pub fn generate_and_set(&mut self, index: usize) {
328340
assert!(index < self.results.as_ref().len());
329341
self.core.generate(&mut self.results);
@@ -335,7 +347,7 @@ impl<R: BlockRngCore> BlockRng64<R> {
335347
impl<R: BlockRngCore<Item=u64>> RngCore for BlockRng64<R>
336348
where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
337349
{
338-
#[inline(always)]
350+
#[inline]
339351
fn next_u32(&mut self) -> u32 {
340352
let mut index = self.index * 2 - self.half_used as usize;
341353
if index >= self.results.as_ref().len() * 2 {
@@ -361,7 +373,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
361373
}
362374
}
363375

364-
#[inline(always)]
376+
#[inline]
365377
fn next_u64(&mut self) -> u64 {
366378
if self.index >= self.results.as_ref().len() {
367379
self.core.generate(&mut self.results);
@@ -374,6 +386,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
374386
value
375387
}
376388

389+
#[inline]
377390
fn fill_bytes(&mut self, dest: &mut [u8]) {
378391
let mut read_len = 0;
379392
self.half_used = false;
@@ -392,6 +405,7 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
392405
}
393406
}
394407

408+
#[inline(always)]
395409
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
396410
Ok(self.fill_bytes(dest))
397411
}
@@ -400,14 +414,17 @@ where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
400414
impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
401415
type Seed = R::Seed;
402416

417+
#[inline(always)]
403418
fn from_seed(seed: Self::Seed) -> Self {
404419
Self::new(R::from_seed(seed))
405420
}
406421

422+
#[inline(always)]
407423
fn seed_from_u64(seed: u64) -> Self {
408424
Self::new(R::seed_from_u64(seed))
409425
}
410426

427+
#[inline(always)]
411428
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
412429
Ok(Self::new(R::from_rng(rng)?))
413430
}

rand_hc/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
- Adjust usage of `#[inline]`
78
## [0.1.0] - 2018-10-17
89
- Pulled out of the Rand crate

rand_hc/src/hc128.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,22 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv
6767
pub struct Hc128Rng(BlockRng<Hc128Core>);
6868

6969
impl RngCore for Hc128Rng {
70-
#[inline(always)]
70+
#[inline]
7171
fn next_u32(&mut self) -> u32 {
7272
self.0.next_u32()
7373
}
7474

75-
#[inline(always)]
75+
#[inline]
7676
fn next_u64(&mut self) -> u64 {
7777
self.0.next_u64()
7878
}
7979

80+
#[inline]
8081
fn fill_bytes(&mut self, dest: &mut [u8]) {
8182
self.0.fill_bytes(dest)
8283
}
8384

85+
#[inline]
8486
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
8587
self.0.try_fill_bytes(dest)
8688
}
@@ -89,10 +91,12 @@ impl RngCore for Hc128Rng {
8991
impl SeedableRng for Hc128Rng {
9092
type Seed = <Hc128Core as SeedableRng>::Seed;
9193

94+
#[inline]
9295
fn from_seed(seed: Self::Seed) -> Self {
9396
Hc128Rng(BlockRng::<Hc128Core>::from_seed(seed))
9497
}
9598

99+
#[inline]
96100
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
97101
BlockRng::<Hc128Core>::from_rng(rng).map(Hc128Rng)
98102
}
@@ -268,6 +272,7 @@ impl Hc128Core {
268272
// Initialize an HC-128 random number generator. The seed has to be
269273
// 256 bits in length (`[u32; 8]`), matching the 128 bit `key` followed by
270274
// 128 bit `iv` when HC-128 where to be used as a stream cipher.
275+
#[inline(always)] // single use: SeedableRng::from_seed
271276
fn init(seed: [u32; SEED_WORDS]) -> Self {
272277
#[inline]
273278
fn f1(x: u32) -> u32 {

rand_isaac/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
- Adjust usage of `#[inline]`
78
## [0.1.1] - 2018-11-26
89
- Fix `rand_core` version requirement
910
- Fix doc links

rand_isaac/src/isaac.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
9292
pub struct IsaacRng(BlockRng<IsaacCore>);
9393

9494
impl RngCore for IsaacRng {
95-
#[inline(always)]
95+
#[inline]
9696
fn next_u32(&mut self) -> u32 {
9797
self.0.next_u32()
9898
}
9999

100-
#[inline(always)]
100+
#[inline]
101101
fn next_u64(&mut self) -> u64 {
102102
self.0.next_u64()
103103
}
104104

105+
#[inline]
105106
fn fill_bytes(&mut self, dest: &mut [u8]) {
106107
self.0.fill_bytes(dest)
107108
}
108109

110+
#[inline]
109111
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
110112
self.0.try_fill_bytes(dest)
111113
}
@@ -114,17 +116,20 @@ impl RngCore for IsaacRng {
114116
impl SeedableRng for IsaacRng {
115117
type Seed = <IsaacCore as SeedableRng>::Seed;
116118

119+
#[inline]
117120
fn from_seed(seed: Self::Seed) -> Self {
118121
IsaacRng(BlockRng::<IsaacCore>::from_seed(seed))
119122
}
120123

121124
/// Create an ISAAC random number generator using an `u64` as seed.
122125
/// If `seed == 0` this will produce the same stream of random numbers as
123126
/// the reference implementation when used unseeded.
127+
#[inline]
124128
fn seed_from_u64(seed: u64) -> Self {
125129
IsaacRng(BlockRng::<IsaacCore>::seed_from_u64(seed))
126130
}
127131

132+
#[inline]
128133
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
129134
BlockRng::<IsaacCore>::from_rng(rng).map(|rng| IsaacRng(rng))
130135
}

rand_isaac/src/isaac64.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
8383
pub struct Isaac64Rng(BlockRng64<Isaac64Core>);
8484

8585
impl RngCore for Isaac64Rng {
86-
#[inline(always)]
86+
#[inline]
8787
fn next_u32(&mut self) -> u32 {
8888
self.0.next_u32()
8989
}
9090

91-
#[inline(always)]
91+
#[inline]
9292
fn next_u64(&mut self) -> u64 {
9393
self.0.next_u64()
9494
}
9595

96+
#[inline]
9697
fn fill_bytes(&mut self, dest: &mut [u8]) {
9798
self.0.fill_bytes(dest)
9899
}
99100

101+
#[inline]
100102
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
101103
self.0.try_fill_bytes(dest)
102104
}
@@ -105,17 +107,20 @@ impl RngCore for Isaac64Rng {
105107
impl SeedableRng for Isaac64Rng {
106108
type Seed = <Isaac64Core as SeedableRng>::Seed;
107109

110+
#[inline]
108111
fn from_seed(seed: Self::Seed) -> Self {
109112
Isaac64Rng(BlockRng64::<Isaac64Core>::from_seed(seed))
110113
}
111114

112115
/// Create an ISAAC random number generator using an `u64` as seed.
113116
/// If `seed == 0` this will produce the same stream of random numbers as
114117
/// the reference implementation when used unseeded.
118+
#[inline]
115119
fn seed_from_u64(seed: u64) -> Self {
116120
Isaac64Rng(BlockRng64::<Isaac64Core>::seed_from_u64(seed))
117121
}
118122

123+
#[inline]
119124
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
120125
BlockRng64::<Isaac64Core>::from_rng(rng).map(|rng| Isaac64Rng(rng))
121126
}

src/rngs/small.rs

+4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ impl RngCore for SmallRng {
8181
self.0.next_u64()
8282
}
8383

84+
#[inline(always)]
8485
fn fill_bytes(&mut self, dest: &mut [u8]) {
8586
self.0.fill_bytes(dest);
8687
}
8788

89+
#[inline(always)]
8890
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
8991
self.0.try_fill_bytes(dest)
9092
}
@@ -93,10 +95,12 @@ impl RngCore for SmallRng {
9395
impl SeedableRng for SmallRng {
9496
type Seed = <Rng as SeedableRng>::Seed;
9597

98+
#[inline(always)]
9699
fn from_seed(seed: Self::Seed) -> Self {
97100
SmallRng(Rng::from_seed(seed))
98101
}
99102

103+
#[inline(always)]
100104
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
101105
Rng::from_rng(rng).map(SmallRng)
102106
}

src/rngs/std.rs

+4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ impl RngCore for StdRng {
3939
self.0.next_u64()
4040
}
4141

42+
#[inline(always)]
4243
fn fill_bytes(&mut self, dest: &mut [u8]) {
4344
self.0.fill_bytes(dest);
4445
}
4546

47+
#[inline(always)]
4648
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
4749
self.0.try_fill_bytes(dest)
4850
}
@@ -51,10 +53,12 @@ impl RngCore for StdRng {
5153
impl SeedableRng for StdRng {
5254
type Seed = <Hc128Rng as SeedableRng>::Seed;
5355

56+
#[inline(always)]
5457
fn from_seed(seed: Self::Seed) -> Self {
5558
StdRng(Hc128Rng::from_seed(seed))
5659
}
5760

61+
#[inline(always)]
5862
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
5963
Hc128Rng::from_rng(rng).map(StdRng)
6064
}

0 commit comments

Comments
 (0)