From 4b84e42f67d7754574bf7d289524f6dffcb5e14a Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 27 Aug 2013 11:30:06 -0700 Subject: [PATCH] node: don't share state with in_tick/last_threw There was no need to share state between C++ and JS for these two values. So they have been moved to their respective locations. This will help performance only a tiny bit, but it does help code complexity much more. --- src/env-inl.h | 18 +++++++++++------- src/env.h | 11 ++++++----- src/node.cc | 12 +++++++++++- src/node.js | 19 ++++++------------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 0c5d272..b520e07 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -85,7 +85,7 @@ inline uint32_t Environment::DomainFlag::count() const { return fields_[kCount]; } -inline Environment::TickInfo::TickInfo() { +inline Environment::TickInfo::TickInfo() : in_tick_(false), last_threw_(false) { for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0; } @@ -97,28 +97,32 @@ inline int Environment::TickInfo::fields_count() const { return kFieldsCount; } -inline uint32_t Environment::TickInfo::in_tick() const { - return fields_[kInTick]; +inline bool Environment::TickInfo::in_tick() const { + return in_tick_; } inline uint32_t Environment::TickInfo::index() const { return fields_[kIndex]; } -inline uint32_t Environment::TickInfo::last_threw() const { - return fields_[kLastThrew]; +inline bool Environment::TickInfo::last_threw() const { + return last_threw_; } inline uint32_t Environment::TickInfo::length() const { return fields_[kLength]; } +inline void Environment::TickInfo::set_in_tick(bool value) { + in_tick_ = value; +} + inline void Environment::TickInfo::set_index(uint32_t value) { fields_[kIndex] = value; } -inline void Environment::TickInfo::set_last_threw(uint32_t value) { - fields_[kLastThrew] = value; +inline void Environment::TickInfo::set_last_threw(bool value) { + last_threw_ = value; } inline Environment* Environment::New(v8::Local context) { diff --git a/src/env.h b/src/env.h index 5814b44..b45d250 100644 --- a/src/env.h +++ b/src/env.h @@ -187,26 +187,27 @@ class Environment { public: inline uint32_t* fields(); inline int fields_count() const; - inline uint32_t in_tick() const; + inline bool in_tick() const; + inline bool last_threw() const; inline uint32_t index() const; - inline uint32_t last_threw() const; inline uint32_t length() const; + inline void set_in_tick(bool value); inline void set_index(uint32_t value); - inline void set_last_threw(uint32_t value); + inline void set_last_threw(bool value); private: friend class Environment; // So we can call the constructor. inline TickInfo(); enum Fields { - kInTick, kIndex, - kLastThrew, kLength, kFieldsCount }; uint32_t fields_[kFieldsCount]; + bool in_tick_; + bool last_threw_; DISALLOW_COPY_AND_ASSIGN(TickInfo); }; diff --git a/src/node.cc b/src/node.cc index dc3d4da..b6bd8b7 100644 --- a/src/node.cc +++ b/src/node.cc @@ -948,7 +948,7 @@ Handle MakeDomainCallback(Environment* env, return ret; } - if (tick_info->in_tick() == 1) { + if (tick_info->in_tick()) { return ret; } @@ -957,12 +957,17 @@ Handle MakeDomainCallback(Environment* env, return ret; } + tick_info->set_in_tick(true); + // process nextTicks after call Local process_object = env->process_object(); Local tick_callback_function = env->tick_callback_function(); tick_callback_function->Call(process_object, 0, NULL); + tick_info->set_in_tick(false); + if (try_catch.HasCaught()) { + tick_info->set_last_threw(true); return Undefined(node_isolate); } @@ -1004,6 +1009,8 @@ Handle MakeCallback(Environment* env, return ret; } + tick_info->set_in_tick(true); + // lazy load no domain next tick callbacks Local tick_callback_function = env->tick_callback_function(); if (tick_callback_function.IsEmpty()) { @@ -1021,7 +1028,10 @@ Handle MakeCallback(Environment* env, // process nextTicks after call tick_callback_function->Call(process_object, 0, NULL); + tick_info->set_in_tick(false); + if (try_catch.HasCaught()) { + tick_info->set_last_threw(true); return Undefined(node_isolate); } diff --git a/src/node.js b/src/node.js index 150cfdf..8692a2c 100644 --- a/src/node.js +++ b/src/node.js @@ -278,10 +278,8 @@ var tickInfo = process._tickInfo; // *Must* match Environment::TickInfo::Fields in src/env.h. - var kInTick = 0; - var kIndex = 1; - var kLastThrew = 2; - var kLength = 3; + var kIndex = 0; + var kLength = 1; process.nextTick = nextTick; // needs to be accessible from cc land @@ -298,7 +296,6 @@ tickInfo[kLength] = nextTickQueue.length; } } - tickInfo[kInTick] = 0; tickInfo[kIndex] = 0; } @@ -307,8 +304,6 @@ function _tickCallback() { var callback, threw; - tickInfo[kInTick] = 1; - while (tickInfo[kIndex] < tickInfo[kLength]) { callback = nextTickQueue[tickInfo[kIndex]++].callback; threw = true; @@ -324,9 +319,7 @@ } function _tickDomainCallback() { - var tock, callback, domain; - - tickInfo[kInTick] = 1; + var tock, callback, threw, domain; while (tickInfo[kIndex] < tickInfo[kLength]) { tock = nextTickQueue[tickInfo[kIndex]++]; @@ -336,12 +329,12 @@ if (domain._disposed) continue; domain.enter(); } - tickInfo[kLastThrew] = 1; + threw = true; try { callback(); - tickInfo[kLastThrew] = 0; + threw = false; } finally { - if (tickInfo[kLastThrew] === 1) tickDone(); + if (threw) tickDone(); } if (domain) domain.exit(); -- 2.7.4