Skip to content

Commit aa9708e

Browse files
joyeecheungaddaleax
authored andcommitted
v8: use AliasedBuffers for passing heap statistics around
Instead of holding shared pointers to ArrayBuffers, simplify the code by using AliasedBuffers directly which allows the binding to own the buffers. PR-URL: #32929 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent d0377a8 commit aa9708e

File tree

2 files changed

+66
-111
lines changed

2 files changed

+66
-111
lines changed

lib/v8.js

+13-24
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ class Deserializer extends _Deserializer { }
7373
const {
7474
cachedDataVersionTag,
7575
setFlagsFromString: _setFlagsFromString,
76-
heapStatisticsArrayBuffer,
77-
heapSpaceStatisticsArrayBuffer,
78-
heapCodeStatisticsArrayBuffer,
79-
updateHeapStatisticsArrayBuffer,
80-
updateHeapSpaceStatisticsArrayBuffer,
81-
updateHeapCodeStatisticsArrayBuffer,
76+
heapStatisticsBuffer,
77+
heapSpaceStatisticsBuffer,
78+
heapCodeStatisticsBuffer,
79+
updateHeapStatisticsBuffer,
80+
updateHeapSpaceStatisticsBuffer,
81+
updateHeapCodeStatisticsBuffer,
8282

8383
// Properties for heap statistics buffer extraction.
8484
kTotalHeapSizeIndex,
@@ -95,7 +95,6 @@ const {
9595

9696
// Properties for heap spaces statistics buffer extraction.
9797
kHeapSpaces,
98-
kHeapSpaceStatisticsPropertiesCount,
9998
kSpaceSizeIndex,
10099
kSpaceUsedSizeIndex,
101100
kSpaceAvailableSizeIndex,
@@ -109,15 +108,6 @@ const {
109108

110109
const kNumberOfHeapSpaces = kHeapSpaces.length;
111110

112-
const heapStatisticsBuffer =
113-
new Float64Array(heapStatisticsArrayBuffer);
114-
115-
const heapSpaceStatisticsBuffer =
116-
new Float64Array(heapSpaceStatisticsArrayBuffer);
117-
118-
const heapCodeStatisticsBuffer =
119-
new Float64Array(heapCodeStatisticsArrayBuffer);
120-
121111
function setFlagsFromString(flags) {
122112
validateString(flags, 'flags');
123113
_setFlagsFromString(flags);
@@ -126,7 +116,7 @@ function setFlagsFromString(flags) {
126116
function getHeapStatistics() {
127117
const buffer = heapStatisticsBuffer;
128118

129-
updateHeapStatisticsArrayBuffer();
119+
updateHeapStatisticsBuffer();
130120

131121
return {
132122
'total_heap_size': buffer[kTotalHeapSizeIndex],
@@ -146,16 +136,15 @@ function getHeapStatistics() {
146136
function getHeapSpaceStatistics() {
147137
const heapSpaceStatistics = new Array(kNumberOfHeapSpaces);
148138
const buffer = heapSpaceStatisticsBuffer;
149-
updateHeapSpaceStatisticsArrayBuffer();
150139

151140
for (let i = 0; i < kNumberOfHeapSpaces; i++) {
152-
const propertyOffset = i * kHeapSpaceStatisticsPropertiesCount;
141+
updateHeapSpaceStatisticsBuffer(i);
153142
heapSpaceStatistics[i] = {
154143
space_name: kHeapSpaces[i],
155-
space_size: buffer[propertyOffset + kSpaceSizeIndex],
156-
space_used_size: buffer[propertyOffset + kSpaceUsedSizeIndex],
157-
space_available_size: buffer[propertyOffset + kSpaceAvailableSizeIndex],
158-
physical_space_size: buffer[propertyOffset + kPhysicalSpaceSizeIndex]
144+
space_size: buffer[kSpaceSizeIndex],
145+
space_used_size: buffer[kSpaceUsedSizeIndex],
146+
space_available_size: buffer[kSpaceAvailableSizeIndex],
147+
physical_space_size: buffer[kPhysicalSpaceSizeIndex]
159148
};
160149
}
161150

@@ -165,7 +154,7 @@ function getHeapSpaceStatistics() {
165154
function getHeapCodeStatistics() {
166155
const buffer = heapCodeStatisticsBuffer;
167156

168-
updateHeapCodeStatisticsArrayBuffer();
157+
updateHeapCodeStatisticsBuffer();
169158
return {
170159
'code_and_metadata_size': buffer[kCodeAndMetadataSizeIndex],
171160
'bytecode_and_metadata_size': buffer[kBytecodeAndMetadataSizeIndex],

src/node_v8.cc

+53-87
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
namespace node {
3030

3131
using v8::Array;
32-
using v8::ArrayBuffer;
33-
using v8::BackingStore;
3432
using v8::Context;
3533
using v8::FunctionCallbackInfo;
3634
using v8::HeapCodeStatistics;
@@ -78,13 +76,29 @@ static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
7876
HEAP_SPACE_STATISTICS_PROPERTIES(V);
7977
#undef V
8078

79+
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
80+
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
81+
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \
82+
V(2, external_script_source_size, kExternalScriptSourceSizeIndex)
83+
84+
#define V(a, b, c) +1
85+
static const size_t kHeapCodeStatisticsPropertiesCount =
86+
HEAP_CODE_STATISTICS_PROPERTIES(V);
87+
#undef V
88+
8189
class BindingData : public BaseObject {
8290
public:
83-
BindingData(Environment* env, Local<Object> obj) : BaseObject(env, obj) {}
84-
85-
std::shared_ptr<BackingStore> heap_statistics_buffer;
86-
std::shared_ptr<BackingStore> heap_space_statistics_buffer;
87-
std::shared_ptr<BackingStore> heap_code_statistics_buffer;
91+
BindingData(Environment* env, Local<Object> obj)
92+
: BaseObject(env, obj),
93+
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
94+
heap_space_statistics_buffer(env->isolate(),
95+
kHeapSpaceStatisticsPropertiesCount),
96+
heap_code_statistics_buffer(env->isolate(),
97+
kHeapCodeStatisticsPropertiesCount) {}
98+
99+
AliasedFloat64Array heap_statistics_buffer;
100+
AliasedFloat64Array heap_space_statistics_buffer;
101+
AliasedFloat64Array heap_code_statistics_buffer;
88102

89103
void MemoryInfo(MemoryTracker* tracker) const override {
90104
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
@@ -97,15 +111,6 @@ class BindingData : public BaseObject {
97111
SET_MEMORY_INFO_NAME(BindingData)
98112
};
99113

100-
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
101-
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
102-
V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \
103-
V(2, external_script_source_size, kExternalScriptSourceSizeIndex)
104-
105-
#define V(a, b, c) +1
106-
static const size_t kHeapCodeStatisticsPropertiesCount =
107-
HEAP_CODE_STATISTICS_PROPERTIES(V);
108-
#undef V
109114

110115
void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
111116
Environment* env = Environment::GetCurrent(args);
@@ -115,13 +120,11 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
115120
args.GetReturnValue().Set(result);
116121
}
117122

118-
119-
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
123+
void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
120124
BindingData* data = Unwrap<BindingData>(args.Data());
121125
HeapStatistics s;
122126
args.GetIsolate()->GetHeapStatistics(&s);
123-
double* const buffer =
124-
static_cast<double*>(data->heap_statistics_buffer->Data());
127+
AliasedFloat64Array& buffer = data->heap_statistics_buffer;
125128
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
126129
HEAP_STATISTICS_PROPERTIES(V)
127130
#undef V
@@ -132,29 +135,23 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
132135
BindingData* data = Unwrap<BindingData>(args.Data());
133136
HeapSpaceStatistics s;
134137
Isolate* const isolate = args.GetIsolate();
135-
size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces();
138+
CHECK(args[0]->IsUint32());
139+
size_t space_index = static_cast<size_t>(args[0].As<v8::Uint32>()->Value());
140+
isolate->GetHeapSpaceStatistics(&s, space_index);
136141

137-
double* const buffer =
138-
static_cast<double*>(data->heap_space_statistics_buffer->Data());
142+
AliasedFloat64Array& buffer = data->heap_space_statistics_buffer;
139143

140-
for (size_t i = 0; i < number_of_heap_spaces; i++) {
141-
isolate->GetHeapSpaceStatistics(&s, i);
142-
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
143-
#define V(index, name, _) \
144-
buffer[property_offset + index] = static_cast<double>(s.name());
145-
HEAP_SPACE_STATISTICS_PROPERTIES(V)
144+
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
145+
HEAP_SPACE_STATISTICS_PROPERTIES(V)
146146
#undef V
147-
}
148147
}
149148

150-
151-
void UpdateHeapCodeStatisticsArrayBuffer(
152-
const FunctionCallbackInfo<Value>& args) {
149+
void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
153150
BindingData* data = Unwrap<BindingData>(args.Data());
154151
HeapCodeStatistics s;
155152
args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
156-
double* const buffer =
157-
static_cast<double*>(data->heap_code_statistics_buffer->Data());
153+
AliasedFloat64Array& buffer = data->heap_code_statistics_buffer;
154+
158155
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
159156
HEAP_CODE_STATISTICS_PROPERTIES(V)
160157
#undef V
@@ -181,20 +178,14 @@ void Initialize(Local<Object> target,
181178
CachedDataVersionTag);
182179

183180
// Export symbols used by v8.getHeapStatistics()
184-
env->SetMethod(target,
185-
"updateHeapStatisticsArrayBuffer",
186-
UpdateHeapStatisticsArrayBuffer);
181+
env->SetMethod(
182+
target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer);
187183

188-
const size_t heap_statistics_buffer_byte_length =
189-
sizeof(double) * kHeapStatisticsPropertiesCount;
190-
191-
Local<ArrayBuffer> heap_statistics_ab =
192-
ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length);
193-
binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore();
194-
target->Set(env->context(),
195-
FIXED_ONE_BYTE_STRING(env->isolate(),
196-
"heapStatisticsArrayBuffer"),
197-
heap_statistics_ab).Check();
184+
target
185+
->Set(env->context(),
186+
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
187+
binding_data->heap_statistics_buffer.GetJSArray())
188+
.Check();
198189

199190
#define V(i, _, name) \
200191
target->Set(env->context(), \
@@ -205,22 +196,14 @@ void Initialize(Local<Object> target,
205196
#undef V
206197

207198
// Export symbols used by v8.getHeapCodeStatistics()
208-
env->SetMethod(target,
209-
"updateHeapCodeStatisticsArrayBuffer",
210-
UpdateHeapCodeStatisticsArrayBuffer);
211-
212-
const size_t heap_code_statistics_buffer_byte_length =
213-
sizeof(double) * kHeapCodeStatisticsPropertiesCount;
199+
env->SetMethod(
200+
target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer);
214201

215-
Local<ArrayBuffer> heap_code_statistics_ab =
216-
ArrayBuffer::New(env->isolate(),
217-
heap_code_statistics_buffer_byte_length);
218-
binding_data->heap_code_statistics_buffer =
219-
heap_code_statistics_ab->GetBackingStore();
220-
target->Set(env->context(),
221-
FIXED_ONE_BYTE_STRING(env->isolate(),
222-
"heapCodeStatisticsArrayBuffer"),
223-
heap_code_statistics_ab).Check();
202+
target
203+
->Set(env->context(),
204+
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
205+
binding_data->heap_code_statistics_buffer.GetJSArray())
206+
.Check();
224207

225208
#define V(i, _, name) \
226209
target->Set(env->context(), \
@@ -230,14 +213,6 @@ void Initialize(Local<Object> target,
230213
HEAP_CODE_STATISTICS_PROPERTIES(V)
231214
#undef V
232215

233-
// Export symbols used by v8.getHeapSpaceStatistics()
234-
target->Set(env->context(),
235-
FIXED_ONE_BYTE_STRING(env->isolate(),
236-
"kHeapSpaceStatisticsPropertiesCount"),
237-
Uint32::NewFromUnsigned(env->isolate(),
238-
kHeapSpaceStatisticsPropertiesCount))
239-
.Check();
240-
241216
size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
242217

243218
// Heap space names are extracted once and exposed to JavaScript to
@@ -258,24 +233,15 @@ void Initialize(Local<Object> target,
258233
number_of_heap_spaces)).Check();
259234

260235
env->SetMethod(target,
261-
"updateHeapSpaceStatisticsArrayBuffer",
236+
"updateHeapSpaceStatisticsBuffer",
262237
UpdateHeapSpaceStatisticsBuffer);
263238

264-
const size_t heap_space_statistics_buffer_byte_length =
265-
sizeof(double) *
266-
kHeapSpaceStatisticsPropertiesCount *
267-
number_of_heap_spaces;
268-
269-
Local<ArrayBuffer> heap_space_statistics_ab =
270-
ArrayBuffer::New(env->isolate(),
271-
heap_space_statistics_buffer_byte_length);
272-
binding_data->heap_space_statistics_buffer =
273-
heap_space_statistics_ab->GetBackingStore();
274-
275-
target->Set(env->context(),
276-
FIXED_ONE_BYTE_STRING(env->isolate(),
277-
"heapSpaceStatisticsArrayBuffer"),
278-
heap_space_statistics_ab).Check();
239+
target
240+
->Set(env->context(),
241+
FIXED_ONE_BYTE_STRING(env->isolate(),
242+
"heapSpaceStatisticsBuffer"),
243+
binding_data->heap_space_statistics_buffer.GetJSArray())
244+
.Check();
279245

280246
#define V(i, _, name) \
281247
target->Set(env->context(), \

0 commit comments

Comments
 (0)