Skip to content

Commit b7f93cc

Browse files
Support vec zero-alloc optimization for tuples and byte arrays
* Implement IsZero trait for tuples up to 8 IsZero elements; * Implement IsZero for u8/i8, leading to implementation of it for arrays of them too; * Add more codegen tests for this optimization. * Lower size of array for IsZero trait because it fails to inline checks
1 parent 2d37db4 commit b7f93cc

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

alloc/src/vec/is_zero.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ macro_rules! impl_is_zero {
1717
};
1818
}
1919

20+
impl_is_zero!(i8, |x| x == 0); // It is needed to impl for arrays and tuples of i8.
2021
impl_is_zero!(i16, |x| x == 0);
2122
impl_is_zero!(i32, |x| x == 0);
2223
impl_is_zero!(i64, |x| x == 0);
2324
impl_is_zero!(i128, |x| x == 0);
2425
impl_is_zero!(isize, |x| x == 0);
2526

27+
impl_is_zero!(u8, |x| x == 0); // It is needed to impl for arrays and tuples of u8.
2628
impl_is_zero!(u16, |x| x == 0);
2729
impl_is_zero!(u32, |x| x == 0);
2830
impl_is_zero!(u64, |x| x == 0);
@@ -54,15 +56,41 @@ unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
5456
fn is_zero(&self) -> bool {
5557
// Because this is generated as a runtime check, it's not obvious that
5658
// it's worth doing if the array is really long. The threshold here
57-
// is largely arbitrary, but was picked because as of 2022-05-01 LLVM
58-
// can const-fold the check in `vec![[0; 32]; n]` but not in
59-
// `vec![[0; 64]; n]`: https://godbolt.org/z/WTzjzfs5b
59+
// is largely arbitrary, but was picked because as of 2022-07-01 LLVM
60+
// fails to const-fold the check in `vec![[1; 32]; n]`
61+
// See https://github.com/rust-lang/rust/pull/97581#issuecomment-1166628022
6062
// Feel free to tweak if you have better evidence.
6163

62-
N <= 32 && self.iter().all(IsZero::is_zero)
64+
N <= 16 && self.iter().all(IsZero::is_zero)
6365
}
6466
}
6567

68+
// This is recursive macro.
69+
macro_rules! impl_for_tuples {
70+
// Stopper
71+
() => {
72+
// No use for implementing for empty tuple because it is ZST.
73+
};
74+
($first_arg:ident $(,$rest:ident)*) => {
75+
unsafe impl <$first_arg: IsZero, $($rest: IsZero,)*> IsZero for ($first_arg, $($rest,)*){
76+
#[inline]
77+
fn is_zero(&self) -> bool{
78+
// Destructure tuple to N references
79+
// Rust allows to hide generic params by local variable names.
80+
#[allow(non_snake_case)]
81+
let ($first_arg, $($rest,)*) = self;
82+
83+
$first_arg.is_zero()
84+
$( && $rest.is_zero() )*
85+
}
86+
}
87+
88+
impl_for_tuples!($($rest),*);
89+
}
90+
}
91+
92+
impl_for_tuples!(A, B, C, D, E, F, G, H);
93+
6694
// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
6795
// For fat pointers, the bytes that would be the pointer metadata in the `Some`
6896
// variant are padding in the `None` variant, so ignoring them and

alloc/src/vec/spec_from_elem.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use core::ptr;
2+
13
use crate::alloc::Allocator;
24
use crate::raw_vec::RawVec;
3-
use core::ptr::{self};
45

56
use super::{ExtendElement, IsZero, Vec};
67

@@ -17,6 +18,18 @@ impl<T: Clone> SpecFromElem for T {
1718
}
1819
}
1920

21+
impl<T: Clone + IsZero> SpecFromElem for T {
22+
#[inline]
23+
default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
24+
if elem.is_zero() {
25+
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
26+
}
27+
let mut v = Vec::with_capacity_in(n, alloc);
28+
v.extend_with(n, ExtendElement(elem));
29+
v
30+
}
31+
}
32+
2033
impl SpecFromElem for i8 {
2134
#[inline]
2235
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A> {
@@ -46,15 +59,3 @@ impl SpecFromElem for u8 {
4659
}
4760
}
4861
}
49-
50-
impl<T: Clone + IsZero> SpecFromElem for T {
51-
#[inline]
52-
fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
53-
if elem.is_zero() {
54-
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
55-
}
56-
let mut v = Vec::with_capacity_in(n, alloc);
57-
v.extend_with(n, ExtendElement(elem));
58-
v
59-
}
60-
}

0 commit comments

Comments
 (0)