Skip to content

Commit 8432d65

Browse files
bnoordhuisdanielleadams
authored andcommitted
src: slim down env-inl.h
Move big and/or infrequently used functions from env-inl.h to env.cc to speed up build times and reduce binary bloat. This commit also touches async_wrap-inl.h and base_object-inl.h because those are closely interwined with env-inl.h. Non-functional change. Refs: #43712 PR-URL: #43745 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 364deea commit 8432d65

File tree

6 files changed

+548
-559
lines changed

6 files changed

+548
-559
lines changed

src/async_wrap-inl.h

-7
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
8080
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
8181
}
8282

83-
84-
// Defined here to avoid a circular dependency with env-inl.h.
85-
inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope(
86-
AsyncWrap* async_wrap)
87-
: DefaultTriggerAsyncIdScope(async_wrap->env(),
88-
async_wrap->get_async_id()) {}
89-
9083
} // namespace node
9184

9285
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/base_object-inl.h

-104
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,6 @@
3232

3333
namespace node {
3434

35-
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
36-
: persistent_handle_(env->isolate(), object), env_(env) {
37-
CHECK_EQ(false, object.IsEmpty());
38-
CHECK_GT(object->InternalFieldCount(), 0);
39-
object->SetAlignedPointerInInternalField(
40-
BaseObject::kSlot,
41-
static_cast<void*>(this));
42-
env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
43-
env->modify_base_object_count(1);
44-
}
45-
46-
BaseObject::~BaseObject() {
47-
env()->modify_base_object_count(-1);
48-
env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
49-
50-
if (UNLIKELY(has_pointer_data())) {
51-
PointerData* metadata = pointer_data();
52-
CHECK_EQ(metadata->strong_ptr_count, 0);
53-
metadata->self = nullptr;
54-
if (metadata->weak_ptr_count == 0)
55-
delete metadata;
56-
}
57-
58-
if (persistent_handle_.IsEmpty()) {
59-
// This most likely happened because the weak callback below cleared it.
60-
return;
61-
}
62-
63-
{
64-
v8::HandleScope handle_scope(env()->isolate());
65-
object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
66-
}
67-
}
68-
6935
void BaseObject::Detach() {
7036
CHECK_GT(pointer_data()->strong_ptr_count, 0);
7137
pointer_data()->is_detached = true;
@@ -107,28 +73,6 @@ T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
10773
return static_cast<T*>(FromJSObject(object));
10874
}
10975

110-
111-
void BaseObject::MakeWeak() {
112-
if (has_pointer_data()) {
113-
pointer_data()->wants_weak_jsobj = true;
114-
if (pointer_data()->strong_ptr_count > 0) return;
115-
}
116-
117-
persistent_handle_.SetWeak(
118-
this,
119-
[](const v8::WeakCallbackInfo<BaseObject>& data) {
120-
BaseObject* obj = data.GetParameter();
121-
// Clear the persistent handle so that ~BaseObject() doesn't attempt
122-
// to mess with internal fields, since the JS object may have
123-
// transitioned into an invalid state.
124-
// Refs: https://github.com/nodejs/node/issues/18897
125-
obj->persistent_handle_.Reset();
126-
CHECK_IMPLIES(obj->has_pointer_data(),
127-
obj->pointer_data()->strong_ptr_count == 0);
128-
obj->OnGCCollect();
129-
}, v8::WeakCallbackType::kParameter);
130-
}
131-
13276
void BaseObject::OnGCCollect() {
13377
delete this;
13478
}
@@ -148,23 +92,6 @@ bool BaseObject::IsWeakOrDetached() const {
14892
return pd->wants_weak_jsobj || pd->is_detached;
14993
}
15094

151-
void BaseObject::LazilyInitializedJSTemplateConstructor(
152-
const v8::FunctionCallbackInfo<v8::Value>& args) {
153-
DCHECK(args.IsConstructCall());
154-
DCHECK_GT(args.This()->InternalFieldCount(), 0);
155-
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156-
}
157-
158-
v8::Local<v8::FunctionTemplate>
159-
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
160-
v8::Local<v8::FunctionTemplate> t =
161-
env->NewFunctionTemplate(LazilyInitializedJSTemplateConstructor);
162-
t->Inherit(BaseObject::GetConstructorTemplate(env));
163-
t->InstanceTemplate()->SetInternalFieldCount(
164-
BaseObject::kInternalFieldCount);
165-
return t;
166-
}
167-
16895
template <int Field>
16996
void BaseObject::InternalFieldGet(
17097
v8::Local<v8::String> property,
@@ -185,37 +112,6 @@ bool BaseObject::has_pointer_data() const {
185112
return pointer_data_ != nullptr;
186113
}
187114

188-
BaseObject::PointerData* BaseObject::pointer_data() {
189-
if (!has_pointer_data()) {
190-
PointerData* metadata = new PointerData();
191-
metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
192-
metadata->self = this;
193-
pointer_data_ = metadata;
194-
}
195-
CHECK(has_pointer_data());
196-
return pointer_data_;
197-
}
198-
199-
void BaseObject::decrease_refcount() {
200-
CHECK(has_pointer_data());
201-
PointerData* metadata = pointer_data();
202-
CHECK_GT(metadata->strong_ptr_count, 0);
203-
unsigned int new_refcount = --metadata->strong_ptr_count;
204-
if (new_refcount == 0) {
205-
if (metadata->is_detached) {
206-
OnGCCollect();
207-
} else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
208-
MakeWeak();
209-
}
210-
}
211-
}
212-
213-
void BaseObject::increase_refcount() {
214-
unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
215-
if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
216-
persistent_handle_.ClearWeak();
217-
}
218-
219115
template <typename T, bool kIsWeak>
220116
BaseObject::PointerData*
221117
BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {

src/base_object.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class BaseObject : public MemoryRetainer {
4444

4545
// Associates this object with `object`. It uses the 0th internal field for
4646
// that, and in particular aborts if there is no such field.
47-
inline BaseObject(Environment* env, v8::Local<v8::Object> object);
48-
inline ~BaseObject() override;
47+
BaseObject(Environment* env, v8::Local<v8::Object> object);
48+
~BaseObject() override;
4949

5050
BaseObject() = delete;
5151

@@ -65,7 +65,7 @@ class BaseObject : public MemoryRetainer {
6565
// was also passed to the `BaseObject()` constructor initially.
6666
// This may return `nullptr` if the C++ object has not been constructed yet,
6767
// e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
68-
static inline void LazilyInitializedJSTemplateConstructor(
68+
static void LazilyInitializedJSTemplateConstructor(
6969
const v8::FunctionCallbackInfo<v8::Value>& args);
7070
static inline BaseObject* FromJSObject(v8::Local<v8::Value> object);
7171
template <typename T>
@@ -74,7 +74,7 @@ class BaseObject : public MemoryRetainer {
7474
// Make the `v8::Global` a weak reference and, `delete` this object once
7575
// the JS object has been garbage collected and there are no (strong)
7676
// BaseObjectPtr references to it.
77-
inline void MakeWeak();
77+
void MakeWeak();
7878

7979
// Undo `MakeWeak()`, i.e. turn this into a strong reference that is a GC
8080
// root and will not be touched by the garbage collector.
@@ -88,7 +88,7 @@ class BaseObject : public MemoryRetainer {
8888
// Utility to create a FunctionTemplate with one internal field (used for
8989
// the `BaseObject*` pointer) and a constructor that initializes that field
9090
// to `nullptr`.
91-
static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
91+
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
9292
Environment* env);
9393

9494
// Setter/Getter pair for internal fields that can be passed to SetAccessor.
@@ -202,11 +202,11 @@ class BaseObject : public MemoryRetainer {
202202
inline bool has_pointer_data() const;
203203
// This creates a PointerData struct if none was associated with this
204204
// BaseObject before.
205-
inline PointerData* pointer_data();
205+
PointerData* pointer_data();
206206

207207
// Functions that adjust the strong pointer count.
208-
inline void decrease_refcount();
209-
inline void increase_refcount();
208+
void decrease_refcount();
209+
void increase_refcount();
210210

211211
Environment* env_;
212212
PointerData* pointer_data_ = nullptr;

0 commit comments

Comments
 (0)