Browse Source

src: reduce AsyncWrap memory footprint

Fold two integral fields into one and use bitops to access/manipulate
them.

PR-URL: https://github.com/iojs/io.js/pull/667
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
v1.8.0-commit
Ben Noordhuis 10 years ago
parent
commit
4bb3184d8d
  1. 15
      src/async-wrap-inl.h
  2. 4
      src/async-wrap.cc
  3. 8
      src/async-wrap.h

15
src/async-wrap-inl.h

@ -17,9 +17,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
v8::Handle<v8::Object> object, v8::Handle<v8::Object> object,
ProviderType provider, ProviderType provider,
AsyncWrap* parent) AsyncWrap* parent)
: BaseObject(env, object), : BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1) {
has_async_queue_(false),
provider_type_(provider) {
// Check user controlled flag to see if the init callback should run. // Check user controlled flag to see if the init callback should run.
if (!env->using_asyncwrap()) if (!env->using_asyncwrap())
return; return;
@ -49,7 +47,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
if (try_catch.HasCaught()) if (try_catch.HasCaught())
FatalError("node::AsyncWrap::AsyncWrap", "init hook threw"); FatalError("node::AsyncWrap::AsyncWrap", "init hook threw");
has_async_queue_ = true; bits_ |= 1; // has_async_queue() is true now.
if (parent != nullptr) { if (parent != nullptr) {
env->async_hooks_post_function()->Call(parent_obj, 0, nullptr); env->async_hooks_post_function()->Call(parent_obj, 0, nullptr);
@ -59,8 +57,13 @@ inline AsyncWrap::AsyncWrap(Environment* env,
} }
inline uint32_t AsyncWrap::provider_type() const { inline bool AsyncWrap::has_async_queue() const {
return provider_type_; return static_cast<bool>(bits_ & 1);
}
inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
return static_cast<ProviderType>(bits_ >> 1);
} }

4
src/async-wrap.cc

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

8
src/async-wrap.h

@ -4,6 +4,8 @@
#include "base-object.h" #include "base-object.h"
#include "v8.h" #include "v8.h"
#include <stdint.h>
namespace node { namespace node {
#define NODE_ASYNC_PROVIDER_TYPES(V) \ #define NODE_ASYNC_PROVIDER_TYPES(V) \
@ -48,7 +50,7 @@ class AsyncWrap : public BaseObject {
inline virtual ~AsyncWrap() override = default; inline virtual ~AsyncWrap() override = default;
inline uint32_t provider_type() const; inline ProviderType provider_type() const;
// Only call these within a valid HandleScope. // Only call these within a valid HandleScope.
v8::Handle<v8::Value> MakeCallback(const v8::Handle<v8::Function> cb, v8::Handle<v8::Value> MakeCallback(const v8::Handle<v8::Function> cb,
@ -63,12 +65,12 @@ class AsyncWrap : public BaseObject {
private: private:
inline AsyncWrap(); inline AsyncWrap();
inline bool has_async_queue() const;
// When the async hooks init JS function is called from the constructor it is // When the async hooks init JS function is called from the constructor it is
// expected the context object will receive a _asyncQueue object property // expected the context object will receive a _asyncQueue object property
// that will be used to call pre/post in MakeCallback. // that will be used to call pre/post in MakeCallback.
bool has_async_queue_; uint32_t bits_;
ProviderType provider_type_;
}; };
} // namespace node } // namespace node

Loading…
Cancel
Save