Skip to content

Commit 4bb3184

Browse files
committed
src: reduce AsyncWrap memory footprint
Fold two integral fields into one and use bitops to access/manipulate them. PR-URL: #667 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 7e779b4 commit 4bb3184

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/async-wrap-inl.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
1717
v8::Handle<v8::Object> object,
1818
ProviderType provider,
1919
AsyncWrap* parent)
20-
: BaseObject(env, object),
21-
has_async_queue_(false),
22-
provider_type_(provider) {
20+
: BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1) {
2321
// Check user controlled flag to see if the init callback should run.
2422
if (!env->using_asyncwrap())
2523
return;
@@ -49,7 +47,7 @@ inline AsyncWrap::AsyncWrap(Environment* env,
4947
if (try_catch.HasCaught())
5048
FatalError("node::AsyncWrap::AsyncWrap", "init hook threw");
5149

52-
has_async_queue_ = true;
50+
bits_ |= 1; // has_async_queue() is true now.
5351

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

6159

62-
inline uint32_t AsyncWrap::provider_type() const {
63-
return provider_type_;
60+
inline bool AsyncWrap::has_async_queue() const {
61+
return static_cast<bool>(bits_ & 1);
62+
}
63+
64+
65+
inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
66+
return static_cast<ProviderType>(bits_ >> 1);
6467
}
6568

6669

src/async-wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Handle<Value> AsyncWrap::MakeCallback(const Handle<Function> cb,
104104
}
105105
}
106106

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

121-
if (has_async_queue_) {
121+
if (has_async_queue()) {
122122
try_catch.SetVerbose(false);
123123
env()->async_hooks_post_function()->Call(context, 0, nullptr);
124124
if (try_catch.HasCaught())

src/async-wrap.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "base-object.h"
55
#include "v8.h"
66

7+
#include <stdint.h>
8+
79
namespace node {
810

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

4951
inline virtual ~AsyncWrap() override = default;
5052

51-
inline uint32_t provider_type() const;
53+
inline ProviderType provider_type() const;
5254

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

6466
private:
6567
inline AsyncWrap();
68+
inline bool has_async_queue() const;
6669

6770
// When the async hooks init JS function is called from the constructor it is
6871
// expected the context object will receive a _asyncQueue object property
6972
// that will be used to call pre/post in MakeCallback.
70-
bool has_async_queue_;
71-
ProviderType provider_type_;
73+
uint32_t bits_;
7274
};
7375

7476
} // namespace node

0 commit comments

Comments
 (0)