node: don't share state with in_tick/last_threw
authorTrevor Norris <trev.norris@gmail.com>
Tue, 27 Aug 2013 18:30:06 +0000 (11:30 -0700)
committerTrevor Norris <trev.norris@gmail.com>
Tue, 29 Oct 2013 22:09:44 +0000 (15:09 -0700)
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
src/env.h
src/node.cc
src/node.js

index 0c5d272..b520e07 100644 (file)
@@ -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<v8::Context> context) {
index 5814b44..b45d250 100644 (file)
--- 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);
   };
index dc3d4da..b6bd8b7 100644 (file)
@@ -948,7 +948,7 @@ Handle<Value> MakeDomainCallback(Environment* env,
     return ret;
   }
 
-  if (tick_info->in_tick() == 1) {
+  if (tick_info->in_tick()) {
     return ret;
   }
 
@@ -957,12 +957,17 @@ Handle<Value> MakeDomainCallback(Environment* env,
     return ret;
   }
 
+  tick_info->set_in_tick(true);
+
   // process nextTicks after call
   Local<Object> process_object = env->process_object();
   Local<Function> 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<Value> MakeCallback(Environment* env,
     return ret;
   }
 
+  tick_info->set_in_tick(true);
+
   // lazy load no domain next tick callbacks
   Local<Function> tick_callback_function = env->tick_callback_function();
   if (tick_callback_function.IsEmpty()) {
@@ -1021,7 +1028,10 @@ Handle<Value> 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);
   }
 
index 150cfdf..8692a2c 100644 (file)
     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
           tickInfo[kLength] = nextTickQueue.length;
         }
       }
-      tickInfo[kInTick] = 0;
       tickInfo[kIndex] = 0;
     }
 
     function _tickCallback() {
       var callback, threw;
 
-      tickInfo[kInTick] = 1;
-
       while (tickInfo[kIndex] < tickInfo[kLength]) {
         callback = nextTickQueue[tickInfo[kIndex]++].callback;
         threw = true;
     }
 
     function _tickDomainCallback() {
-      var tock, callback, domain;
-
-      tickInfo[kInTick] = 1;
+      var tock, callback, threw, domain;
 
       while (tickInfo[kIndex] < tickInfo[kLength]) {
         tock = nextTickQueue[tickInfo[kIndex]++];
           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();