Skip to content

Commit

Permalink
src: reduce AsyncWrap memory footprint
Browse files Browse the repository at this point in the history
Fold two integral fields into one and use bitops to access/manipulate
them.

PR-URL: #667
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
bnoordhuis committed Feb 11, 2015
1 parent 7e779b4 commit 4bb3184
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/async-wrap-inl.h
Expand Up @@ -17,9 +17,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
v8::Handle<v8::Object> object,
ProviderType provider,
AsyncWrap* parent)
: BaseObject(env, object),
has_async_queue_(false),
provider_type_(provider) {
: BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1) {
// Check user controlled flag to see if the init callback should run.
if (!env->using_asyncwrap())
return;
Expand Down Expand Up @@ -49,7 +47,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
if (try_catch.HasCaught())
FatalError("node::AsyncWrap::AsyncWrap", "init hook threw");

has_async_queue_ = true;
bits_ |= 1; // has_async_queue() is true now.

if (parent != nullptr) {
env->async_hooks_post_function()->Call(parent_obj, 0, nullptr);
Expand All @@ -59,8 +57,13 @@ inline AsyncWrap::AsyncWrap(Environment* env,
}


inline uint32_t AsyncWrap::provider_type() const {
return provider_type_;
inline bool AsyncWrap::has_async_queue() const {
return static_cast<bool>(bits_ & 1);
}


inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
return static_cast<ProviderType>(bits_ >> 1);
}


Expand Down
4 changes: 2 additions & 2 deletions src/async-wrap.cc
Expand Up @@ -104,7 +104,7 @@ Handle<Value> AsyncWrap::MakeCallback(const Handle<Function> cb,
}
}

if (has_async_queue_) {
if (has_async_queue()) {
try_catch.SetVerbose(false);
env()->async_hooks_pre_function()->Call(context, 0, nullptr);
if (try_catch.HasCaught())
Expand All @@ -118,7 +118,7 @@ Handle<Value> AsyncWrap::MakeCallback(const Handle<Function> cb,
return Undefined(env()->isolate());
}

if (has_async_queue_) {
if (has_async_queue()) {
try_catch.SetVerbose(false);
env()->async_hooks_post_function()->Call(context, 0, nullptr);
if (try_catch.HasCaught())
Expand Down
8 changes: 5 additions & 3 deletions src/async-wrap.h
Expand Up @@ -4,6 +4,8 @@
#include "base-object.h"
#include "v8.h"

#include <stdint.h>

namespace node {

#define NODE_ASYNC_PROVIDER_TYPES(V) \
Expand Down Expand Up @@ -48,7 +50,7 @@ class AsyncWrap : public BaseObject {

inline virtual ~AsyncWrap() override = default;

inline uint32_t provider_type() const;
inline ProviderType provider_type() const;

// Only call these within a valid HandleScope.
v8::Handle<v8::Value> MakeCallback(const v8::Handle<v8::Function> cb,
Expand All @@ -63,12 +65,12 @@ class AsyncWrap : public BaseObject {

private:
inline AsyncWrap();
inline bool has_async_queue() const;

// When the async hooks init JS function is called from the constructor it is
// expected the context object will receive a _asyncQueue object property
// that will be used to call pre/post in MakeCallback.
bool has_async_queue_;
ProviderType provider_type_;
uint32_t bits_;
};

} // namespace node
Expand Down

0 comments on commit 4bb3184

Please sign in to comment.