Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
node: don't share state with in_tick/last_threw
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
trevnorris committed Oct 29, 2013
1 parent a9a53ca commit 4b84e42
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
18 changes: 11 additions & 7 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
12 changes: 11 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ Handle<Value> MakeDomainCallback(Environment* env,
return ret;
}

if (tick_info->in_tick() == 1) {
if (tick_info->in_tick()) {
return ret;
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}

Expand Down
19 changes: 6 additions & 13 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -298,7 +296,6 @@
tickInfo[kLength] = nextTickQueue.length;
}
}
tickInfo[kInTick] = 0;
tickInfo[kIndex] = 0;
}

Expand All @@ -307,8 +304,6 @@
function _tickCallback() {
var callback, threw;

tickInfo[kInTick] = 1;

while (tickInfo[kIndex] < tickInfo[kLength]) {
callback = nextTickQueue[tickInfo[kIndex]++].callback;
threw = true;
Expand All @@ -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]++];
Expand All @@ -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();
Expand Down

0 comments on commit 4b84e42

Please sign in to comment.