1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use std::{
alloc::Layout,
cell::Cell,
hash::{BuildHasher, Hash},
ptr::NonNull,
slice,
};
use crate::{Allocator, Box, HashMap, Vec};
/// A trait to explicitly clone an object into an arena allocator.
///
/// As a convention `Cloned` associated type should always be the same as `Self`,
/// It'd only differ in the lifetime, Here's an example:
///
/// ```
/// # use oxc_allocator::{Allocator, CloneIn, Vec};
/// # struct Struct<'a> {a: Vec<'a, u8>, b: u8}
///
/// impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Struct<'old_alloc> {
/// type Cloned = Struct<'new_alloc>;
/// fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
/// Struct { a: self.a.clone_in(allocator), b: self.b.clone_in(allocator) }
/// }
/// }
/// ```
///
/// Implementations of this trait on non-allocated items usually short-circuit to `Clone::clone`;
/// However, it **isn't** guaranteed.
///
pub trait CloneIn<'new_alloc>: Sized {
/// The type of the cloned object.
///
/// This should always be `Self` with a different lifetime.
type Cloned;
/// Clone `self` into the given `allocator`. `allocator` may be the same one
/// that `self` is already in.
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned;
/// Almost identical as `clone_in`, but for some special type, it will also clone the semantic ids.
/// Please use this method only if you make sure semantic info is synced with the ast node.
#[inline]
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
self.clone_in(allocator)
}
}
impl<'alloc, T, C> CloneIn<'alloc> for Option<T>
where
T: CloneIn<'alloc, Cloned = C>,
{
type Cloned = Option<C>;
fn clone_in(&self, allocator: &'alloc Allocator) -> Self::Cloned {
self.as_ref().map(|it| it.clone_in(allocator))
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
self.as_ref().map(|it| it.clone_in_with_semantic_ids(allocator))
}
}
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Box<'_, T>
where
T: CloneIn<'new_alloc, Cloned = C>,
{
type Cloned = Box<'new_alloc, C>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Box::new_in(self.as_ref().clone_in(allocator), allocator)
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Box::new_in(self.as_ref().clone_in_with_semantic_ids(allocator), allocator)
}
}
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Box<'_, [T]>
where
T: CloneIn<'new_alloc, Cloned = C>,
{
type Cloned = Box<'new_alloc, [C]>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
let slice = self.as_ref();
// Compile-time check that `T` and `C` have identical size and alignment - which they always will
// with intended usage that `T` and `C` are same types, just with different lifetimes.
// This guarantees that layout of clone is same as layout of `slice`,
// so we can create `Layout` with `for_value`, which has no runtime checks.
const {
assert!(
size_of::<C>() == size_of::<T>() && align_of::<C>() == align_of::<T>(),
"Size and alignment of `T` and `<T as CloneIn>::Cloned` must be the same"
);
}
let layout = Layout::for_value(slice);
let dst_ptr = allocator.alloc_layout(layout).cast::<C>();
// SAFETY: We allocated space for `slice.len()` items of type `C`, starting at `dst_ptr`.
// Therefore, writing `slice.len()` elements to that memory region is safe.
// `C` isn't `Drop`, and allocation is in the arena, so we don't need to worry about a panic
// in the loop - can't lead to a memory leak.
unsafe {
let mut ptr = dst_ptr;
for item in slice {
ptr.write(item.clone_in(allocator));
ptr = ptr.add(1);
}
}
// SAFETY: We just initialized `slice.len()` x `C`s, starting at `dst_ptr`
let new_slice = unsafe { slice::from_raw_parts_mut(dst_ptr.as_ptr(), slice.len()) };
// SAFETY: `NonNull::from(new_slice)` produces a valid pointer. The data is in the arena.
// Lifetime of returned `Box` matches the `Allocator` the data was allocated in.
unsafe { Box::from_non_null(NonNull::from(new_slice)) }
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
let slice = self.as_ref();
// Compile-time check that `T` and `C` have identical size and alignment - which they always will
// with intended usage that `T` and `C` are same types, just with different lifetimes.
// This guarantees that layout of clone is same as layout of `slice`,
// so we can create `Layout` with `for_value`, which has no runtime checks.
const {
assert!(
size_of::<C>() == size_of::<T>() && align_of::<C>() == align_of::<T>(),
"Size and alignment of `T` and `<T as CloneIn>::Cloned` must be the same"
);
}
let layout = Layout::for_value(slice);
let dst_ptr = allocator.alloc_layout(layout).cast::<C>();
// SAFETY: We allocated space for `slice.len()` items of type `C`, starting at `dst_ptr`.
// Therefore, writing `slice.len()` elements to that memory region is safe.
// `C` isn't `Drop`, and allocation is in the arena, so we don't need to worry about a panic
// in the loop - can't lead to a memory leak.
unsafe {
let mut ptr = dst_ptr;
for item in slice {
ptr.write(item.clone_in_with_semantic_ids(allocator));
ptr = ptr.add(1);
}
}
// SAFETY: We just initialized `slice.len()` x `C`s, starting at `dst_ptr`
let new_slice = unsafe { slice::from_raw_parts_mut(dst_ptr.as_ptr(), slice.len()) };
// SAFETY: `NonNull::from(new_slice)` produces a valid pointer. The data is in the arena.
// Lifetime of returned `Box` matches the `Allocator` the data was allocated in.
unsafe { Box::from_non_null(NonNull::from(new_slice)) }
}
}
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Vec<'_, T>
where
T: CloneIn<'new_alloc, Cloned = C>,
// TODO: This lifetime bound possibly shouldn't be required.
// https://github.com/oxc-project/oxc/pull/9656#issuecomment-2719762898
C: 'new_alloc,
{
type Cloned = Vec<'new_alloc, C>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
// The implementation below is equivalent to:
// `Vec::from_iter_in(self.iter().map(|it| it.clone_in(allocator)), allocator)`
// But `Vec::from_iter_in` is inefficient because it performs a bounds check for each item.
// This is unnecessary in this case as we know the length of the slice with certainty.
// This implementation takes advantage of that invariant, and skips those checks.
let slice = self.as_slice();
let mut vec = Vec::<C>::with_capacity_in(slice.len(), allocator);
// SAFETY: We allocated capacity for `slice.len()` elements in `vec`.
// Therefore, writing `slice.len()` elements to that memory region is safe.
// `C` and `Vec` aren't `Drop`, and allocation is in the arena, so we don't need to worry about
// a panic in this loop - can't lead to a memory leak. We just set length at the end.
unsafe {
let mut ptr = vec.as_mut_ptr();
for item in slice {
ptr.write(item.clone_in(allocator));
ptr = ptr.add(1);
}
vec.set_len(slice.len());
}
vec
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
let slice = self.as_slice();
let mut vec = Vec::<C>::with_capacity_in(slice.len(), allocator);
// SAFETY: We allocated capacity for `slice.len()` elements in `vec`.
// Therefore, writing `slice.len()` elements to that memory region is safe.
// `C` and `Vec` aren't `Drop`, and allocation is in the arena, so we don't need to worry about
// a panic in this loop - can't lead to a memory leak. We just set length at the end.
unsafe {
let mut ptr = vec.as_mut_ptr();
for item in slice {
ptr.write(item.clone_in_with_semantic_ids(allocator));
ptr = ptr.add(1);
}
vec.set_len(slice.len());
}
vec
}
}
impl<'new_alloc, K, V, CK, CV, S> CloneIn<'new_alloc> for HashMap<'_, K, V, S>
where
K: CloneIn<'new_alloc, Cloned = CK>,
V: CloneIn<'new_alloc, Cloned = CV>,
CK: Hash + Eq,
S: Default + BuildHasher,
{
type Cloned = HashMap<'new_alloc, CK, CV, S>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
// Keys in original hash map are guaranteed to be unique.
// Unfortunately, we have no static guarantee that `CloneIn` maintains that uniqueness
// - original keys (`K`) are guaranteed unique, but cloned keys (`CK`) might not be.
// If we did have that guarantee, we could use the faster `insert_unique_unchecked` here.
// `hashbrown::HashMap` also has a faster cloning method in its `Clone` implementation,
// but those APIs are not exposed, and `Clone` doesn't support custom allocators.
// So sadly this is a lot slower than it could be, especially for `Copy` types.
let mut cloned = HashMap::with_capacity_in(self.len(), allocator);
for (key, value) in self {
cloned.insert(key.clone_in(allocator), value.clone_in(allocator));
}
cloned
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
let mut cloned = HashMap::with_capacity_in(self.len(), allocator);
for (key, value) in self {
cloned.insert(
key.clone_in_with_semantic_ids(allocator),
value.clone_in_with_semantic_ids(allocator),
);
}
cloned
}
}
impl<'alloc, T: Copy> CloneIn<'alloc> for Cell<T> {
type Cloned = Cell<T>;
fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned {
Cell::new(self.get())
}
}
impl<'new_alloc> CloneIn<'new_alloc> for &str {
type Cloned = &'new_alloc str;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
allocator.alloc_str(self)
}
}
macro_rules! impl_clone_in {
($($t:ty)*) => {
$(
impl<'alloc> CloneIn<'alloc> for $t {
type Cloned = Self;
#[inline(always)]
fn clone_in(&self, _: &'alloc Allocator) -> Self {
*self
}
}
)*
}
}
impl_clone_in! {
usize u8 u16 u32 u64 u128
isize i8 i16 i32 i64 i128
f32 f64
bool char
}
#[cfg(test)]
mod test {
use super::{Allocator, CloneIn, HashMap, Vec};
#[test]
fn clone_in_boxed_slice() {
let allocator = Allocator::default();
let mut original = Vec::from_iter_in([1, 2, 3], &allocator).into_boxed_slice();
let cloned = original.clone_in(&allocator);
let cloned2 = original.clone_in_with_semantic_ids(&allocator);
original[1] = 4;
assert_eq!(original.as_ref(), &[1, 4, 3]);
assert_eq!(cloned.as_ref(), &[1, 2, 3]);
assert_eq!(cloned2.as_ref(), &[1, 2, 3]);
}
#[test]
fn clone_in_vec() {
let allocator = Allocator::default();
let mut original = Vec::with_capacity_in(8, &allocator);
original.extend_from_slice(&[1, 2, 3]);
let cloned = original.clone_in(&allocator);
let cloned2 = original.clone_in_with_semantic_ids(&allocator);
original[1] = 4;
assert_eq!(original.as_slice(), &[1, 4, 3]);
assert_eq!(cloned.as_slice(), &[1, 2, 3]);
assert_eq!(cloned.capacity(), 3);
assert_eq!(cloned2.as_slice(), &[1, 2, 3]);
assert_eq!(cloned2.capacity(), 3);
}
#[test]
fn clone_in_hash_map() {
let allocator = Allocator::default();
let mut original: HashMap<'_, &str, &str> = HashMap::with_capacity_in(8, &allocator);
original.extend(&[("x", "xx"), ("y", "yy"), ("z", "zz")]);
let cloned = original.clone_in(&allocator);
let cloned2 = original.clone_in_with_semantic_ids(&allocator);
*original.get_mut("y").unwrap() = "changed";
let mut original_as_vec = original.iter().collect::<std::vec::Vec<_>>();
original_as_vec.sort_unstable();
assert_eq!(original_as_vec, &[(&"x", &"xx"), (&"y", &"changed"), (&"z", &"zz")]);
assert_eq!(cloned.capacity(), 3);
let mut cloned_as_vec = cloned.iter().collect::<std::vec::Vec<_>>();
cloned_as_vec.sort_unstable();
assert_eq!(cloned_as_vec, &[(&"x", &"xx"), (&"y", &"yy"), (&"z", &"zz")]);
assert_eq!(cloned2.capacity(), 3);
let mut cloned2_as_vec = cloned2.iter().collect::<std::vec::Vec<_>>();
cloned2_as_vec.sort_unstable();
assert_eq!(cloned2_as_vec, &[(&"x", &"xx"), (&"y", &"yy"), (&"z", &"zz")]);
}
}