Skip to content

Commit e7aa3de

Browse files
committed
feat(allocator): replace allocator_ap2's Vec with Vec2
1 parent 450c4d3 commit e7aa3de

File tree

6 files changed

+65
-64
lines changed

6 files changed

+65
-64
lines changed

crates/oxc_allocator/src/clone_in.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ where
5555
}
5656
}
5757

58-
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Vec<'_, T>
58+
impl<'new_alloc, T, C: 'new_alloc> CloneIn<'new_alloc> for Vec<'_, T>
5959
where
6060
T: CloneIn<'new_alloc, Cloned = C>,
6161
{

crates/oxc_allocator/src/string.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,18 @@ impl<'alloc> String<'alloc> {
104104
/// Caller must ensure this `Vec<u8>` comprises a valid UTF-8 string.
105105
//
106106
// `#[inline(always)]` because this is a no-op at runtime
107+
#[expect(clippy::needless_pass_by_value)]
107108
#[inline(always)]
108109
pub unsafe fn from_utf8_unchecked(bytes: Vec<'alloc, u8>) -> String<'alloc> {
109110
// Cannot use `bumpalo::String::from_utf8_unchecked` because it takes a `bumpalo::collections::Vec`,
110111
// and our inner `Vec` type is `allocator_api2::vec::Vec`.
111112
// SAFETY: Conversion is safe because both types store data in arena in same way.
112113
// Lifetime of returned `String` is same as lifetime of original `Vec<u8>`.
113114
unsafe {
114-
let inner = ManuallyDrop::into_inner(bytes.0);
115-
let (ptr, len, capacity, bump) = inner.into_raw_parts_with_alloc();
115+
let ptr = bytes.as_mut_ptr();
116+
let len = bytes.len();
117+
let capacity = bytes.capacity();
118+
let bump = bytes.bump();
116119
Self(ManuallyDrop::new(BumpaloString::from_raw_parts_in(ptr, len, capacity, bump)))
117120
}
118121
}

crates/oxc_allocator/src/vec.rs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ use std::{
1111
hash::{Hash, Hasher},
1212
mem::ManuallyDrop,
1313
ops,
14-
ptr::NonNull,
1514
slice::SliceIndex,
1615
};
1716

18-
use allocator_api2::vec::Vec as InnerVec;
19-
use bumpalo::Bump;
17+
use crate::vec2::Vec as InnerVec;
2018
#[cfg(any(feature = "serialize", test))]
2119
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
2220
#[cfg(any(feature = "serialize", test))]
@@ -37,7 +35,7 @@ use crate::{Allocator, Box};
3735
/// Static checks make this impossible to do. [`Vec::new_in`] and all other methods which create
3836
/// a [`Vec`] will refuse to compile if called with a [`Drop`] type.
3937
#[derive(PartialEq, Eq)]
40-
pub struct Vec<'alloc, T>(pub(crate) ManuallyDrop<InnerVec<T, &'alloc Bump>>);
38+
pub struct Vec<'alloc, T>(pub(crate) ManuallyDrop<InnerVec<'alloc, T>>);
4139

4240
/// SAFETY: Not actually safe, but for enabling `Send` for downstream crates.
4341
unsafe impl<T> Send for Vec<'_, T> {}
@@ -168,43 +166,43 @@ impl<'alloc, T> Vec<'alloc, T> {
168166
Self(ManuallyDrop::new(vec))
169167
}
170168

171-
/// Converts the vector into [`Box<[T]>`][owned slice].
172-
///
173-
/// Any excess capacity the vector has will not be included in the slice.
174-
/// The excess memory will be leaked in the arena (i.e. not reused by another allocation).
175-
///
176-
/// # Examples
177-
/// ```
178-
/// use oxc_allocator::{Allocator, Vec};
179-
///
180-
/// let allocator = Allocator::default();
181-
/// let mut v = Vec::with_capacity_in(10, &allocator);
182-
/// v.extend([1, 2, 3]);
183-
/// let b = v.into_boxed_slice();
184-
///
185-
/// assert_eq!(&*b, &[1, 2, 3]);
186-
/// assert_eq!(b.len(), 3);
187-
/// ```
188-
///
189-
/// [owned slice]: Box
190-
#[inline]
191-
pub fn into_boxed_slice(self) -> Box<'alloc, [T]> {
192-
let inner = ManuallyDrop::into_inner(self.0);
193-
let slice = inner.leak();
194-
let ptr = NonNull::from(slice);
195-
// SAFETY: `ptr` points to a valid slice `[T]`.
196-
// `allocator_api2::vec::Vec::leak` consumes the inner `Vec` without dropping it.
197-
// Lifetime of returned `Box<'alloc, [T]>` is same as lifetime of consumed `Vec<'alloc, T>`,
198-
// so data in the `Box` must be valid for its lifetime.
199-
// `Vec` uniquely owned the data, and we have consumed the `Vec`, so the new `Box` has
200-
// unique ownership of the data (no aliasing).
201-
// `ptr` was created from a `&mut [T]`.
202-
unsafe { Box::from_non_null(ptr) }
203-
}
169+
// /// Converts the vector into [`Box<[T]>`][owned slice].
170+
// ///
171+
// /// Any excess capacity the vector has will not be included in the slice.
172+
// /// The excess memory will be leaked in the arena (i.e. not reused by another allocation).
173+
// ///
174+
// /// # Examples
175+
// /// ```
176+
// /// use oxc_allocator::{Allocator, Vec};
177+
// ///
178+
// /// let allocator = Allocator::default();
179+
// /// let mut v = Vec::with_capacity_in(10, &allocator);
180+
// /// v.extend([1, 2, 3]);
181+
// /// let b = v.into_boxed_slice();
182+
// ///
183+
// /// assert_eq!(&*b, &[1, 2, 3]);
184+
// /// assert_eq!(b.len(), 3);
185+
// /// ```
186+
// ///
187+
// /// [owned slice]: Box
188+
// #[inline]
189+
// pub fn into_boxed_slice(self) -> Box<'alloc, [T]> {
190+
// let inner = ManuallyDrop::into_inner(self.0);
191+
// let slice = inner.leak();
192+
// let ptr = NonNull::from(slice);
193+
// // SAFETY: `ptr` points to a valid slice `[T]`.
194+
// // `allocator_api2::vec::Vec::leak` consumes the inner `Vec` without dropping it.
195+
// // Lifetime of returned `Box<'alloc, [T]>` is same as lifetime of consumed `Vec<'alloc, T>`,
196+
// // so data in the `Box` must be valid for its lifetime.
197+
// // `Vec` uniquely owned the data, and we have consumed the `Vec`, so the new `Box` has
198+
// // unique ownership of the data (no aliasing).
199+
// // `ptr` was created from a `&mut [T]`.
200+
// unsafe { Box::from_non_null(ptr) }
201+
// }
204202
}
205203

206-
impl<'alloc, T> ops::Deref for Vec<'alloc, T> {
207-
type Target = InnerVec<T, &'alloc Bump>;
204+
impl<'alloc, T: 'alloc> ops::Deref for Vec<'alloc, T> {
205+
type Target = InnerVec<'alloc, T>;
208206

209207
#[inline]
210208
fn deref(&self) -> &Self::Target {
@@ -214,13 +212,13 @@ impl<'alloc, T> ops::Deref for Vec<'alloc, T> {
214212

215213
impl<'alloc, T> ops::DerefMut for Vec<'alloc, T> {
216214
#[inline]
217-
fn deref_mut(&mut self) -> &mut InnerVec<T, &'alloc Bump> {
215+
fn deref_mut(&mut self) -> &mut InnerVec<'alloc, T> {
218216
&mut self.0
219217
}
220218
}
221219

222220
impl<'alloc, T> IntoIterator for Vec<'alloc, T> {
223-
type IntoIter = <InnerVec<T, &'alloc Bump> as IntoIterator>::IntoIter;
221+
type IntoIter = <InnerVec<'alloc, T> as IntoIterator>::IntoIter;
224222
type Item = T;
225223

226224
#[inline(always)]
@@ -353,18 +351,18 @@ mod test {
353351
}
354352
}
355353

356-
#[test]
357-
fn vec_to_boxed_slice() {
358-
let allocator = Allocator::default();
359-
let mut v = Vec::with_capacity_in(10, &allocator);
360-
v.extend([1, 2, 3]);
354+
// #[test]
355+
// fn vec_to_boxed_slice() {
356+
// let allocator = Allocator::default();
357+
// let mut v = Vec::with_capacity_in(10, &allocator);
358+
// v.extend([1, 2, 3]);
361359

362-
let b = v.into_boxed_slice();
363-
// Check return value is an `oxc_allocator::Box`, not an `allocator_api2::boxed::Box`
364-
let b: Box<[u8]> = b;
360+
// let b = v.into_boxed_slice();
361+
// // Check return value is an `oxc_allocator::Box`, not an `allocator_api2::boxed::Box`
362+
// let b: Box<[u8]> = b;
365363

366-
assert_eq!(&*b, &[1, 2, 3]);
367-
// Check length of slice is equal to what `v.len()` was, not `v.capacity()`
368-
assert_eq!(b.len(), 3);
369-
}
364+
// assert_eq!(&*b, &[1, 2, 3]);
365+
// // Check length of slice is equal to what `v.len()` was, not `v.capacity()`
366+
// assert_eq!(b.len(), 3);
367+
// }
370368
}

crates/oxc_isolated_declarations/src/declaration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a> IsolatedDeclarations<'a> {
105105

106106
fn transform_ts_module_block(
107107
&mut self,
108-
block: &ArenaBox<'a, TSModuleBlock<'a>>,
108+
block: &'a ArenaBox<'a, TSModuleBlock<'a>>,
109109
) -> ArenaBox<'a, TSModuleBlock<'a>> {
110110
// We need to enter a new scope for the module block, avoid add binding to the parent scope
111111
// TODO: doesn't have a scope_id!
@@ -117,7 +117,7 @@ impl<'a> IsolatedDeclarations<'a> {
117117

118118
pub(crate) fn transform_ts_module_declaration(
119119
&mut self,
120-
decl: &ArenaBox<'a, TSModuleDeclaration<'a>>,
120+
decl: &'a ArenaBox<'a, TSModuleDeclaration<'a>>,
121121
) -> ArenaBox<'a, TSModuleDeclaration<'a>> {
122122
if decl.declare {
123123
return decl.clone_in(self.ast.allocator);
@@ -153,7 +153,7 @@ impl<'a> IsolatedDeclarations<'a> {
153153

154154
pub(crate) fn transform_declaration(
155155
&mut self,
156-
decl: &Declaration<'a>,
156+
decl: &'a Declaration<'a>,
157157
check_binding: bool,
158158
) -> Option<Declaration<'a>> {
159159
match decl {

crates/oxc_isolated_declarations/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a> IsolatedDeclarations<'a> {
7979
/// # Errors
8080
///
8181
/// Returns `Vec<Error>` if any errors were collected during the transformation.
82-
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
82+
pub fn build(mut self, program: &'a Program<'a>) -> IsolatedDeclarationsReturn<'a> {
8383
self.internal_annotations = self
8484
.strip_internal
8585
.then(|| Self::build_internal_annotations(program))
@@ -132,7 +132,7 @@ impl<'a> IsolatedDeclarations<'a> {
132132
}
133133

134134
impl<'a> IsolatedDeclarations<'a> {
135-
fn transform_program(&mut self, program: &Program<'a>) -> ArenaVec<'a, Statement<'a>> {
135+
fn transform_program(&mut self, program: &'a Program<'a>) -> ArenaVec<'a, Statement<'a>> {
136136
let has_import_or_export = program.body.iter().any(Statement::is_module_declaration);
137137

138138
if has_import_or_export {
@@ -144,7 +144,7 @@ impl<'a> IsolatedDeclarations<'a> {
144144

145145
fn transform_program_without_module_declaration(
146146
&mut self,
147-
stmts: &ArenaVec<'a, Statement<'a>>,
147+
stmts: &'a ArenaVec<'a, Statement<'a>>,
148148
) -> ArenaVec<'a, Statement<'a>> {
149149
self.report_error_for_expando_function(stmts);
150150

@@ -166,7 +166,7 @@ impl<'a> IsolatedDeclarations<'a> {
166166

167167
fn transform_statements_on_demand(
168168
&mut self,
169-
stmts: &ArenaVec<'a, Statement<'a>>,
169+
stmts: &'a ArenaVec<'a, Statement<'a>>,
170170
) -> ArenaVec<'a, Statement<'a>> {
171171
self.report_error_for_expando_function(stmts);
172172

crates/oxc_isolated_declarations/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{IsolatedDeclarations, diagnostics::default_export_inferred};
77
impl<'a> IsolatedDeclarations<'a> {
88
pub(crate) fn transform_export_named_declaration(
99
&mut self,
10-
prev_decl: &ExportNamedDeclaration<'a>,
10+
prev_decl: &'a ExportNamedDeclaration<'a>,
1111
) -> Option<ExportNamedDeclaration<'a>> {
1212
let decl = self.transform_declaration(prev_decl.declaration.as_ref()?, false)?;
1313

0 commit comments

Comments
 (0)