Skip to content

Commit b35d551

Browse files
authored
Rollup merge of #162810 - ywxt:dedup, r=Amanieu
vectorized vec::dedup pre-scan This PR vectorizes the process of pre-scaning during `Vec::dedup`. on aarch64: bench | baseline  |  final | |  ±Δ -- | -- | -- | -- | -- all_100 | 56 | 60 | 3 | +6.2% all_1000 | 507 | 508 | 4 | +0.2% all_10000 | 4,961 | 4,961 | 26 | +0.0% all_100000 | 48,876 | 49,179 | 204 | +0.6% none_100 | 49 | 17 | 1 | -65.5% none_1000 | 495 | 129 | 2 | -74.0% none_10000 | 4,873 | 1,226 | 7 | -74.8% none_100000 | 48,673 | 12,549 | 97 | -74.2% random_100 | 81 | 74 | 1 | -8.7% random_1000 | 692 | 610 | 3 | -11.8% random_10000 | 7,887 | 7,187 | 101 | -8.9% random_100000 | 385,167 | 363,078 | 3,987 | -5.7% slice_truncate_100 | 92 | 91 | 2 | -0.6% slice_truncate_1000 | 712 | 710 | 3 | -0.2% slice_truncate_10000 | 9,151 | 9,130 | 53 | -0.2% slice_truncate_100000 | 398,559 | 388,269 | 1,127 | -2.6% string_long_all_100 | 7,585 | 7,638 | 49 | +0.7% string_long_all_1000 | 335,862 | 326,630 | 7,669 | -2.7% string_long_all_10000 | 4,351,640 | 4,356,293 | 26,004 | +0.1% string_long_none_100 | 2,954 | 2,912 | 34 | -1.4% string_long_none_1000 | 30,945 | 29,910 | 512 | -3.3% string_long_none_10000 | 310,118 | 304,530 | 2,447 | -1.8% string_long_random_100 | 5,924 | 5,906 | 33 | -0.3% string_long_random_1000 | 119,938 | 117,517 | 1,163 | -2.0% string_long_random_10000 | 1,560,353 | 1,583,948 | 16,559 | +1.5% string_medium_all_100 | 2,532 | 2,535 | 36 | +0.1% string_medium_all_1000 | 26,413 | 26,436 | 233 | +0.1% string_medium_all_10000 | 271,453 | 268,422 | 2,226 | -1.1% string_medium_none_100 | 400 | 393 | 6 | -1.9% string_medium_none_1000 | 4,074 | 3,918 | 34 | -3.8% string_medium_none_10000 | 41,120 | 39,650 | 553 | -3.6% string_medium_random_100 | 1,155 | 1,168 | 38 | +1.1% string_medium_random_1000 | 27,462 | 27,281 | 133 | -0.7% string_medium_random_10000 | 165,098 | 164,762 | 3,789 | -0.2% string_short_all_100 | 2,353 | 2,372 | 38 | +0.8% string_short_all_1000 | 23,455 | 23,766 | 233 | +1.3% string_short_all_10000 | 235,946 | 236,944 | 3,684 | +0.4% string_short_none_100 | 281 | 268 | 5 | -4.6% string_short_none_1000 | 2,767 | 2,603 | 21 | -6.0% string_short_none_10000 | 27,633 | 25,911 | 250 | -6.2% string_short_random_100 | 1,072 | 1,079 | 15 | +0.6% string_short_random_1000 | 24,270 | 24,180 | 59 | -0.4% string_short_random_10000 | 100,799 | 98,519 | 594 | -2.3% u8_all_1000 | 464 | 463 | 3 | -0.2% u8_all_10000 | 4,479 | 4,477 | 38 | -0.0% u8_none_1000 | 469 | 87 | 2 | -81.5% u8_none_10000 | 4,611 | 811 | 7 | -82.4% u8_random_1000 | 568 | 567 | 11 | -0.2% u8_random_10000 | 4,583 | 4,578 | 36 | -0.1% wide_all_1000 | 1,849 | 1,854 | 20 | +0.3% wide_all_10000 | 18,499 | 18,446 | 176 | -0.3% wide_none_1000 | 1,081 | 909 | 1 | -15.9% wide_none_10000 | 10,395 | 9,289 | 122 | -10.6% wide_random_1000 | 1,805 | 1,796 | 6 | -0.5% wide_random_10000 | 17,994 | 17,984 | 82 | -0.1%
2 parents 0679ba4 + 2ad8211 commit b35d551

3 files changed

Lines changed: 390 additions & 108 deletions

File tree

‎library/alloc/src/vec/mod.rs‎

Lines changed: 159 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,24 @@ impl<T, A: Allocator> Vec<T, A> {
27132713
if first_duplicate_idx == len {
27142714
return;
27152715
}
2716+
// SAFETY: first_duplicate_idx always in range [1..len)
2717+
unsafe {
2718+
self.dedup_tail(same_bucket, first_duplicate_idx);
2719+
}
2720+
}
27162721

2722+
/// Continues deduplication after the first duplicate is found.
2723+
///
2724+
/// The function treats `self[first_duplicate]` as the first duplicated
2725+
/// element and drops it.
2726+
///
2727+
/// # Safety
2728+
///
2729+
/// `1 <= first_duplicate < self.len()`
2730+
unsafe fn dedup_tail<F>(&mut self, mut same_bucket: F, first_duplicate: usize)
2731+
where
2732+
F: FnMut(&mut T, &mut T) -> bool,
2733+
{
27172734
/* INVARIANT: vec.len() > read > write > write-1 >= 0 */
27182735
struct FillGapOnDrop<'a, T, A: core::alloc::Allocator> {
27192736
/* Offset of the element we want to check if it is duplicate */
@@ -2760,16 +2777,20 @@ impl<T, A: Allocator> Vec<T, A> {
27602777
}
27612778
}
27622779

2780+
let start = self.as_mut_ptr();
2781+
let len = self.len();
2782+
27632783
/* Drop items while going through Vec, it should be more efficient than
27642784
* doing slice partition_dedup + truncate */
27652785

27662786
// Construct gap first and then drop item to avoid memory corruption if `T::drop` panics.
27672787
let mut gap =
2768-
FillGapOnDrop { read: first_duplicate_idx + 1, write: first_duplicate_idx, vec: self };
2769-
// SAFETY: we checked that first_duplicate_idx in bounds before.
2788+
FillGapOnDrop { read: first_duplicate + 1, write: first_duplicate, vec: self };
2789+
2790+
// SAFETY: By this function's safety contract, `first_duplicate < self.len()`.
27702791
// If drop panics, `gap` would remove this item without drop.
27712792
unsafe {
2772-
ptr::drop_in_place(start.add(first_duplicate_idx));
2793+
ptr::drop_in_place(start.add(first_duplicate));
27732794
}
27742795

27752796
// SAFETY: Because of the invariant, read_ptr, prev_ptr and write_ptr
@@ -3788,8 +3809,142 @@ impl<T: PartialEq, A: Allocator> Vec<T, A> {
37883809
#[stable(feature = "rust1", since = "1.0.0")]
37893810
#[inline]
37903811
pub fn dedup(&mut self) {
3791-
self.dedup_by(|a, b| a == b)
3812+
// Vectorize the process of finding first duplicate if possible
3813+
if let Some(chunk_size) = dedup_simd_chunk_size::<T>() {
3814+
// Unlike dedup_by, we don't guarantee the order
3815+
// and time in which we call `PartialEq::eq`.
3816+
let Some(first_duplicate) = find_first_duplicate(self.as_slice(), chunk_size) else {
3817+
return;
3818+
};
3819+
3820+
// SAFETY: `1 <= first_duplicate < self.len()`
3821+
unsafe {
3822+
self.dedup_tail(|a, b| a == b, first_duplicate);
3823+
}
3824+
} else {
3825+
self.dedup_by(|a, b| a == b);
3826+
}
3827+
}
3828+
}
3829+
3830+
const fn dedup_simd_chunk_size<T>() -> Option<usize> {
3831+
// The minimal SIMD width on supported platforms
3832+
#[allow(unused)]
3833+
const SIMD_WIDTH: usize = 16;
3834+
let size = size_of::<T>();
3835+
3836+
// We only have SIMD implementations for these element sizes.
3837+
if !matches!(size, 1 | 2 | 4 | 8) {
3838+
return None;
3839+
}
3840+
3841+
let simd_width: Option<usize> = cfg_select! {
3842+
all(target_arch = "arm", target_feature = "neon") => {
3843+
// Packed 64-bit integer lane equality is unavailable on arm neon
3844+
if size == 8 { None } else { Some(SIMD_WIDTH) }
3845+
}
3846+
3847+
all(target_arch = "aarch64", target_feature = "neon") => {
3848+
Some(SIMD_WIDTH)
3849+
}
3850+
3851+
all(
3852+
any(target_arch = "x86", target_arch = "x86_64"),
3853+
target_feature = "sse4.1"
3854+
) => Some(SIMD_WIDTH),
3855+
3856+
all(
3857+
any(target_arch = "x86", target_arch = "x86_64"),
3858+
target_feature = "sse2"
3859+
) => {
3860+
// Packed 64-bit integer lane equality is only available on sse4.1
3861+
if size == 8 { None } else { Some(SIMD_WIDTH) }
3862+
}
3863+
3864+
all(
3865+
any(target_arch = "riscv32", target_arch = "riscv64"),
3866+
target_feature = "v"
3867+
) => Some(SIMD_WIDTH),
3868+
3869+
all(
3870+
any(target_arch = "loongarch32", target_arch = "loongarch64"),
3871+
target_feature = "lsx"
3872+
) => Some(SIMD_WIDTH),
3873+
3874+
_ => None,
3875+
};
3876+
3877+
if let Some(simd_width) = simd_width { Some(simd_width * 2 / size) } else { None }
3878+
}
3879+
3880+
fn find_first_duplicate<T: PartialEq>(v: &[T], chunk_size: usize) -> Option<usize> {
3881+
let len = v.len();
3882+
if len <= 1 {
3883+
return None;
3884+
}
3885+
3886+
let mut i = 1;
3887+
3888+
// The scalar fallback path if the length is not long enough
3889+
if len < chunk_size + 1 {
3890+
while i < len {
3891+
// It keeps the same comparison order as `dedup_tail`
3892+
//
3893+
// SAFETY: i always in range [1..len)
3894+
if unsafe { v.get_unchecked(i) == v.get_unchecked(i - 1) } {
3895+
return Some(i);
3896+
}
3897+
i += 1;
3898+
}
3899+
return None;
3900+
}
3901+
3902+
/// # Safety
3903+
/// - `start > 0` and `start + chunk_size <= v.len()`
3904+
/// - `0 < chunk_size <= 32`
3905+
#[inline(always)]
3906+
unsafe fn find_duplicate<T: PartialEq>(
3907+
v: &[T],
3908+
start: usize,
3909+
chunk_size: usize,
3910+
) -> Option<usize> {
3911+
let mut mask: u32 = 0;
3912+
debug_assert!(
3913+
u32::BITS as usize >= chunk_size,
3914+
"the bit width of mask must be greater than or equal to chunk_size"
3915+
);
3916+
for j in 0..chunk_size {
3917+
// SAFETY: start + j always in range [1..v.len())
3918+
let eq = unsafe { v.get_unchecked(start + j) == v.get_unchecked(start + j - 1) };
3919+
mask |= (eq as u32) << j;
3920+
}
3921+
if mask != 0 {
3922+
return Some(start + mask.trailing_zeros() as usize);
3923+
}
3924+
None
37923925
}
3926+
3927+
// This loop performs chunk_size comparisons in parallel without early exit,
3928+
// which is what allows for SIMD optimization.
3929+
while i + chunk_size <= len {
3930+
// SAFETY: `i >= 1` and `i + chunk_size <= len`
3931+
if let Some(duplicate) = unsafe { find_duplicate(v, i, chunk_size) } {
3932+
return Some(duplicate);
3933+
}
3934+
i += chunk_size;
3935+
}
3936+
3937+
// This may check the same elements multiple times.
3938+
// However we don't guarantee `PartialEq::eq` is only called once per pair.
3939+
if i < len {
3940+
let start = len - chunk_size;
3941+
// SAFETY: `len - chunk_size >= 1` and `start + chunk_size <= len`
3942+
if let Some(duplicate) = unsafe { find_duplicate(v, start, chunk_size) } {
3943+
return Some(duplicate);
3944+
}
3945+
}
3946+
3947+
None
37933948
}
37943949

37953950
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)