Skip to content

Commit dffaead

Browse files
committed
rt: Create handle scope around boot compilation
This fixes OOM bug when compiling lots of functions.
1 parent ced1662 commit dffaead

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

dora-runtime/src/boots.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::boots::serializer::{ByteBuffer, encode_compilation_info};
1414
use crate::cannon::codegen::get_function_address as get_function_address_raw;
1515
use crate::compiler::{CompilationData, CompilationMode};
1616
use crate::gc::Address;
17-
use crate::handle::{Handle, create_handle};
17+
use crate::handle::{Handle, create_handle, handle_scope};
1818
use crate::mirror::{Object, Ref, Str, UInt8Array, byte_array_from_buffer};
1919
use crate::threads::current_thread;
2020
use crate::vm::compute_vtable_index;
@@ -144,37 +144,39 @@ pub fn compile(
144144
compilation_data: CompilationData,
145145
mode: CompilationMode,
146146
) -> CodeDescriptor {
147-
let mut buffer = ByteBuffer::new();
148-
encode_compilation_info(vm, &compilation_data, mode, &mut buffer);
149-
150-
let encoded_compilation_info: Handle<Object> =
151-
create_handle(byte_array_from_buffer(vm, buffer.data()).cast());
152-
153-
let tld_address = current_thread().tld_address();
154-
155-
let dora_stub_address = vm.native_methods.dora_entry_trampoline();
156-
let compile_fct_ptr: extern "C" fn(Address, Address, Address) -> Ref<UInt8Array> =
157-
unsafe { mem::transmute(dora_stub_address) };
158-
159-
let machine_code = create_handle(compile_fct_ptr(
160-
tld_address,
161-
compile_address,
162-
encoded_compilation_info.direct_ptr(),
163-
));
164-
let mut serialized_data = vec![0; machine_code.len()];
165-
166-
unsafe {
167-
ptr::copy_nonoverlapping(
168-
machine_code.data() as *mut u8,
169-
serialized_data.as_mut_ptr(),
170-
machine_code.len(),
171-
);
172-
}
173-
174-
let mut reader = ByteReader::new(serialized_data);
175-
let code = decode_code_descriptor(&mut reader);
176-
assert!(!reader.has_more());
177-
code
147+
handle_scope(|| {
148+
let mut buffer = ByteBuffer::new();
149+
encode_compilation_info(vm, &compilation_data, mode, &mut buffer);
150+
151+
let encoded_compilation_info: Handle<Object> =
152+
create_handle(byte_array_from_buffer(vm, buffer.data()).cast());
153+
154+
let tld_address = current_thread().tld_address();
155+
156+
let dora_stub_address = vm.native_methods.dora_entry_trampoline();
157+
let compile_fct_ptr: extern "C" fn(Address, Address, Address) -> Ref<UInt8Array> =
158+
unsafe { mem::transmute(dora_stub_address) };
159+
160+
let machine_code = create_handle(compile_fct_ptr(
161+
tld_address,
162+
compile_address,
163+
encoded_compilation_info.direct_ptr(),
164+
));
165+
let mut serialized_data = vec![0; machine_code.len()];
166+
167+
unsafe {
168+
ptr::copy_nonoverlapping(
169+
machine_code.data() as *mut u8,
170+
serialized_data.as_mut_ptr(),
171+
machine_code.len(),
172+
);
173+
}
174+
175+
let mut reader = ByteReader::new(serialized_data);
176+
let code = decode_code_descriptor(&mut reader);
177+
assert!(!reader.has_more());
178+
code
179+
})
178180
}
179181

180182
extern "C" fn get_function_address(data: Handle<UInt8Array>) -> Address {

0 commit comments

Comments
 (0)