Skip to content

Commit c85e2cc

Browse files
authored
chore(heap): Remove Option<T> from heap vectors (#880)
1 parent 56d2f43 commit c85e2cc

File tree

67 files changed

+426
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+426
-683
lines changed

nova_vm/src/ecmascript/abstract_operations/type_conversion.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
ecmascript::{
2525
builtins::{
2626
ArgumentsList,
27-
primitive_objects::{PrimitiveObjectData, PrimitiveObjectHeapData},
27+
primitive_objects::{PrimitiveObjectData, PrimitiveObjectRecord},
2828
},
2929
execution::{
3030
Agent, JsResult,
@@ -1144,67 +1144,67 @@ pub(crate) fn to_object<'a>(
11441144
// Return a new Boolean object whose [[BooleanData]] internal slot is set to argument.
11451145
Value::Boolean(bool) => Ok(agent
11461146
.heap
1147-
.create(PrimitiveObjectHeapData {
1147+
.create(PrimitiveObjectRecord {
11481148
object_index: None,
11491149
data: PrimitiveObjectData::Boolean(bool),
11501150
})
11511151
.into_object()),
11521152
// Return a new String object whose [[StringData]] internal slot is set to argument.
11531153
Value::String(str) => Ok(agent
11541154
.heap
1155-
.create(PrimitiveObjectHeapData {
1155+
.create(PrimitiveObjectRecord {
11561156
object_index: None,
11571157
data: PrimitiveObjectData::String(str.unbind()),
11581158
})
11591159
.into_object()),
11601160
Value::SmallString(str) => Ok(agent
11611161
.heap
1162-
.create(PrimitiveObjectHeapData {
1162+
.create(PrimitiveObjectRecord {
11631163
object_index: None,
11641164
data: PrimitiveObjectData::SmallString(str),
11651165
})
11661166
.into_object()),
11671167
// Return a new Symbol object whose [[SymbolnData]] internal slot is set to argument.
11681168
Value::Symbol(symbol) => Ok(agent
11691169
.heap
1170-
.create(PrimitiveObjectHeapData {
1170+
.create(PrimitiveObjectRecord {
11711171
object_index: None,
11721172
data: PrimitiveObjectData::Symbol(symbol.unbind()),
11731173
})
11741174
.into_object()),
11751175
// Return a new Number object whose [[NumberData]] internal slot is set to argument.
11761176
Value::Number(number) => Ok(agent
11771177
.heap
1178-
.create(PrimitiveObjectHeapData {
1178+
.create(PrimitiveObjectRecord {
11791179
object_index: None,
11801180
data: PrimitiveObjectData::Number(number.unbind()),
11811181
})
11821182
.into_object()),
11831183
Value::Integer(integer) => Ok(agent
11841184
.heap
1185-
.create(PrimitiveObjectHeapData {
1185+
.create(PrimitiveObjectRecord {
11861186
object_index: None,
11871187
data: PrimitiveObjectData::Integer(integer),
11881188
})
11891189
.into_object()),
11901190
Value::SmallF64(float) => Ok(agent
11911191
.heap
1192-
.create(PrimitiveObjectHeapData {
1192+
.create(PrimitiveObjectRecord {
11931193
object_index: None,
11941194
data: PrimitiveObjectData::SmallF64(float),
11951195
})
11961196
.into_object()),
11971197
// Return a new BigInt object whose [[BigIntData]] internal slot is set to argument.
11981198
Value::BigInt(bigint) => Ok(agent
11991199
.heap
1200-
.create(PrimitiveObjectHeapData {
1200+
.create(PrimitiveObjectRecord {
12011201
object_index: None,
12021202
data: PrimitiveObjectData::BigInt(bigint.unbind()),
12031203
})
12041204
.into_object()),
12051205
Value::SmallBigInt(bigint) => Ok(agent
12061206
.heap
1207-
.create(PrimitiveObjectHeapData {
1207+
.create(PrimitiveObjectRecord {
12081208
object_index: None,
12091209
data: PrimitiveObjectData::SmallBigInt(bigint),
12101210
})

nova_vm/src/ecmascript/builders/builtin_function_builder.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,7 @@ impl
595595
.builtin_functions
596596
.get_mut(self.this.get_index())
597597
.unwrap();
598-
assert!(slot.is_none());
599-
*slot = Some(data);
598+
*slot = data;
600599
self.this
601600
}
602601
}
@@ -648,8 +647,7 @@ impl
648647
.builtin_functions
649648
.get_mut(self.this.get_index())
650649
.unwrap();
651-
assert!(slot.is_none());
652-
*slot = Some(data);
650+
*slot = data;
653651
self.this
654652
}
655653
}
@@ -692,8 +690,7 @@ impl
692690
.builtin_functions
693691
.get_mut(self.this.get_index())
694692
.unwrap();
695-
assert!(slot.is_none());
696-
*slot = Some(data);
693+
*slot = data;
697694
self.this
698695
}
699696
}

nova_vm/src/ecmascript/builtins/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ impl<'a> CreateHeapData<ArrayHeapData<'a>, Array<'a>> for Heap {
940940
self.arrays
941941
.push(data.unbind())
942942
.expect("Failed to allocate Array");
943-
self.alloc_counter += core::mem::size_of::<Option<ArrayHeapData<'static>>>();
943+
self.alloc_counter += core::mem::size_of::<ArrayHeapData<'static>>();
944944
Array(BaseIndex::from_u32_index(i))
945945
}
946946
}

nova_vm/src/ecmascript/builtins/array_buffer.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,10 @@ impl ArrayBuffer<'_> {
205205
let array_buffers = &mut *agent.heap.array_buffers;
206206
let (source_data, target_data) = if self.get_index() > source.get_index() {
207207
let (before, after) = array_buffers.split_at_mut(self.get_index());
208-
(
209-
before[source.get_index()].as_ref().unwrap(),
210-
after[0].as_mut().unwrap(),
211-
)
208+
(&before[source.get_index()], &mut after[0])
212209
} else {
213210
let (before, after) = array_buffers.split_at_mut(source.get_index());
214-
(
215-
after[0].as_ref().unwrap(),
216-
before[self.get_index()].as_mut().unwrap(),
217-
)
211+
(&after[0], &mut before[self.get_index()])
218212
};
219213
let source_data = source_data.buffer.get_data_block();
220214
let target_data = target_data.buffer.get_data_block_mut();
@@ -269,23 +263,19 @@ impl IndexMut<ArrayBuffer<'_>> for Agent {
269263
}
270264
}
271265

272-
impl Index<ArrayBuffer<'_>> for Vec<Option<ArrayBufferHeapData<'static>>> {
266+
impl Index<ArrayBuffer<'_>> for Vec<ArrayBufferHeapData<'static>> {
273267
type Output = ArrayBufferHeapData<'static>;
274268

275269
fn index(&self, index: ArrayBuffer) -> &Self::Output {
276270
self.get(index.get_index())
277271
.expect("ArrayBuffer out of bounds")
278-
.as_ref()
279-
.expect("ArrayBuffer slot empty")
280272
}
281273
}
282274

283-
impl IndexMut<ArrayBuffer<'_>> for Vec<Option<ArrayBufferHeapData<'static>>> {
275+
impl IndexMut<ArrayBuffer<'_>> for Vec<ArrayBufferHeapData<'static>> {
284276
fn index_mut(&mut self, index: ArrayBuffer) -> &mut Self::Output {
285277
self.get_mut(index.get_index())
286278
.expect("ArrayBuffer out of bounds")
287-
.as_mut()
288-
.expect("ArrayBuffer slot empty")
289279
}
290280
}
291281

@@ -350,8 +340,8 @@ impl HeapSweepWeakReference for ArrayBuffer<'static> {
350340

351341
impl<'a> CreateHeapData<ArrayBufferHeapData<'a>, ArrayBuffer<'a>> for Heap {
352342
fn create(&mut self, data: ArrayBufferHeapData<'a>) -> ArrayBuffer<'a> {
353-
self.array_buffers.push(Some(data.unbind()));
354-
self.alloc_counter += core::mem::size_of::<Option<ArrayBufferHeapData<'static>>>();
343+
self.array_buffers.push(data.unbind());
344+
self.alloc_counter += core::mem::size_of::<ArrayBufferHeapData<'static>>();
355345
ArrayBuffer(BaseIndex::last(&self.array_buffers))
356346
}
357347
}

nova_vm/src/ecmascript/builtins/bound_function.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,30 +268,26 @@ impl<'a> IndexMut<BoundFunction<'a>> for Agent {
268268
}
269269
}
270270

271-
impl<'a> Index<BoundFunction<'a>> for Vec<Option<BoundFunctionHeapData<'static>>> {
271+
impl<'a> Index<BoundFunction<'a>> for Vec<BoundFunctionHeapData<'static>> {
272272
type Output = BoundFunctionHeapData<'static>;
273273

274274
fn index(&self, index: BoundFunction<'a>) -> &Self::Output {
275275
self.get(index.get_index())
276276
.expect("BoundFunction out of bounds")
277-
.as_ref()
278-
.expect("BoundFunction slot empty")
279277
}
280278
}
281279

282-
impl<'a> IndexMut<BoundFunction<'a>> for Vec<Option<BoundFunctionHeapData<'static>>> {
280+
impl<'a> IndexMut<BoundFunction<'a>> for Vec<BoundFunctionHeapData<'static>> {
283281
fn index_mut(&mut self, index: BoundFunction<'a>) -> &mut Self::Output {
284282
self.get_mut(index.get_index())
285283
.expect("BoundFunction out of bounds")
286-
.as_mut()
287-
.expect("BoundFunction slot empty")
288284
}
289285
}
290286

291287
impl<'a> CreateHeapData<BoundFunctionHeapData<'a>, BoundFunction<'a>> for Heap {
292288
fn create(&mut self, data: BoundFunctionHeapData<'a>) -> BoundFunction<'a> {
293-
self.bound_functions.push(Some(data.unbind()));
294-
self.alloc_counter += core::mem::size_of::<Option<BoundFunctionHeapData<'static>>>();
289+
self.bound_functions.push(data.unbind());
290+
self.alloc_counter += core::mem::size_of::<BoundFunctionHeapData<'static>>();
295291
BoundFunction(BaseIndex::last(&self.bound_functions))
296292
}
297293
}

nova_vm/src/ecmascript/builtins/builtin_constructor.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,19 @@ impl IndexMut<BuiltinConstructorFunction<'_>> for Agent {
121121
}
122122
}
123123

124-
impl Index<BuiltinConstructorFunction<'_>> for Vec<Option<BuiltinConstructorRecord<'static>>> {
124+
impl Index<BuiltinConstructorFunction<'_>> for Vec<BuiltinConstructorRecord<'static>> {
125125
type Output = BuiltinConstructorRecord<'static>;
126126

127127
fn index(&self, index: BuiltinConstructorFunction) -> &Self::Output {
128128
self.get(index.get_index())
129129
.expect("BuiltinConstructorFunction out of bounds")
130-
.as_ref()
131-
.expect("BuiltinConstructorFunction slot empty")
132130
}
133131
}
134132

135-
impl IndexMut<BuiltinConstructorFunction<'_>> for Vec<Option<BuiltinConstructorRecord<'static>>> {
133+
impl IndexMut<BuiltinConstructorFunction<'_>> for Vec<BuiltinConstructorRecord<'static>> {
136134
fn index_mut(&mut self, index: BuiltinConstructorFunction) -> &mut Self::Output {
137135
self.get_mut(index.get_index())
138136
.expect("BuiltinConstructorFunction out of bounds")
139-
.as_mut()
140-
.expect("BuiltinConstructorFunction slot empty")
141137
}
142138
}
143139

@@ -402,8 +398,8 @@ impl Rootable for BuiltinConstructorFunction<'_> {
402398

403399
impl<'a> CreateHeapData<BuiltinConstructorRecord<'a>, BuiltinConstructorFunction<'a>> for Heap {
404400
fn create(&mut self, data: BuiltinConstructorRecord) -> BuiltinConstructorFunction<'a> {
405-
self.builtin_constructors.push(Some(data.unbind()));
406-
self.alloc_counter += core::mem::size_of::<Option<BuiltinConstructorRecord<'static>>>();
401+
self.builtin_constructors.push(data.unbind());
402+
self.alloc_counter += core::mem::size_of::<BuiltinConstructorRecord<'static>>();
407403

408404
BuiltinConstructorFunction(BaseIndex::last(&self.builtin_constructors))
409405
}

nova_vm/src/ecmascript/builtins/builtin_function.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,10 @@ pub struct BuiltinFunction<'a>(BaseIndex<'a, BuiltinFunctionHeapData<'static>>);
450450
impl BuiltinFunction<'_> {
451451
/// Allocate a a new uninitialised (None) BuiltinFunction and return its reference.
452452
pub(crate) fn new_uninitialised(agent: &mut Agent) -> Self {
453-
agent.heap.builtin_functions.push(None);
453+
agent
454+
.heap
455+
.builtin_functions
456+
.push(BuiltinFunctionHeapData::BLANK);
454457
BuiltinFunction(BaseIndex::last(&agent.heap.builtin_functions))
455458
}
456459

@@ -536,23 +539,19 @@ impl IndexMut<BuiltinFunction<'_>> for Agent {
536539
}
537540
}
538541

539-
impl Index<BuiltinFunction<'_>> for Vec<Option<BuiltinFunctionHeapData<'static>>> {
542+
impl Index<BuiltinFunction<'_>> for Vec<BuiltinFunctionHeapData<'static>> {
540543
type Output = BuiltinFunctionHeapData<'static>;
541544

542545
fn index(&self, index: BuiltinFunction) -> &Self::Output {
543546
self.get(index.get_index())
544547
.expect("BuiltinFunction out of bounds")
545-
.as_ref()
546-
.expect("BuiltinFunction slot empty")
547548
}
548549
}
549550

550-
impl IndexMut<BuiltinFunction<'_>> for Vec<Option<BuiltinFunctionHeapData<'static>>> {
551+
impl IndexMut<BuiltinFunction<'_>> for Vec<BuiltinFunctionHeapData<'static>> {
551552
fn index_mut(&mut self, index: BuiltinFunction) -> &mut Self::Output {
552553
self.get_mut(index.get_index())
553554
.expect("BuiltinFunction out of bounds")
554-
.as_mut()
555-
.expect("BuiltinFunction slot empty")
556555
}
557556
}
558557

@@ -840,8 +839,8 @@ pub fn create_builtin_function<'a>(
840839

841840
impl<'a> CreateHeapData<BuiltinFunctionHeapData<'a>, BuiltinFunction<'a>> for Heap {
842841
fn create(&mut self, data: BuiltinFunctionHeapData<'a>) -> BuiltinFunction<'a> {
843-
self.builtin_functions.push(Some(data.unbind()));
844-
self.alloc_counter += core::mem::size_of::<Option<BuiltinFunctionHeapData<'static>>>();
842+
self.builtin_functions.push(data.unbind());
843+
self.alloc_counter += core::mem::size_of::<BuiltinFunctionHeapData<'static>>();
845844
BuiltinFunction(BaseIndex::last(&self.builtin_functions))
846845
}
847846
}

nova_vm/src/ecmascript/builtins/control_abstraction_objects/async_function_objects/await_reaction.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl AwaitReaction<'_> {
5353
Self(value, PhantomData, PhantomData)
5454
}
5555

56-
pub(crate) fn last(scripts: &[Option<AwaitReactionRecord>]) -> Self {
56+
pub(crate) fn last(scripts: &[AwaitReactionRecord]) -> Self {
5757
let index = scripts.len() - 1;
5858
Self::from_index(index)
5959
}
@@ -161,23 +161,19 @@ impl IndexMut<AwaitReaction<'_>> for Agent {
161161
}
162162
}
163163

164-
impl Index<AwaitReaction<'_>> for Vec<Option<AwaitReactionRecord<'static>>> {
164+
impl Index<AwaitReaction<'_>> for Vec<AwaitReactionRecord<'static>> {
165165
type Output = AwaitReactionRecord<'static>;
166166

167167
fn index(&self, index: AwaitReaction) -> &Self::Output {
168168
self.get(index.into_index())
169169
.expect("AwaitReactionIdentifier out of bounds")
170-
.as_ref()
171-
.expect("AwaitReactionIdentifier slot empty")
172170
}
173171
}
174172

175-
impl IndexMut<AwaitReaction<'_>> for Vec<Option<AwaitReactionRecord<'static>>> {
173+
impl IndexMut<AwaitReaction<'_>> for Vec<AwaitReactionRecord<'static>> {
176174
fn index_mut(&mut self, index: AwaitReaction) -> &mut Self::Output {
177175
self.get_mut(index.into_index())
178176
.expect("AwaitReactionIdentifier out of bounds")
179-
.as_mut()
180-
.expect("AwaitReactionIdentifier slot empty")
181177
}
182178
}
183179

@@ -255,8 +251,8 @@ pub struct AwaitReactionRecord<'a> {
255251

256252
impl<'a> CreateHeapData<AwaitReactionRecord<'a>, AwaitReaction<'a>> for Heap {
257253
fn create(&mut self, data: AwaitReactionRecord<'a>) -> AwaitReaction<'a> {
258-
self.await_reactions.push(Some(data.unbind()));
259-
self.alloc_counter += core::mem::size_of::<Option<AwaitReactionRecord<'static>>>();
254+
self.await_reactions.push(data.unbind());
255+
self.alloc_counter += core::mem::size_of::<AwaitReactionRecord<'static>>();
260256
AwaitReaction::last(&self.await_reactions)
261257
}
262258
}

nova_vm/src/ecmascript/builtins/control_abstraction_objects/async_generator_objects.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ impl<'a> InternalMethods<'a> for AsyncGenerator<'a> {}
379379

380380
impl<'a> CreateHeapData<AsyncGeneratorHeapData<'a>, AsyncGenerator<'a>> for Heap {
381381
fn create(&mut self, data: AsyncGeneratorHeapData<'a>) -> AsyncGenerator<'a> {
382-
self.async_generators.push(Some(data.unbind()));
383-
self.alloc_counter += core::mem::size_of::<Option<AsyncGeneratorHeapData<'static>>>();
382+
self.async_generators.push(data.unbind());
383+
self.alloc_counter += core::mem::size_of::<AsyncGeneratorHeapData<'static>>();
384384
AsyncGenerator(BaseIndex::last(&self.async_generators))
385385
}
386386
}
@@ -399,23 +399,19 @@ impl IndexMut<AsyncGenerator<'_>> for Agent {
399399
}
400400
}
401401

402-
impl Index<AsyncGenerator<'_>> for Vec<Option<AsyncGeneratorHeapData<'static>>> {
402+
impl Index<AsyncGenerator<'_>> for Vec<AsyncGeneratorHeapData<'static>> {
403403
type Output = AsyncGeneratorHeapData<'static>;
404404

405405
fn index(&self, index: AsyncGenerator) -> &Self::Output {
406406
self.get(index.get_index())
407407
.expect("AsyncGenerator out of bounds")
408-
.as_ref()
409-
.expect("AsyncGenerator slot empty")
410408
}
411409
}
412410

413-
impl IndexMut<AsyncGenerator<'_>> for Vec<Option<AsyncGeneratorHeapData<'static>>> {
411+
impl IndexMut<AsyncGenerator<'_>> for Vec<AsyncGeneratorHeapData<'static>> {
414412
fn index_mut(&mut self, index: AsyncGenerator) -> &mut Self::Output {
415413
self.get_mut(index.get_index())
416414
.expect("AsyncGenerator out of bounds")
417-
.as_mut()
418-
.expect("AsyncGenerator slot empty")
419415
}
420416
}
421417

0 commit comments

Comments
 (0)