From 4bb3184d8d08208bfb5233c394cbc1994a531b5f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 30 Jan 2015 15:15:56 +0100 Subject: [PATCH] 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 Reviewed-By: Trevor Norris --- src/async-wrap-inl.h | 15 +++++++++------ src/async-wrap.cc | 4 ++-- src/async-wrap.h | 8 +++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/async-wrap-inl.h b/src/async-wrap-inl.h index f0b5c1ea08f16a..b08f791c2e7635 100644 --- a/src/async-wrap-inl.h +++ b/src/async-wrap-inl.h @@ -17,9 +17,7 @@ inline AsyncWrap::AsyncWrap(Environment* env, v8::Handle object, ProviderType provider, AsyncWrap* parent) - : BaseObject(env, object), - has_async_queue_(false), - provider_type_(provider) { + : BaseObject(env, object), bits_(static_cast(provider) << 1) { // Check user controlled flag to see if the init callback should run. if (!env->using_asyncwrap()) return; @@ -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); @@ -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(bits_ & 1); +} + + +inline AsyncWrap::ProviderType AsyncWrap::provider_type() const { + return static_cast(bits_ >> 1); } diff --git a/src/async-wrap.cc b/src/async-wrap.cc index 3ee97f6a820c00..7887caf4f5b027 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -104,7 +104,7 @@ Handle AsyncWrap::MakeCallback(const Handle 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()) @@ -118,7 +118,7 @@ Handle AsyncWrap::MakeCallback(const Handle 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()) diff --git a/src/async-wrap.h b/src/async-wrap.h index 6022ed2fbc5403..86748a5fefd89b 100644 --- a/src/async-wrap.h +++ b/src/async-wrap.h @@ -4,6 +4,8 @@ #include "base-object.h" #include "v8.h" +#include + namespace node { #define NODE_ASYNC_PROVIDER_TYPES(V) \ @@ -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 MakeCallback(const v8::Handle cb, @@ -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