From aafb224147555c59d5abd277cc45f76668ac7046 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 15 Jan 2020 20:20:06 +0100 Subject: [PATCH] src: exclude C++ SetImmediate() from count There is no real reason to manage a count manually, given that checking whether there are C++ callbacks is a single pointer comparison. This makes it easier to add other kinds of native C++ callbacks that are managed in a similar way. Backport-PR-URL: https://github.com/nodejs/node/pull/32301 PR-URL: https://github.com/nodejs/node/pull/31386 Refs: https://github.com/openjs-foundation/summit/pull/240 Reviewed-By: Gireesh Punathil Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott --- src/env-inl.h | 9 --------- src/env.cc | 12 ++++-------- src/env.h | 2 -- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 5ad84e8222a52f..0de92c25cb7997 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -242,14 +242,6 @@ inline bool ImmediateInfo::has_outstanding() const { return fields_[kHasOutstanding] == 1; } -inline void ImmediateInfo::count_inc(uint32_t increment) { - fields_[kCount] += increment; -} - -inline void ImmediateInfo::count_dec(uint32_t decrement) { - fields_[kCount] -= decrement; -} - inline void ImmediateInfo::ref_count_inc(uint32_t increment) { fields_[kRefCount] += increment; } @@ -771,7 +763,6 @@ void Environment::CreateImmediate(Fn&& cb, bool ref) { auto callback = std::make_unique>( std::move(cb), ref); native_immediates_.Push(std::move(callback)); - immediate_info()->count_inc(1); } template diff --git a/src/env.cc b/src/env.cc index 61832920347c6b..ea0f2a4004a2c6 100644 --- a/src/env.cc +++ b/src/env.cc @@ -664,7 +664,6 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) { TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), "RunAndClearNativeImmediates", this); size_t ref_count = 0; - size_t count = 0; NativeImmediateQueue queue; queue.ConcatMove(std::move(native_immediates_)); @@ -673,7 +672,6 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) { TryCatchScope try_catch(this); DebugSealHandleScope seal_handle_scope(isolate()); while (std::unique_ptr head = queue.Shift()) { - count++; if (head->is_refed()) ref_count++; @@ -691,9 +689,10 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) { }; while (queue.size() > 0 && drain_list()) {} - DCHECK_GE(immediate_info()->count(), count); - immediate_info()->count_dec(count); immediate_info()->ref_count_dec(ref_count); + + if (immediate_info()->ref_count() == 0) + ToggleImmediateRef(false); } @@ -779,15 +778,12 @@ void Environment::CheckImmediate(uv_check_t* handle) { TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), "CheckImmediate", env); - if (env->immediate_info()->count() == 0) - return; - HandleScope scope(env->isolate()); Context::Scope context_scope(env->context()); env->RunAndClearNativeImmediates(); - if (!env->can_call_into_js()) + if (env->immediate_info()->count() == 0 || !env->can_call_into_js()) return; do { diff --git a/src/env.h b/src/env.h index ce4668b5839968..86a8f43858f5e2 100644 --- a/src/env.h +++ b/src/env.h @@ -734,8 +734,6 @@ class ImmediateInfo : public MemoryRetainer { inline uint32_t count() const; inline uint32_t ref_count() const; inline bool has_outstanding() const; - inline void count_inc(uint32_t increment); - inline void count_dec(uint32_t decrement); inline void ref_count_inc(uint32_t increment); inline void ref_count_dec(uint32_t decrement);