Commit 598f6c34 authored by Jakob Kummerow's avatar Jakob Kummerow Committed by V8 LUCI CQ

[wasm-c-api] Fix unnecessary heap memory growth

When adding new instances to the WeakArrayList of a script's
instances, use Append() instead of AddToEnd() to allow possible
compaction of the WAL.
Also, check for interrupt requests every now and then, which
allows incremental marking to get finalized when appropriate.

Bug: v8:11774
Change-Id: I8be959a78d8ef370f65e8f9849f48e34c9691a13
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2905603
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: 's avatarManos Koukoutos <manoskouk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74681}
parent f9a15e1c
......@@ -265,6 +265,17 @@ auto Engine::make(own<Config>&& config) -> own<Engine> {
return make_own(seal<Engine>(engine));
}
// This should be called somewhat regularly, especially on potentially hot
// sections of pure C++ execution. To achieve that, we call it on API entry
// points that heap-allocate but don't call into generated code.
// For example, finalization of incremental marking is relying on it.
void CheckAndHandleInterrupts(i::Isolate* isolate) {
i::StackLimitCheck check(isolate);
if (check.InterruptRequested()) {
isolate->stack_guard()->HandleInterrupts();
}
}
// Stores
StoreImpl::~StoreImpl() {
......@@ -969,6 +980,7 @@ auto Module::make(Store* store_abs, const vec<byte_t>& binary) -> own<Module> {
StoreImpl* store = impl(store_abs);
i::Isolate* isolate = store->i_isolate();
i::HandleScope scope(isolate);
CheckAndHandleInterrupts(isolate);
i::wasm::ModuleWireBytes bytes(
{reinterpret_cast<const uint8_t*>(binary.get()), binary.size()});
i::wasm::WasmFeatures features = i::wasm::WasmFeatures::FromIsolate(isolate);
......@@ -1272,6 +1284,7 @@ auto make_func(Store* store_abs, FuncData* data) -> own<Func> {
auto store = impl(store_abs);
i::Isolate* isolate = store->i_isolate();
i::HandleScope handle_scope(isolate);
CheckAndHandleInterrupts(isolate);
i::Handle<i::Managed<FuncData>> embedder_data =
i::Managed<FuncData>::FromRawPtr(isolate, sizeof(FuncData), data);
i::Handle<i::WasmCapiFunction> function = i::WasmCapiFunction::New(
......@@ -1648,6 +1661,7 @@ auto Global::make(Store* store_abs, const GlobalType* type, const Val& val)
StoreImpl* store = impl(store_abs);
i::Isolate* isolate = store->i_isolate();
i::HandleScope handle_scope(isolate);
CheckAndHandleInterrupts(isolate);
DCHECK_EQ(type->content()->kind(), val.kind());
......@@ -1749,6 +1763,7 @@ auto Table::make(Store* store_abs, const TableType* type, const Ref* ref)
StoreImpl* store = impl(store_abs);
i::Isolate* isolate = store->i_isolate();
i::HandleScope scope(isolate);
CheckAndHandleInterrupts(isolate);
// Get "element".
i::wasm::ValueType i_type;
......@@ -1868,6 +1883,7 @@ auto Memory::make(Store* store_abs, const MemoryType* type) -> own<Memory> {
StoreImpl* store = impl(store_abs);
i::Isolate* isolate = store->i_isolate();
i::HandleScope scope(isolate);
CheckAndHandleInterrupts(isolate);
const Limits& limits = type->limits();
uint32_t minimum = limits.min;
......@@ -1938,6 +1954,7 @@ own<Instance> Instance::make(Store* store_abs, const Module* module_abs,
const implement<Module>::type* module = impl(module_abs);
i::Isolate* isolate = store->i_isolate();
i::HandleScope handle_scope(isolate);
CheckAndHandleInterrupts(isolate);
DCHECK_EQ(module->v8_object()->GetIsolate(), isolate);
......@@ -2007,6 +2024,7 @@ auto Instance::exports() const -> ownvec<Extern> {
StoreImpl* store = instance->store();
i::Isolate* isolate = store->i_isolate();
i::HandleScope handle_scope(isolate);
CheckAndHandleInterrupts(isolate);
i::Handle<i::WasmInstanceObject> instance_obj = instance->v8_object();
i::Handle<i::WasmModuleObject> module_obj(instance_obj->module_object(),
isolate);
......
......@@ -877,7 +877,7 @@ void WasmMemoryObject::AddInstance(Isolate* isolate,
? Handle<WeakArrayList>(memory->instances(), isolate)
: handle(ReadOnlyRoots(isolate->heap()).empty_weak_array_list(),
isolate);
Handle<WeakArrayList> new_instances = WeakArrayList::AddToEnd(
Handle<WeakArrayList> new_instances = WeakArrayList::Append(
isolate, old_instances, MaybeObjectHandle::Weak(instance));
memory->set_instances(*new_instances);
Handle<JSArrayBuffer> buffer(memory->array_buffer(), isolate);
......@@ -1292,11 +1292,10 @@ Handle<WasmInstanceObject> WasmInstanceObject::New(
// Insert the new instance into the scripts weak list of instances. This list
// is used for breakpoints affecting all instances belonging to the script.
// TODO(wasm): Allow to reuse holes in the {WeakArrayList} below.
if (module_object->script().type() == Script::TYPE_WASM) {
Handle<WeakArrayList> weak_instance_list(
module_object->script().wasm_weak_instance_list(), isolate);
weak_instance_list = WeakArrayList::AddToEnd(
weak_instance_list = WeakArrayList::Append(
isolate, weak_instance_list, MaybeObjectHandle::Weak(instance));
module_object->script().set_wasm_weak_instance_list(*weak_instance_list);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment