Skip to content

Commit a0e90f1

Browse files
Refactor: Add add/sub to TaggedLen (#570) (#572)
* Refactor TaggedLen to use add and sub directly (#570) * refactor: remove increment, decrement methods in TaggedLen --------- Signed-off-by: Alejandro Vaz <alejandro.vaz.myt@gmail.com> Co-authored-by: Alejandro Vaz <alejandro.vaz.myt@gmail.com>
1 parent cf3b5d1 commit a0e90f1

2 files changed

Lines changed: 22 additions & 38 deletions

File tree

src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a, T: 'a, const N: usize> Drop for Drain<'a, T, N> {
289289
let dst = ptr.add(start);
290290
core::ptr::copy(src, dst, self.0.tail_len);
291291
}
292-
source_vec.set_len(start + self.0.tail_len);
292+
source_vec.len.add(self.0.tail_len);
293293
}
294294
}
295295
}
@@ -308,7 +308,7 @@ impl<'a, T: 'a, const N: usize> Drop for Drain<'a, T, N> {
308308
unsafe {
309309
let vec = vec.as_mut();
310310
let old_len = vec.len();
311-
vec.set_len(old_len + drop_len + self.tail_len);
311+
vec.len.add(drop_len + self.tail_len);
312312
vec.truncate(old_len + self.tail_len);
313313
}
314314

@@ -374,7 +374,7 @@ impl<T, const N: usize> Drain<'_, T, N> {
374374
if let Some(new_item) = replace_with.next() {
375375
unsafe {
376376
core::ptr::write(place, new_item);
377-
vec.set_len(vec.len() + 1);
377+
vec.len.add(1);
378378
}
379379
} else {
380380
return false;
@@ -1137,7 +1137,7 @@ impl<T, const N: usize> SmallVec<T, N> {
11371137
debug_assert!(len < self.capacity());
11381138
// SAFETY: we have wrote the value to the address already
11391139
unsafe {
1140-
self.len.increment();
1140+
self.len.add(1);
11411141
}
11421142
}
11431143

@@ -1157,7 +1157,7 @@ impl<T, const N: usize> SmallVec<T, N> {
11571157
// SAFETY: new_len < len since len is non-zero and
11581158
// we are returning ownership of the current value.
11591159
unsafe {
1160-
self.len.decrement();
1160+
self.len.sub(1);
11611161
}
11621162
// SAFETY: this element was initialized and we just gave up ownership of
11631163
// it, so we can give it away
@@ -1188,7 +1188,7 @@ impl<T, const N: usize> SmallVec<T, N> {
11881188
// SAFETY: we have a mutable reference to each vector and each uniquely
11891189
// owns its memory. so the ranges can't overlap
11901190
unsafe { copy_nonoverlapping(other.as_ptr(), ptr, other_len) };
1191-
unsafe { self.set_len(total_len) }
1191+
unsafe { self.len.add(other_len) }
11921192
}
11931193

11941194
#[inline]
@@ -1388,7 +1388,7 @@ impl<T, const N: usize> SmallVec<T, N> {
13881388
let value = core::ptr::read(self.as_ptr().add(index));
13891389
let base_ptr = self.as_mut_ptr();
13901390
core::ptr::copy(base_ptr.add(new_len), base_ptr.add(index), 1);
1391-
self.set_len(new_len);
1391+
self.len.sub(1);
13921392
value
13931393
}
13941394
}
@@ -1423,7 +1423,7 @@ impl<T, const N: usize> SmallVec<T, N> {
14231423
let new_len = len - 1;
14241424
unsafe {
14251425
// SAFETY: new_len < len
1426-
self.set_len(new_len);
1426+
self.len.sub(1);
14271427
let ptr = self.as_mut_ptr();
14281428
let ith = ptr.add(index);
14291429
// This item is initialized since index < len
@@ -1478,7 +1478,7 @@ impl<T, const N: usize> SmallVec<T, N> {
14781478
debug_assert!(len < self.capacity());
14791479
// SAFETY: we have wrote the value to the address already
14801480
unsafe {
1481-
self.len.increment();
1481+
self.len.add(1);
14821482
}
14831483
}
14841484

@@ -1908,7 +1908,7 @@ impl<T: Clone, const N: usize> SmallVec<T, N> {
19081908
unsafe {
19091909
let dst = self.as_mut_ptr().add(l);
19101910
copy_nonoverlapping(src, dst, len);
1911-
self.set_len(l + len);
1911+
self.len.add(len);
19121912
}
19131913
}
19141914

@@ -1932,7 +1932,7 @@ impl<T: Clone, const N: usize> SmallVec<T, N> {
19321932
let l = self.len();
19331933
let ptr = self.as_mut_ptr();
19341934
copy_nonoverlapping(ptr.add(start), ptr.add(l), len);
1935-
self.set_len(l + len);
1935+
self.len.add(len);
19361936
}
19371937
}
19381938

@@ -1953,7 +1953,7 @@ impl<T: Clone, const N: usize> SmallVec<T, N> {
19531953
copy_nonoverlapping(other.as_ptr(), ith_ptr, len);
19541954

19551955
// SAFETY: all the elements are initialized
1956-
self.set_len(l + len);
1956+
self.len.add(len);
19571957
}
19581958
}
19591959

@@ -2238,7 +2238,7 @@ impl<T, const N: usize> SmallVec<T, N> {
22382238

22392239
// SAFETY: The elements were initialized in the loop above.
22402240
unsafe {
2241-
self.set_len(old_len + len);
2241+
self.len.add(len);
22422242
}
22432243
}
22442244

@@ -2496,7 +2496,7 @@ unsafe impl<const N: usize> BufMut for SmallVec<u8, N> {
24962496
}
24972497

24982498
// Addition will not overflow since the sum is at most the capacity.
2499-
unsafe { self.set_len(len + cnt) };
2499+
unsafe { self.len.add(cnt) };
25002500
}
25012501

25022502
#[inline]

src/taggedlen.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,20 @@ impl<T> TaggedLen<T> {
5252
if Self::IS_ZST { self.0 } else { self.0 >> 1 }
5353
}
5454

55-
/// Returns the same tag with the length increased by one.
56-
///
57-
/// This increases the length without rereading the `on heap` flag.
58-
///
59-
/// # Safety
60-
///
61-
/// The caller must ensure that after incrementing, the length would still
62-
/// be less than [`isize::MAX`] in bytes. For non-ZSTs this means the
63-
/// length must be less than `isize::MAX - 1` before the call.
6455
#[inline]
65-
pub const unsafe fn increment(&mut self) {
56+
pub const unsafe fn add(&mut self, n: usize) {
6657
self.0 += if Self::IS_ZST {
67-
1
58+
n
6859
} else {
69-
debug_assert!(self.value() + 1 < isize::MAX as usize);
70-
0b10
60+
debug_assert!(self.value() + n < isize::MAX as usize);
61+
n << 1
7162
}
7263
}
7364

74-
/// Returns the same tag with the length decreased by one.
75-
///
76-
/// This decreases the length without rereading the `on heap` flag.
77-
///
78-
/// # Safety
79-
///
80-
/// The caller must ensure that the length is greater than zero before the
81-
/// call.
8265
#[inline]
83-
pub const unsafe fn decrement(&mut self) {
84-
debug_assert!(self.value() > 0);
85-
self.0 -= if Self::IS_ZST { 1 } else { 0b10 };
66+
pub const unsafe fn sub(&mut self, n: usize) {
67+
debug_assert!(self.value() >= n);
68+
69+
self.0 -= if Self::IS_ZST { n } else { n << 1 };
8670
}
8771
}

0 commit comments

Comments
 (0)