feat(allocator): add Vec2::retain_mut method#9655
Conversation
CodSpeed Performance ReportMerging #9655 will not alter performanceComparing Summary
|
450c4d3 to
b3bbb9e
Compare
9580d1e to
642f881
Compare
d9a6cb3 to
415d24f
Compare
b3bbb9e to
14c989a
Compare
415d24f to
c068933
Compare
14c989a to
8bdcb8c
Compare
|
Are you able to summarize what "modified a little" means? |
Different from standard implementation, Bumpalo accepts a bump lifetime instead of an Allocator generic type, so all changes made revolve around this difference. See the following diff you will know what I am talking about. diff --git a/crates/oxc_allocator/src/vec2/mod.rs b/crates/oxc_allocator/src/vec2/mod.rs
index 945eaf4e7..a0185a10d 100644
--- a/crates/oxc_allocator/src/vec2/mod.rs
+++ b/crates/oxc_allocator/src/vec2/mod.rs
@@ -1318,7 +1318,10 @@ impl<'bump, T: 'bump> Vec<'bump, T> {
/// });
/// assert_eq!(vec, [2, 3, 4]);
/// ```
- #[stable(feature = "vec_retain_mut", since = "1.61.0")]
+ // The implementation is based on the [`std::vec::Vec::retain_mut`].
+ //
+ // Allowing the following clippy rules just to make the code same as the original implementation.
+ #[expect(clippy::items_after_statements, clippy::redundant_else)]
pub fn retain_mut<F>(&mut self, mut f: F)
where
F: FnMut(&mut T) -> bool,
@@ -1345,14 +1348,14 @@ impl<'bump, T: 'bump> Vec<'bump, T> {
// This drop guard will be invoked when predicate or `drop` of element panicked.
// It shifts unchecked elements to cover holes and `set_len` to the correct length.
// In cases when predicate and `drop` never panick, it will be optimized out.
- struct BackshiftOnDrop<'a, T, A: Allocator> {
- v: &'a mut Vec<T, A>,
+ struct BackshiftOnDrop<'bump, 'a, T> {
+ v: &'a mut Vec<'bump, T>,
processed_len: usize,
deleted_cnt: usize,
original_len: usize,
}
- impl<T, A: Allocator> Drop for BackshiftOnDrop<'_, T, A> {
+ impl<T> Drop for BackshiftOnDrop<'_, '_, T> {
fn drop(&mut self) {
if self.deleted_cnt > 0 {
// SAFETY: Trailing unchecked items must be valid since we never touch them.
@@ -1373,10 +1376,10 @@ impl<'bump, T: 'bump> Vec<'bump, T> {
let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len };
- fn process_loop<F, T, A: Allocator, const DELETED: bool>(
+ fn process_loop<F, T, const DELETED: bool>(
original_len: usize,
f: &mut F,
- g: &mut BackshiftOnDrop<'_, T, A>,
+ g: &mut BackshiftOnDrop<'_, '_, T>,
) where
F: FnMut(&mut T) -> bool,
{
@@ -1409,10 +1412,10 @@ impl<'bump, T: 'bump> Vec<'bump, T> {
}
// Stage 1: Nothing was deleted.
- process_loop::<F, T, A, false>(original_len, &mut f, &mut g);
+ process_loop::<F, T, false>(original_len, &mut f, &mut g);
// Stage 2: Some elements were deleted.
- process_loop::<F, T, A, true>(original_len, &mut f, &mut g);
+ process_loop::<F, T, true>(original_len, &mut f, &mut g);
// All item are processed. This can be optimized to `set_len` by LLVM.
drop(g);
|
c068933 to
3943563
Compare
8bdcb8c to
247b928
Compare
247b928 to
fcb5728
Compare
|
Brilliant. Thank you. That really is very little change. The only odd thing is why have to change the lifetime bounds on |
Oh, this is a mistake change, this change is actually what I was trying to fix the lifetime problem in #9656. I don't know why they appear in this PR, anyway I will revert them. |
fcb5728 to
ec2f8da
Compare
|
OK great. We can merge this one too then. |
Merge activity
|
OXC has a few places using this API, so we need to add this method before replacing allocator-api2's `Vec`. The implementation is copied from the https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2121-2123, and modified a little to make it fit the `Vec2`.
ec2f8da to
65d9662
Compare
self.reserve(1) calls with self.grow_one() for better efficiency
#9856

OXC has a few places using this API, so we need to add this method before replacing allocator-api2's
Vec. The implementation is copied from the https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2121-2123, and modified a little to make it fit theVec2.