Skip to content

Commit 47a23ab

Browse files
committed
Auto merge of #283 - Zoxc:inline-tweaks, r=Amanieu
Inline small functions This adds `#[inline]` to small functions which should be beneficial to inline. rustc compilation performance (code size of `rustc_driver` up by 0.09%): ``` clap:check 1.9486s 1.9416s -0.36% hashmap-instances:check 0.0629s 0.0626s -0.52% helloworld:check 0.0443s 0.0439s -0.69% hyper:check 0.3011s 0.3000s -0.36% regex:check 1.1505s 1.1468s -0.33% syn:check 1.6989s 1.6904s -0.50% syntex_syntax:check 6.8479s 6.8288s -0.28% winapi:check 8.3437s 8.2967s -0.56% Total 20.3979s 20.3108s -0.43% Summary 4.0000s 3.9820s -0.45% ``` This is the effect on compile time this has on my [HashMap compile time benchmark](#277 (comment)): ``` hashmap-instances:check 0.0635s 0.0632s -0.33% hashmap-instances:release 32.0928s 32.4440s +1.09% hashmap-instances:debug 7.2193s 7.2800s +0.84% Total 39.3756s 39.7873s +1.05% Summary 1.5000s 1.5080s +0.54% ``` We saw a 1.6% improvement in rustc's build time for a -20% improvement on `hashmap-instances:release` on rust-lang/rust#87233. So I would expect around a 0.08% regression for rustc's build time from this PR.
2 parents 4aca8e4 + b50ecce commit 47a23ab

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/raw/mod.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ fn unlikely(b: bool) -> bool {
7373
}
7474

7575
#[cfg(feature = "nightly")]
76-
#[cfg_attr(feature = "inline-more", inline)]
76+
#[inline]
7777
unsafe fn offset_from<T>(to: *const T, from: *const T) -> usize {
7878
to.offset_from(from) as usize
7979
}
8080
#[cfg(not(feature = "nightly"))]
81-
#[cfg_attr(feature = "inline-more", inline)]
81+
#[inline]
8282
unsafe fn offset_from<T>(to: *const T, from: *const T) -> usize {
8383
(to as usize - from as usize) / mem::size_of::<T>()
8484
}
@@ -292,14 +292,14 @@ pub struct Bucket<T> {
292292
unsafe impl<T> Send for Bucket<T> {}
293293

294294
impl<T> Clone for Bucket<T> {
295-
#[cfg_attr(feature = "inline-more", inline)]
295+
#[inline]
296296
fn clone(&self) -> Self {
297297
Self { ptr: self.ptr }
298298
}
299299
}
300300

301301
impl<T> Bucket<T> {
302-
#[cfg_attr(feature = "inline-more", inline)]
302+
#[inline]
303303
unsafe fn from_base_index(base: NonNull<T>, index: usize) -> Self {
304304
let ptr = if mem::size_of::<T>() == 0 {
305305
// won't overflow because index must be less than length
@@ -311,15 +311,15 @@ impl<T> Bucket<T> {
311311
ptr: NonNull::new_unchecked(ptr),
312312
}
313313
}
314-
#[cfg_attr(feature = "inline-more", inline)]
314+
#[inline]
315315
unsafe fn to_base_index(&self, base: NonNull<T>) -> usize {
316316
if mem::size_of::<T>() == 0 {
317317
self.ptr.as_ptr() as usize - 1
318318
} else {
319319
offset_from(base.as_ptr(), self.ptr.as_ptr())
320320
}
321321
}
322-
#[cfg_attr(feature = "inline-more", inline)]
322+
#[inline]
323323
pub fn as_ptr(&self) -> *mut T {
324324
if mem::size_of::<T>() == 0 {
325325
// Just return an arbitrary ZST pointer which is properly aligned
@@ -328,7 +328,7 @@ impl<T> Bucket<T> {
328328
unsafe { self.ptr.as_ptr().sub(1) }
329329
}
330330
}
331-
#[cfg_attr(feature = "inline-more", inline)]
331+
#[inline]
332332
unsafe fn next_n(&self, offset: usize) -> Self {
333333
let ptr = if mem::size_of::<T>() == 0 {
334334
(self.ptr.as_ptr() as usize + offset) as *mut T
@@ -343,24 +343,24 @@ impl<T> Bucket<T> {
343343
pub unsafe fn drop(&self) {
344344
self.as_ptr().drop_in_place();
345345
}
346-
#[cfg_attr(feature = "inline-more", inline)]
346+
#[inline]
347347
pub unsafe fn read(&self) -> T {
348348
self.as_ptr().read()
349349
}
350-
#[cfg_attr(feature = "inline-more", inline)]
350+
#[inline]
351351
pub unsafe fn write(&self, val: T) {
352352
self.as_ptr().write(val);
353353
}
354-
#[cfg_attr(feature = "inline-more", inline)]
354+
#[inline]
355355
pub unsafe fn as_ref<'a>(&self) -> &'a T {
356356
&*self.as_ptr()
357357
}
358-
#[cfg_attr(feature = "inline-more", inline)]
358+
#[inline]
359359
pub unsafe fn as_mut<'a>(&self) -> &'a mut T {
360360
&mut *self.as_ptr()
361361
}
362362
#[cfg(feature = "raw")]
363-
#[cfg_attr(feature = "inline-more", inline)]
363+
#[inline]
364364
pub unsafe fn copy_from_nonoverlapping(&self, other: &Self) {
365365
self.as_ptr().copy_from_nonoverlapping(other.as_ptr(), 1);
366366
}
@@ -399,7 +399,7 @@ impl<T> RawTable<T, Global> {
399399
/// In effect this returns a table with exactly 1 bucket. However we can
400400
/// leave the data pointer dangling since that bucket is never written to
401401
/// due to our load factor forcing us to always have at least 1 free bucket.
402-
#[cfg_attr(feature = "inline-more", inline)]
402+
#[inline]
403403
pub const fn new() -> Self {
404404
Self {
405405
table: RawTableInner::new_in(Global),
@@ -428,7 +428,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
428428
/// In effect this returns a table with exactly 1 bucket. However we can
429429
/// leave the data pointer dangling since that bucket is never written to
430430
/// due to our load factor forcing us to always have at least 1 free bucket.
431-
#[cfg_attr(feature = "inline-more", inline)]
431+
#[inline]
432432
pub fn new_in(alloc: A) -> Self {
433433
Self {
434434
table: RawTableInner::new_in(alloc),
@@ -506,26 +506,26 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
506506
}
507507

508508
/// Returns pointer to one past last element of data table.
509-
#[cfg_attr(feature = "inline-more", inline)]
509+
#[inline]
510510
pub unsafe fn data_end(&self) -> NonNull<T> {
511511
NonNull::new_unchecked(self.table.ctrl.as_ptr().cast())
512512
}
513513

514514
/// Returns pointer to start of data table.
515-
#[cfg_attr(feature = "inline-more", inline)]
515+
#[inline]
516516
#[cfg(feature = "nightly")]
517517
pub unsafe fn data_start(&self) -> *mut T {
518518
self.data_end().as_ptr().wrapping_sub(self.buckets())
519519
}
520520

521521
/// Returns the index of a bucket from a `Bucket`.
522-
#[cfg_attr(feature = "inline-more", inline)]
522+
#[inline]
523523
pub unsafe fn bucket_index(&self, bucket: &Bucket<T>) -> usize {
524524
bucket.to_base_index(self.data_end())
525525
}
526526

527527
/// Returns a pointer to an element in the table.
528-
#[cfg_attr(feature = "inline-more", inline)]
528+
#[inline]
529529
pub unsafe fn bucket(&self, index: usize) -> Bucket<T> {
530530
debug_assert_ne!(self.table.bucket_mask, 0);
531531
debug_assert!(index < self.buckets());
@@ -913,19 +913,19 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
913913
///
914914
/// This number is a lower bound; the table might be able to hold
915915
/// more, but is guaranteed to be able to hold at least this many.
916-
#[cfg_attr(feature = "inline-more", inline)]
916+
#[inline]
917917
pub fn capacity(&self) -> usize {
918918
self.table.items + self.table.growth_left
919919
}
920920

921921
/// Returns the number of elements in the table.
922-
#[cfg_attr(feature = "inline-more", inline)]
922+
#[inline]
923923
pub fn len(&self) -> usize {
924924
self.table.items
925925
}
926926

927927
/// Returns the number of buckets in the table.
928-
#[cfg_attr(feature = "inline-more", inline)]
928+
#[inline]
929929
pub fn buckets(&self) -> usize {
930930
self.table.bucket_mask + 1
931931
}
@@ -934,7 +934,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
934934
/// the caller to ensure that the `RawTable` outlives the `RawIter`.
935935
/// Because we cannot make the `next` method unsafe on the `RawIter`
936936
/// struct, we have to make the `iter` method unsafe.
937-
#[cfg_attr(feature = "inline-more", inline)]
937+
#[inline]
938938
pub unsafe fn iter(&self) -> RawIter<T> {
939939
let data = Bucket::from_base_index(self.data_end(), 0);
940940
RawIter {
@@ -1031,7 +1031,7 @@ unsafe impl<T, A: Allocator + Clone> Send for RawTable<T, A> where T: Send {}
10311031
unsafe impl<T, A: Allocator + Clone> Sync for RawTable<T, A> where T: Sync {}
10321032

10331033
impl<A> RawTableInner<A> {
1034-
#[cfg_attr(feature = "inline-more", inline)]
1034+
#[inline]
10351035
const fn new_in(alloc: A) -> Self {
10361036
Self {
10371037
// Be careful to cast the entire slice to a raw pointer.
@@ -1204,22 +1204,22 @@ impl<A: Allocator + Clone> RawTableInner<A> {
12041204
}
12051205
}
12061206

1207-
#[cfg_attr(feature = "inline-more", inline)]
1207+
#[inline]
12081208
unsafe fn bucket<T>(&self, index: usize) -> Bucket<T> {
12091209
debug_assert_ne!(self.bucket_mask, 0);
12101210
debug_assert!(index < self.buckets());
12111211
Bucket::from_base_index(self.data_end(), index)
12121212
}
12131213

1214-
#[cfg_attr(feature = "inline-more", inline)]
1214+
#[inline]
12151215
unsafe fn bucket_ptr(&self, index: usize, size_of: usize) -> *mut u8 {
12161216
debug_assert_ne!(self.bucket_mask, 0);
12171217
debug_assert!(index < self.buckets());
12181218
let base: *mut u8 = self.data_end().as_ptr();
12191219
base.sub((index + 1) * size_of)
12201220
}
12211221

1222-
#[cfg_attr(feature = "inline-more", inline)]
1222+
#[inline]
12231223
unsafe fn data_end<T>(&self) -> NonNull<T> {
12241224
NonNull::new_unchecked(self.ctrl.as_ptr().cast())
12251225
}
@@ -1771,7 +1771,7 @@ impl<T: Clone, A: Allocator + Clone> RawTable<T, A> {
17711771
}
17721772

17731773
impl<T, A: Allocator + Clone + Default> Default for RawTable<T, A> {
1774-
#[cfg_attr(feature = "inline-more", inline)]
1774+
#[inline]
17751775
fn default() -> Self {
17761776
Self::new_in(Default::default())
17771777
}
@@ -1945,7 +1945,7 @@ impl<T> Iterator for RawIterRange<T> {
19451945
}
19461946
}
19471947

1948-
#[cfg_attr(feature = "inline-more", inline)]
1948+
#[inline]
19491949
fn size_hint(&self) -> (usize, Option<usize>) {
19501950
// We don't have an item count, so just guess based on the range size.
19511951
(
@@ -2127,7 +2127,7 @@ impl<T> Iterator for RawIter<T> {
21272127
}
21282128
}
21292129

2130-
#[cfg_attr(feature = "inline-more", inline)]
2130+
#[inline]
21312131
fn size_hint(&self) -> (usize, Option<usize>) {
21322132
(self.items, Some(self.items))
21332133
}
@@ -2193,7 +2193,7 @@ impl<T, A: Allocator + Clone> Iterator for RawIntoIter<T, A> {
21932193
unsafe { Some(self.iter.next()?.read()) }
21942194
}
21952195

2196-
#[cfg_attr(feature = "inline-more", inline)]
2196+
#[inline]
21972197
fn size_hint(&self) -> (usize, Option<usize>) {
21982198
self.iter.size_hint()
21992199
}
@@ -2257,7 +2257,7 @@ impl<T, A: Allocator + Clone> Iterator for RawDrain<'_, T, A> {
22572257
}
22582258
}
22592259

2260-
#[cfg_attr(feature = "inline-more", inline)]
2260+
#[inline]
22612261
fn size_hint(&self) -> (usize, Option<usize>) {
22622262
self.iter.size_hint()
22632263
}

0 commit comments

Comments
 (0)