Skip to content

Commit c47d042

Browse files
committed
src: move v8 stats buffers out of Environment
Moves state that is specific to the `v8` binding into the `v8` binding implementation as a cleanup. PR-URL: #32538 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f54b5b2 commit c47d042

File tree

3 files changed

+55
-78
lines changed

3 files changed

+55
-78
lines changed

src/env-inl.h

-33
Original file line numberDiff line numberDiff line change
@@ -580,39 +580,6 @@ inline double Environment::get_default_trigger_async_id() {
580580
return default_trigger_async_id;
581581
}
582582

583-
inline double* Environment::heap_statistics_buffer() const {
584-
CHECK_NOT_NULL(heap_statistics_buffer_);
585-
return static_cast<double*>(heap_statistics_buffer_->Data());
586-
}
587-
588-
inline void Environment::set_heap_statistics_buffer(
589-
std::shared_ptr<v8::BackingStore> backing_store) {
590-
CHECK(!heap_statistics_buffer_); // Should be set only once.
591-
heap_statistics_buffer_ = std::move(backing_store);
592-
}
593-
594-
inline double* Environment::heap_space_statistics_buffer() const {
595-
CHECK(heap_space_statistics_buffer_);
596-
return static_cast<double*>(heap_space_statistics_buffer_->Data());
597-
}
598-
599-
inline void Environment::set_heap_space_statistics_buffer(
600-
std::shared_ptr<v8::BackingStore> backing_store) {
601-
CHECK(!heap_space_statistics_buffer_); // Should be set only once.
602-
heap_space_statistics_buffer_ = std::move(backing_store);
603-
}
604-
605-
inline double* Environment::heap_code_statistics_buffer() const {
606-
CHECK(heap_code_statistics_buffer_);
607-
return static_cast<double*>(heap_code_statistics_buffer_->Data());
608-
}
609-
610-
inline void Environment::set_heap_code_statistics_buffer(
611-
std::shared_ptr<v8::BackingStore> backing_store) {
612-
CHECK(!heap_code_statistics_buffer_); // Should be set only once.
613-
heap_code_statistics_buffer_ = std::move(backing_store);
614-
}
615-
616583
inline char* Environment::http_parser_buffer() const {
617584
return http_parser_buffer_;
618585
}

src/env.h

+1-17
Original file line numberDiff line numberDiff line change
@@ -1007,18 +1007,6 @@ class Environment : public MemoryRetainer {
10071007
inline uint32_t get_next_script_id();
10081008
inline uint32_t get_next_function_id();
10091009

1010-
inline double* heap_statistics_buffer() const;
1011-
inline void set_heap_statistics_buffer(
1012-
std::shared_ptr<v8::BackingStore> backing_store);
1013-
1014-
inline double* heap_space_statistics_buffer() const;
1015-
inline void set_heap_space_statistics_buffer(
1016-
std::shared_ptr<v8::BackingStore> backing_store);
1017-
1018-
inline double* heap_code_statistics_buffer() const;
1019-
inline void set_heap_code_statistics_buffer(
1020-
std::shared_ptr<v8::BackingStore> backing_store);
1021-
10221010
inline char* http_parser_buffer() const;
10231011
inline void set_http_parser_buffer(char* buffer);
10241012
inline bool http_parser_buffer_in_use() const;
@@ -1380,14 +1368,10 @@ class Environment : public MemoryRetainer {
13801368
int handle_cleanup_waiting_ = 0;
13811369
int request_waiting_ = 0;
13821370

1383-
std::shared_ptr<v8::BackingStore> heap_statistics_buffer_;
1384-
std::shared_ptr<v8::BackingStore> heap_space_statistics_buffer_;
1385-
std::shared_ptr<v8::BackingStore> heap_code_statistics_buffer_;
1386-
13871371
char* http_parser_buffer_ = nullptr;
13881372
bool http_parser_buffer_in_use_ = false;
1389-
13901373
EnabledDebugList enabled_debug_list_;
1374+
13911375
AliasedFloat64Array fs_stats_field_array_;
13921376
AliasedBigUint64Array fs_stats_field_bigint_array_;
13931377

src/node_v8.cc

+54-28
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
#include "node.h"
23+
#include "base_object-inl.h"
2324
#include "env-inl.h"
25+
#include "memory_tracker-inl.h"
2426
#include "util-inl.h"
2527
#include "v8.h"
2628

2729
namespace node {
2830

2931
using v8::Array;
3032
using v8::ArrayBuffer;
33+
using v8::BackingStore;
3134
using v8::Context;
3235
using v8::FunctionCallbackInfo;
3336
using v8::HeapCodeStatistics;
@@ -59,7 +62,7 @@ using v8::Value;
5962
V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex)
6063

6164
#define V(a, b, c) +1
62-
static const size_t kHeapStatisticsPropertiesCount =
65+
static constexpr size_t kHeapStatisticsPropertiesCount =
6366
HEAP_STATISTICS_PROPERTIES(V);
6467
#undef V
6568

@@ -71,10 +74,28 @@ static const size_t kHeapStatisticsPropertiesCount =
7174
V(3, physical_space_size, kPhysicalSpaceSizeIndex)
7275

7376
#define V(a, b, c) +1
74-
static const size_t kHeapSpaceStatisticsPropertiesCount =
77+
static constexpr size_t kHeapSpaceStatisticsPropertiesCount =
7578
HEAP_SPACE_STATISTICS_PROPERTIES(V);
7679
#undef V
7780

81+
class BindingData : public BaseObject {
82+
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;
88+
89+
void MemoryInfo(MemoryTracker* tracker) const override {
90+
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
91+
tracker->TrackField("heap_space_statistics_buffer",
92+
heap_space_statistics_buffer);
93+
tracker->TrackField("heap_code_statistics_buffer",
94+
heap_code_statistics_buffer);
95+
}
96+
SET_SELF_SIZE(BindingData)
97+
SET_MEMORY_INFO_NAME(BindingData)
98+
};
7899

79100
#define HEAP_CODE_STATISTICS_PROPERTIES(V) \
80101
V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \
@@ -96,40 +117,44 @@ void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
96117

97118

98119
void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo<Value>& args) {
99-
Environment* env = Environment::GetCurrent(args);
120+
BindingData* data = Unwrap<BindingData>(args.Data());
100121
HeapStatistics s;
101-
env->isolate()->GetHeapStatistics(&s);
102-
double* const buffer = env->heap_statistics_buffer();
122+
args.GetIsolate()->GetHeapStatistics(&s);
123+
double* const buffer =
124+
static_cast<double*>(data->heap_statistics_buffer->Data());
103125
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
104126
HEAP_STATISTICS_PROPERTIES(V)
105127
#undef V
106128
}
107129

108130

109131
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
110-
Environment* env = Environment::GetCurrent(args);
132+
BindingData* data = Unwrap<BindingData>(args.Data());
111133
HeapSpaceStatistics s;
112-
Isolate* const isolate = env->isolate();
113-
double* buffer = env->heap_space_statistics_buffer();
114-
size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();
134+
Isolate* const isolate = args.GetIsolate();
135+
size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces();
136+
137+
double* const buffer =
138+
static_cast<double*>(data->heap_space_statistics_buffer->Data());
115139

116140
for (size_t i = 0; i < number_of_heap_spaces; i++) {
117141
isolate->GetHeapSpaceStatistics(&s, i);
118142
size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount;
119-
#define V(index, name, _) buffer[property_offset + index] = \
120-
static_cast<double>(s.name());
121-
HEAP_SPACE_STATISTICS_PROPERTIES(V)
143+
#define V(index, name, _) \
144+
buffer[property_offset + index] = static_cast<double>(s.name());
145+
HEAP_SPACE_STATISTICS_PROPERTIES(V)
122146
#undef V
123147
}
124148
}
125149

126150

127151
void UpdateHeapCodeStatisticsArrayBuffer(
128152
const FunctionCallbackInfo<Value>& args) {
129-
Environment* env = Environment::GetCurrent(args);
153+
BindingData* data = Unwrap<BindingData>(args.Data());
130154
HeapCodeStatistics s;
131-
env->isolate()->GetHeapCodeAndMetadataStatistics(&s);
132-
double* const buffer = env->heap_code_statistics_buffer();
155+
args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s);
156+
double* const buffer =
157+
static_cast<double*>(data->heap_code_statistics_buffer->Data());
133158
#define V(index, name, _) buffer[index] = static_cast<double>(s.name());
134159
HEAP_CODE_STATISTICS_PROPERTIES(V)
135160
#undef V
@@ -148,6 +173,9 @@ void Initialize(Local<Object> target,
148173
Local<Context> context,
149174
void* priv) {
150175
Environment* env = Environment::GetCurrent(context);
176+
Environment::BindingScope<BindingData> binding_scope(env);
177+
if (!binding_scope) return;
178+
BindingData* binding_data = binding_scope.data;
151179

152180
env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
153181
CachedDataVersionTag);
@@ -158,11 +186,11 @@ void Initialize(Local<Object> target,
158186
UpdateHeapStatisticsArrayBuffer);
159187

160188
const size_t heap_statistics_buffer_byte_length =
161-
sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount;
189+
sizeof(double) * kHeapStatisticsPropertiesCount;
162190

163191
Local<ArrayBuffer> heap_statistics_ab =
164192
ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length);
165-
env->set_heap_statistics_buffer(heap_statistics_ab->GetBackingStore());
193+
binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore();
166194
target->Set(env->context(),
167195
FIXED_ONE_BYTE_STRING(env->isolate(),
168196
"heapStatisticsArrayBuffer"),
@@ -182,19 +210,17 @@ void Initialize(Local<Object> target,
182210
UpdateHeapCodeStatisticsArrayBuffer);
183211

184212
const size_t heap_code_statistics_buffer_byte_length =
185-
sizeof(*env->heap_code_statistics_buffer())
186-
* kHeapCodeStatisticsPropertiesCount;
213+
sizeof(double) * kHeapCodeStatisticsPropertiesCount;
187214

188215
Local<ArrayBuffer> heap_code_statistics_ab =
189216
ArrayBuffer::New(env->isolate(),
190217
heap_code_statistics_buffer_byte_length);
191-
env->set_heap_code_statistics_buffer(
192-
heap_code_statistics_ab->GetBackingStore());
218+
binding_data->heap_code_statistics_buffer =
219+
heap_code_statistics_ab->GetBackingStore();
193220
target->Set(env->context(),
194221
FIXED_ONE_BYTE_STRING(env->isolate(),
195222
"heapCodeStatisticsArrayBuffer"),
196-
heap_code_statistics_ab)
197-
.Check();
223+
heap_code_statistics_ab).Check();
198224

199225
#define V(i, _, name) \
200226
target->Set(env->context(), \
@@ -236,20 +262,20 @@ void Initialize(Local<Object> target,
236262
UpdateHeapSpaceStatisticsBuffer);
237263

238264
const size_t heap_space_statistics_buffer_byte_length =
239-
sizeof(*env->heap_space_statistics_buffer()) *
265+
sizeof(double) *
240266
kHeapSpaceStatisticsPropertiesCount *
241267
number_of_heap_spaces;
242268

243269
Local<ArrayBuffer> heap_space_statistics_ab =
244270
ArrayBuffer::New(env->isolate(),
245271
heap_space_statistics_buffer_byte_length);
246-
env->set_heap_space_statistics_buffer(
247-
heap_space_statistics_ab->GetBackingStore());
272+
binding_data->heap_space_statistics_buffer =
273+
heap_space_statistics_ab->GetBackingStore();
274+
248275
target->Set(env->context(),
249276
FIXED_ONE_BYTE_STRING(env->isolate(),
250277
"heapSpaceStatisticsArrayBuffer"),
251-
heap_space_statistics_ab)
252-
.Check();
278+
heap_space_statistics_ab).Check();
253279

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

0 commit comments

Comments
 (0)