From 07c6fb983b9c57753e2a9af9644720ff027355ac Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 27 Dec 2017 13:47:51 -0500 Subject: [PATCH] src: use AliasedBuffer for TickInfo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backport-PR-URL: https://github.com/nodejs/node/pull/19006 PR-URL: https://github.com/nodejs/node/pull/17881 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Reviewed-By: Khaidi Chu Reviewed-By: Tobias Nießen --- lib/internal/process/next_tick.js | 6 +++--- src/env-inl.h | 17 ++++++----------- src/env.h | 11 +++++------ src/node.cc | 14 +++----------- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index 2f68783d91800d..9481bebd224be6 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -29,7 +29,7 @@ function setupNextTick() { ] = process._setupNextTick(_tickCallback); // *Must* match Environment::TickInfo::Fields in src/env.h. - const kScheduled = 0; + const kHasScheduled = 0; const nextTickQueue = { head: null, @@ -40,7 +40,7 @@ function setupNextTick() { this.tail.next = entry; } else { this.head = entry; - tickInfo[kScheduled] = 1; + tickInfo[kHasScheduled] = 1; } this.tail = entry; }, @@ -50,7 +50,7 @@ function setupNextTick() { const ret = this.head.data; if (this.head === this.tail) { this.head = this.tail = null; - tickInfo[kScheduled] = 0; + tickInfo[kHasScheduled] = 0; } else { this.head = this.head.next; } diff --git a/src/env-inl.h b/src/env-inl.h index 476be6763d1248..a4a27cbfe69ff8 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -217,21 +217,15 @@ inline bool Environment::AsyncCallbackScope::in_makecallback() const { return env_->makecallback_cntr_ > 1; } -inline Environment::TickInfo::TickInfo() { - for (int i = 0; i < kFieldsCount; ++i) - fields_[i] = 0; -} +inline Environment::TickInfo::TickInfo(v8::Isolate* isolate) + : fields_(isolate, kFieldsCount) {} -inline uint8_t* Environment::TickInfo::fields() { +inline AliasedBuffer& Environment::TickInfo::fields() { return fields_; } -inline int Environment::TickInfo::fields_count() const { - return kFieldsCount; -} - -inline uint8_t Environment::TickInfo::scheduled() const { - return fields_[kScheduled]; +inline bool Environment::TickInfo::has_scheduled() const { + return fields_[kHasScheduled] == 1; } inline void Environment::AssignToContext(v8::Local context, @@ -269,6 +263,7 @@ inline Environment::Environment(IsolateData* isolate_data, v8::Local context) : isolate_(context->GetIsolate()), isolate_data_(isolate_data), + tick_info_(context->GetIsolate()), timer_base_(uv_now(isolate_data->event_loop())), printed_error_(false), trace_sync_io_(false), diff --git a/src/env.h b/src/env.h index fa6ecb2e6bd69c..d8012f7bcba762 100644 --- a/src/env.h +++ b/src/env.h @@ -453,20 +453,19 @@ class Environment { class TickInfo { public: - inline uint8_t* fields(); - inline int fields_count() const; - inline uint8_t scheduled() const; + inline AliasedBuffer& fields(); + inline bool has_scheduled() const; private: friend class Environment; // So we can call the constructor. - inline TickInfo(); + inline explicit TickInfo(v8::Isolate* isolate); enum Fields { - kScheduled, + kHasScheduled, kFieldsCount }; - uint8_t fields_[kFieldsCount]; + AliasedBuffer fields_; DISALLOW_COPY_AND_ASSIGN(TickInfo); }; diff --git a/src/node.cc b/src/node.cc index 34422a373b9c65..9e84dda74a9daa 100644 --- a/src/node.cc +++ b/src/node.cc @@ -169,7 +169,6 @@ using v8::SealHandleScope; using v8::String; using v8::TryCatch; using v8::Uint32Array; -using v8::Uint8Array; using v8::Undefined; using v8::V8; using v8::Value; @@ -1152,13 +1151,6 @@ void SetupNextTick(const FunctionCallbackInfo& args) { env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "_setupNextTick")).FromJust(); - // Values use to cross communicate with processNextTick. - uint8_t* const fields = env->tick_info()->fields(); - uint8_t const fields_count = env->tick_info()->fields_count(); - - Local array_buffer = - ArrayBuffer::New(env->isolate(), fields, sizeof(*fields) * fields_count); - v8::Local run_microtasks_fn = env->NewFunctionTemplate(RunMicrotasks)->GetFunction(env->context()) .ToLocalChecked(); @@ -1167,7 +1159,7 @@ void SetupNextTick(const FunctionCallbackInfo& args) { Local ret = Array::New(env->isolate(), 2); ret->Set(env->context(), 0, - Uint8Array::New(array_buffer, 0, fields_count)).FromJust(); + env->tick_info()->fields().GetJSArray()).FromJust(); ret->Set(env->context(), 1, run_microtasks_fn).FromJust(); args.GetReturnValue().Set(ret); @@ -1286,7 +1278,7 @@ void InternalCallbackScope::Close() { Environment::TickInfo* tick_info = env_->tick_info(); - if (tick_info->scheduled() == 0) { + if (!tick_info->has_scheduled()) { env_->isolate()->RunMicrotasks(); } @@ -1297,7 +1289,7 @@ void InternalCallbackScope::Close() { CHECK_EQ(env_->trigger_async_id(), 0); } - if (tick_info->scheduled() == 0) { + if (!tick_info->has_scheduled()) { return; }