From 3b0617dd19070ebb59149b091452858aeb7b233d Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 31 Mar 2024 22:27:33 +0200 Subject: [PATCH] vm: migrate ContextifyScript to cppgc This patch migrates ContextifyScript to cppgc-based memory management using CppgcMixin. PR-URL: https://github.com/nodejs/node/pull/52295 Refs: https://github.com/nodejs/node/issues/40786 Refs: https://docs.google.com/document/d/1ny2Qz_EsUnXGKJRGxoA-FXIE2xpLgaMAN6jD7eAkqFQ/edit Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu Reviewed-By: Stephen Belanger --- .../vm/compile-script-in-isolate-cache.js | 12 ++--- src/node_contextify.cc | 48 ++++++++++++------- src/node_contextify.h | 19 ++++---- test/pummel/test-heapdump-env.js | 22 +-------- test/pummel/test-heapdump-vm-script.js | 13 +++++ 5 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 test/pummel/test-heapdump-vm-script.js diff --git a/benchmark/vm/compile-script-in-isolate-cache.js b/benchmark/vm/compile-script-in-isolate-cache.js index 7eceb0eba0d215..7c909d840b4316 100644 --- a/benchmark/vm/compile-script-in-isolate-cache.js +++ b/benchmark/vm/compile-script-in-isolate-cache.js @@ -5,17 +5,17 @@ const common = require('../common.js'); const fs = require('fs'); const vm = require('vm'); -const fixtures = require('../../test/common/fixtures.js'); -const scriptPath = fixtures.path('snapshot', 'typescript.js'); +const path = require('path'); const bench = common.createBenchmark(main, { type: ['with-dynamic-import-callback', 'without-dynamic-import-callback'], - n: [100], + filename: ['test/fixtures/snapshot/typescript.js', 'test/fixtures/syntax/good_syntax.js'], + n: [1000], }); -const scriptSource = fs.readFileSync(scriptPath, 'utf8'); - -function main({ n, type }) { +function main({ n, type, filename }) { + const scriptPath = path.resolve(__dirname, '..', '..', filename); + const scriptSource = fs.readFileSync(scriptPath, 'utf8'); let script; bench.start(); const options = {}; diff --git a/src/node_contextify.cc b/src/node_contextify.cc index fc137486f5e6ba..7b4f1f27506adf 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -22,6 +22,7 @@ #include "node_contextify.h" #include "base_object-inl.h" +#include "cppgc/allocation.h" #include "memory_tracker-inl.h" #include "module_wrap.h" #include "node_context_data.h" @@ -960,6 +961,12 @@ void ContextifyScript::RegisterExternalReferences( registry->Register(RunInContext); } +ContextifyScript* ContextifyScript::New(Environment* env, + Local object) { + return cppgc::MakeGarbageCollected( + env->isolate()->GetCppHeap()->GetAllocationHandle(), env, object); +} + void ContextifyScript::New(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); @@ -1010,8 +1017,7 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { id_symbol = args[7].As(); } - ContextifyScript* contextify_script = - new ContextifyScript(env, args.This()); + ContextifyScript* contextify_script = New(env, args.This()); if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( TRACING_CATEGORY_NODE2(vm, script)) != 0) { @@ -1069,9 +1075,7 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { return; } - contextify_script->script_.Reset(isolate, v8_script); - contextify_script->script_.SetWeak(); - contextify_script->object()->SetInternalField(kUnboundScriptSlot, v8_script); + contextify_script->set_unbound_script(v8_script); std::unique_ptr new_cached_data; if (produce_cached_data) { @@ -1174,11 +1178,9 @@ void ContextifyScript::CreateCachedData( const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); ContextifyScript* wrapped_script; - ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.This()); - Local unbound_script = - PersistentToLocal::Default(env->isolate(), wrapped_script->script_); + ASSIGN_OR_RETURN_UNWRAP_CPPGC(&wrapped_script, args.This()); std::unique_ptr cached_data( - ScriptCompiler::CreateCodeCache(unbound_script)); + ScriptCompiler::CreateCodeCache(wrapped_script->unbound_script())); if (!cached_data) { args.GetReturnValue().Set(Buffer::New(env, 0).ToLocalChecked()); } else { @@ -1192,9 +1194,8 @@ void ContextifyScript::CreateCachedData( void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - ContextifyScript* wrapped_script; - ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.This()); + ASSIGN_OR_RETURN_UNWRAP_CPPGC(&wrapped_script, args.This()); CHECK_EQ(args.Length(), 5); CHECK(args[0]->IsObject() || args[0]->IsNull()); @@ -1264,10 +1265,9 @@ bool ContextifyScript::EvalMachine(Local context, TryCatchScope try_catch(env); ContextifyScript* wrapped_script; - ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.This(), false); - Local unbound_script = - PersistentToLocal::Default(env->isolate(), wrapped_script->script_); - Local