Skip to content

Commit b1971a1

Browse files
committed
src: refactor v8 binding
1. Put the v8 binding data class into a header so we can reuse the class definition during deserialization. 2. Put the v8 binding code into node::v8_utils namespace for clarity. 3. Move the binding data property initialization into its constructor so that we can reuse it during deserialization 4. Reorder the v8 binding initialization so that we don't unnecessarily initialize the properties in a loop
1 parent b54d470 commit b1971a1

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@
749749
'src/node_union_bytes.h',
750750
'src/node_url.h',
751751
'src/node_version.h',
752+
'src/node_v8.h',
752753
'src/node_v8_platform-inl.h',
753754
'src/node_wasi.h',
754755
'src/node_watchdog.h',

src/node_v8.cc

+41-60
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
#include "node.h"
22+
#include "node_v8.h"
2323
#include "base_object-inl.h"
2424
#include "env-inl.h"
2525
#include "memory_tracker-inl.h"
26+
#include "node.h"
2627
#include "util-inl.h"
2728
#include "v8.h"
2829

2930
namespace node {
30-
31+
namespace v8_utils {
3132
using v8::Array;
3233
using v8::Context;
3334
using v8::FunctionCallbackInfo;
35+
using v8::HandleScope;
3436
using v8::HeapCodeStatistics;
3537
using v8::HeapSpaceStatistics;
3638
using v8::HeapStatistics;
@@ -44,7 +46,6 @@ using v8::Uint32;
4446
using v8::V8;
4547
using v8::Value;
4648

47-
4849
#define HEAP_STATISTICS_PROPERTIES(V) \
4950
V(0, total_heap_size, kTotalHeapSizeIndex) \
5051
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \
@@ -63,7 +64,6 @@ static constexpr size_t kHeapStatisticsPropertiesCount =
6364
HEAP_STATISTICS_PROPERTIES(V);
6465
#undef V
6566

66-
6767
#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \
6868
V(0, space_size, kSpaceSizeIndex) \
6969
V(1, space_used_size, kSpaceUsedSizeIndex) \
@@ -85,32 +85,34 @@ static const size_t kHeapCodeStatisticsPropertiesCount =
8585
HEAP_CODE_STATISTICS_PROPERTIES(V);
8686
#undef V
8787

88-
class BindingData : public BaseObject {
89-
public:
90-
BindingData(Environment* env, Local<Object> obj)
91-
: BaseObject(env, obj),
92-
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
93-
heap_space_statistics_buffer(env->isolate(),
94-
kHeapSpaceStatisticsPropertiesCount),
95-
heap_code_statistics_buffer(env->isolate(),
96-
kHeapCodeStatisticsPropertiesCount) {}
97-
98-
static constexpr FastStringKey type_name { "v8" };
99-
100-
AliasedFloat64Array heap_statistics_buffer;
101-
AliasedFloat64Array heap_space_statistics_buffer;
102-
AliasedFloat64Array heap_code_statistics_buffer;
103-
104-
void MemoryInfo(MemoryTracker* tracker) const override {
105-
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
106-
tracker->TrackField("heap_space_statistics_buffer",
107-
heap_space_statistics_buffer);
108-
tracker->TrackField("heap_code_statistics_buffer",
109-
heap_code_statistics_buffer);
110-
}
111-
SET_SELF_SIZE(BindingData)
112-
SET_MEMORY_INFO_NAME(BindingData)
113-
};
88+
BindingData::BindingData(Environment* env, Local<Object> obj)
89+
: BaseObject(env, obj),
90+
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
91+
heap_space_statistics_buffer(env->isolate(),
92+
kHeapSpaceStatisticsPropertiesCount),
93+
heap_code_statistics_buffer(env->isolate(),
94+
kHeapCodeStatisticsPropertiesCount) {
95+
obj->Set(env->context(),
96+
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
97+
heap_statistics_buffer.GetJSArray())
98+
.Check();
99+
obj->Set(env->context(),
100+
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
101+
heap_code_statistics_buffer.GetJSArray())
102+
.Check();
103+
obj->Set(env->context(),
104+
FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsBuffer"),
105+
heap_space_statistics_buffer.GetJSArray())
106+
.Check();
107+
}
108+
109+
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
110+
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
111+
tracker->TrackField("heap_space_statistics_buffer",
112+
heap_space_statistics_buffer);
113+
tracker->TrackField("heap_code_statistics_buffer",
114+
heap_code_statistics_buffer);
115+
}
114116

115117
// TODO(addaleax): Remove once we're on C++17.
116118
constexpr FastStringKey BindingData::type_name;
@@ -179,36 +181,12 @@ void Initialize(Local<Object> target,
179181

180182
env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
181183
CachedDataVersionTag);
182-
183-
// Export symbols used by v8.getHeapStatistics()
184184
env->SetMethod(
185185
target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer);
186186

187-
target
188-
->Set(env->context(),
189-
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
190-
binding_data->heap_statistics_buffer.GetJSArray())
191-
.Check();
192-
193-
#define V(i, _, name) \
194-
target->Set(env->context(), \
195-
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
196-
Uint32::NewFromUnsigned(env->isolate(), i)).Check();
197-
198-
HEAP_STATISTICS_PROPERTIES(V)
199-
200-
// Export symbols used by v8.getHeapCodeStatistics()
201187
env->SetMethod(
202188
target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer);
203189

204-
target
205-
->Set(env->context(),
206-
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
207-
binding_data->heap_code_statistics_buffer.GetJSArray())
208-
.Check();
209-
210-
HEAP_CODE_STATISTICS_PROPERTIES(V)
211-
212190
size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
213191

214192
// Heap space names are extracted once and exposed to JavaScript to
@@ -230,20 +208,23 @@ void Initialize(Local<Object> target,
230208
"updateHeapSpaceStatisticsBuffer",
231209
UpdateHeapSpaceStatisticsBuffer);
232210

233-
target
234-
->Set(env->context(),
235-
FIXED_ONE_BYTE_STRING(env->isolate(),
236-
"heapSpaceStatisticsBuffer"),
237-
binding_data->heap_space_statistics_buffer.GetJSArray())
211+
#define V(i, _, name) \
212+
target \
213+
->Set(env->context(), \
214+
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
215+
Uint32::NewFromUnsigned(env->isolate(), i)) \
238216
.Check();
239217

218+
HEAP_STATISTICS_PROPERTIES(V)
219+
HEAP_CODE_STATISTICS_PROPERTIES(V)
240220
HEAP_SPACE_STATISTICS_PROPERTIES(V)
241221
#undef V
242222

243223
// Export symbols used by v8.setFlagsFromString()
244224
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
245225
}
246226

227+
} // namespace v8_utils
247228
} // namespace node
248229

249-
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)
230+
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize)

src/node_v8.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef SRC_NODE_V8_H_
2+
#define SRC_NODE_V8_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "aliased_buffer.h"
7+
#include "base_object.h"
8+
#include "util.h"
9+
#include "v8.h"
10+
11+
namespace node {
12+
class Environment;
13+
14+
namespace v8_utils {
15+
class BindingData : public BaseObject {
16+
public:
17+
BindingData(Environment* env, v8::Local<v8::Object> obj);
18+
19+
static constexpr FastStringKey type_name{"node::v8::BindingData"};
20+
21+
AliasedFloat64Array heap_statistics_buffer;
22+
AliasedFloat64Array heap_space_statistics_buffer;
23+
AliasedFloat64Array heap_code_statistics_buffer;
24+
25+
void MemoryInfo(MemoryTracker* tracker) const override;
26+
SET_SELF_SIZE(BindingData)
27+
SET_MEMORY_INFO_NAME(BindingData)
28+
};
29+
30+
} // namespace v8_utils
31+
32+
} // namespace node
33+
34+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
35+
36+
#endif // SRC_NODE_V8_H_

0 commit comments

Comments
 (0)