From 6d01f1fa41947dc2ea087611ed443616b757fef8 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Wed, 4 Sep 2019 23:54:01 +0300 Subject: perf_hooks: remove GC callbacks on zero observers count When all existed PerformanceObserver instances removed for type `gc` GC callbacks should be removed. PR-URL: https://github.com/nodejs/node/pull/29444 Reviewed-By: Anna Henningsen Reviewed-By: Minwoo Jung --- src/node_perf.cc | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'src/node_perf.cc') diff --git a/src/node_perf.cc b/src/node_perf.cc index 3efaca6065..da711fee84 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -277,7 +277,13 @@ void MarkGarbageCollectionEnd(Isolate* isolate, }); } -static void SetupGarbageCollectionTracking( +void GarbageCollectionCleanupHook(void* data) { + Environment* env = static_cast(data); + env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data); + env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data); +} + +static void InstallGarbageCollectionTracking( const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -285,11 +291,15 @@ static void SetupGarbageCollectionTracking( static_cast(env)); env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd, static_cast(env)); - env->AddCleanupHook([](void* data) { - Environment* env = static_cast(data); - env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data); - env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data); - }, env); + env->AddCleanupHook(GarbageCollectionCleanupHook, env); +} + +static void RemoveGarbageCollectionTracking( + const FunctionCallbackInfo &args) { + Environment* env = Environment::GetCurrent(args); + + env->RemoveCleanupHook(GarbageCollectionCleanupHook, env); + GarbageCollectionCleanupHook(env); } // Gets the name of a function @@ -575,8 +585,12 @@ void Initialize(Local target, env->SetMethod(target, "markMilestone", MarkMilestone); env->SetMethod(target, "setupObservers", SetupPerformanceObservers); env->SetMethod(target, "timerify", Timerify); - env->SetMethod( - target, "setupGarbageCollectionTracking", SetupGarbageCollectionTracking); + env->SetMethod(target, + "installGarbageCollectionTracking", + InstallGarbageCollectionTracking); + env->SetMethod(target, + "removeGarbageCollectionTracking", + RemoveGarbageCollectionTracking); env->SetMethod(target, "notify", Notify); Local constants = Object::New(isolate); -- cgit v1.2.3