Skip to content

Commit 795d80b

Browse files
committed
src: update v8 to use DictionaryTemplate caching
1 parent 22c57aa commit 795d80b

File tree

2 files changed

+67
-65
lines changed

2 files changed

+67
-65
lines changed

src/env_properties.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,11 @@
468468
V(fdclose_constructor_template, v8::ObjectTemplate) \
469469
V(fdentry_constructor_template, v8::FunctionTemplate) \
470470
V(filehandlereadwrap_template, v8::ObjectTemplate) \
471+
V(free_list_statistics_template, v8::DictionaryTemplate) \
471472
V(fsreqpromise_constructor_template, v8::ObjectTemplate) \
472473
V(handle_wrap_ctor_template, v8::FunctionTemplate) \
473474
V(heap_statistics_template, v8::DictionaryTemplate) \
475+
V(v8_heap_statistics_template, v8::DictionaryTemplate) \
474476
V(histogram_ctor_template, v8::FunctionTemplate) \
475477
V(http2settings_constructor_template, v8::ObjectTemplate) \
476478
V(http2stream_constructor_template, v8::ObjectTemplate) \
@@ -483,13 +485,16 @@
483485
V(message_port_constructor_template, v8::FunctionTemplate) \
484486
V(module_wrap_constructor_template, v8::FunctionTemplate) \
485487
V(microtask_queue_ctor_template, v8::FunctionTemplate) \
488+
V(object_stats_template, v8::DictionaryTemplate) \
489+
V(page_stats_template, v8::DictionaryTemplate) \
486490
V(pipe_constructor_template, v8::FunctionTemplate) \
487491
V(promise_wrap_template, v8::ObjectTemplate) \
488492
V(sab_lifetimepartner_constructor_template, v8::FunctionTemplate) \
489493
V(script_context_constructor_template, v8::FunctionTemplate) \
490494
V(secure_context_constructor_template, v8::FunctionTemplate) \
491495
V(shutdown_wrap_template, v8::ObjectTemplate) \
492496
V(socketaddress_constructor_template, v8::FunctionTemplate) \
497+
V(space_stats_template, v8::DictionaryTemplate) \
493498
V(sqlite_statement_sync_constructor_template, v8::FunctionTemplate) \
494499
V(sqlite_statement_sync_iterator_constructor_template, v8::FunctionTemplate) \
495500
V(sqlite_session_constructor_template, v8::FunctionTemplate) \

src/node_v8.cc

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using v8::Array;
3535
using v8::BigInt;
3636
using v8::CFunction;
3737
using v8::Context;
38+
using v8::DictionaryTemplate;
3839
using v8::FunctionCallbackInfo;
3940
using v8::FunctionTemplate;
4041
using v8::HandleScope;
@@ -326,9 +327,57 @@ static void SetHeapStatistics(JSONWriter* writer, Isolate* isolate) {
326327
static MaybeLocal<Object> ConvertHeapStatsToJSObject(
327328
Isolate* isolate, const cppgc::HeapStatistics& stats) {
328329
Local<Context> context = isolate->GetCurrentContext();
330+
Environment* env = Environment::GetCurrent(isolate);
329331
// Space Statistics
330332
LocalVector<Value> space_statistics_array(isolate);
331333
space_statistics_array.reserve(stats.space_stats.size());
334+
335+
auto object_stats_template = env->object_stats_template();
336+
auto page_stats_tmpl = env->page_stats_template();
337+
auto free_list_statistics_template = env->free_list_statistics_template();
338+
auto space_stats_tmpl = env->space_stats_template();
339+
auto heap_stats_tmpl = env->v8_heap_statistics_template();
340+
if (object_stats_template.IsEmpty()) {
341+
std::string_view object_stats_names[] = {"allocated_bytes", "object_count"};
342+
object_stats_template =
343+
DictionaryTemplate::New(isolate, object_stats_names);
344+
env->set_object_stats_template(object_stats_template);
345+
}
346+
if (page_stats_tmpl.IsEmpty()) {
347+
std::string_view page_stats_names[] = {"committed_size_bytes",
348+
"resident_size_bytes",
349+
"used_size_bytes",
350+
"object_statistics"};
351+
page_stats_tmpl = DictionaryTemplate::New(isolate, page_stats_names);
352+
env->set_page_stats_template(page_stats_tmpl);
353+
}
354+
if (free_list_statistics_template.IsEmpty()) {
355+
std::string_view free_list_statistics_names[] = {
356+
"bucket_size", "free_count", "free_size"};
357+
free_list_statistics_template =
358+
DictionaryTemplate::New(isolate, free_list_statistics_names);
359+
env->set_free_list_statistics_template(free_list_statistics_template);
360+
}
361+
if (space_stats_tmpl.IsEmpty()) {
362+
std::string_view space_stats_names[] = {"name",
363+
"committed_size_bytes",
364+
"resident_size_bytes",
365+
"used_size_bytes",
366+
"page_stats",
367+
"free_list_stats"};
368+
space_stats_tmpl = DictionaryTemplate::New(isolate, space_stats_names);
369+
env->set_space_stats_template(space_stats_tmpl);
370+
}
371+
if (heap_stats_tmpl.IsEmpty()) {
372+
std::string_view heap_statistics_names[] = {"committed_size_bytes",
373+
"resident_size_bytes",
374+
"used_size_bytes",
375+
"space_statistics",
376+
"type_names"};
377+
heap_stats_tmpl = DictionaryTemplate::New(isolate, heap_statistics_names);
378+
env->set_v8_heap_statistics_template(heap_stats_tmpl);
379+
}
380+
332381
for (size_t i = 0; i < stats.space_stats.size(); i++) {
333382
const cppgc::HeapStatistics::SpaceStatistics& space_stats =
334383
stats.space_stats[i];
@@ -344,30 +393,18 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
344393
for (size_t k = 0; k < page_stats.object_statistics.size(); k++) {
345394
const cppgc::HeapStatistics::ObjectStatsEntry& object_stats =
346395
page_stats.object_statistics[k];
347-
Local<Name> object_stats_names[] = {
348-
FIXED_ONE_BYTE_STRING(isolate, "allocated_bytes"),
349-
FIXED_ONE_BYTE_STRING(isolate, "object_count")};
350-
Local<Value> object_stats_values[] = {
396+
MaybeLocal<Value> object_stats_values[] = {
351397
Uint32::NewFromUnsigned(
352398
isolate, static_cast<uint32_t>(object_stats.allocated_bytes)),
353399
Uint32::NewFromUnsigned(
354400
isolate, static_cast<uint32_t>(object_stats.object_count))};
355401
Local<Object> object_stats_object =
356-
Object::New(isolate,
357-
Null(isolate),
358-
object_stats_names,
359-
object_stats_values,
360-
arraysize(object_stats_names));
402+
object_stats_template->NewInstance(context, object_stats_values);
361403
object_statistics_array.emplace_back(object_stats_object);
362404
}
363405

364406
// Set page statistics
365-
Local<Name> page_stats_names[] = {
366-
FIXED_ONE_BYTE_STRING(isolate, "committed_size_bytes"),
367-
FIXED_ONE_BYTE_STRING(isolate, "resident_size_bytes"),
368-
FIXED_ONE_BYTE_STRING(isolate, "used_size_bytes"),
369-
FIXED_ONE_BYTE_STRING(isolate, "object_statistics")};
370-
Local<Value> page_stats_values[] = {
407+
MaybeLocal<Value> page_stats_values[] = {
371408
Uint32::NewFromUnsigned(
372409
isolate, static_cast<uint32_t>(page_stats.committed_size_bytes)),
373410
Uint32::NewFromUnsigned(
@@ -377,21 +414,12 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
377414
Array::New(isolate,
378415
object_statistics_array.data(),
379416
object_statistics_array.size())};
380-
Local<Object> page_stats_object =
381-
Object::New(isolate,
382-
Null(isolate),
383-
page_stats_names,
384-
page_stats_values,
385-
arraysize(page_stats_names));
386-
page_statistics_array.emplace_back(page_stats_object);
417+
page_statistics_array.emplace_back(
418+
page_stats_tmpl->NewInstance(context, page_stats_values));
387419
}
388420

389421
// Free List Statistics
390-
Local<Name> free_list_statistics_names[] = {
391-
FIXED_ONE_BYTE_STRING(isolate, "bucket_size"),
392-
FIXED_ONE_BYTE_STRING(isolate, "free_count"),
393-
FIXED_ONE_BYTE_STRING(isolate, "free_size")};
394-
Local<Value> free_list_statistics_values[] = {
422+
MaybeLocal<Value> free_list_statistics_values[] = {
395423
ToV8ValuePrimitiveArray(
396424
context, space_stats.free_list_stats.bucket_size, isolate),
397425
ToV8ValuePrimitiveArray(
@@ -400,27 +428,16 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
400428
context, space_stats.free_list_stats.free_size, isolate)};
401429

402430
Local<Object> free_list_statistics_obj =
403-
Object::New(isolate,
404-
Null(isolate),
405-
free_list_statistics_names,
406-
free_list_statistics_values,
407-
arraysize(free_list_statistics_names));
431+
free_list_statistics_template->NewInstance(context,
432+
free_list_statistics_values);
408433

409434
// Set Space Statistics
410-
Local<Name> space_stats_names[] = {
411-
FIXED_ONE_BYTE_STRING(isolate, "name"),
412-
FIXED_ONE_BYTE_STRING(isolate, "committed_size_bytes"),
413-
FIXED_ONE_BYTE_STRING(isolate, "resident_size_bytes"),
414-
FIXED_ONE_BYTE_STRING(isolate, "used_size_bytes"),
415-
FIXED_ONE_BYTE_STRING(isolate, "page_stats"),
416-
FIXED_ONE_BYTE_STRING(isolate, "free_list_stats")};
417-
418435
Local<Value> name_value;
419436
if (!ToV8Value(context, stats.space_stats[i].name, isolate)
420437
.ToLocal(&name_value)) {
421438
return MaybeLocal<Object>();
422439
}
423-
Local<Value> space_stats_values[] = {
440+
MaybeLocal<Value> space_stats_values[] = {
424441
name_value,
425442
Uint32::NewFromUnsigned(
426443
isolate,
@@ -436,29 +453,16 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
436453
page_statistics_array.size()),
437454
free_list_statistics_obj,
438455
};
439-
Local<Object> space_stats_object =
440-
Object::New(isolate,
441-
Null(isolate),
442-
space_stats_names,
443-
space_stats_values,
444-
arraysize(space_stats_names));
445-
space_statistics_array.emplace_back(space_stats_object);
456+
space_statistics_array.emplace_back(
457+
space_stats_tmpl->NewInstance(context, space_stats_values));
446458
}
447459

448-
// Set heap statistics
449-
Local<Name> heap_statistics_names[] = {
450-
FIXED_ONE_BYTE_STRING(isolate, "committed_size_bytes"),
451-
FIXED_ONE_BYTE_STRING(isolate, "resident_size_bytes"),
452-
FIXED_ONE_BYTE_STRING(isolate, "used_size_bytes"),
453-
FIXED_ONE_BYTE_STRING(isolate, "space_statistics"),
454-
FIXED_ONE_BYTE_STRING(isolate, "type_names")};
455-
456460
Local<Value> type_names_value;
457461
if (!ToV8Value(context, stats.type_names, isolate)
458462
.ToLocal(&type_names_value)) {
459463
return MaybeLocal<Object>();
460464
}
461-
Local<Value> heap_statistics_values[] = {
465+
MaybeLocal<Value> heap_statistics_values[] = {
462466
Uint32::NewFromUnsigned(
463467
isolate, static_cast<uint32_t>(stats.committed_size_bytes)),
464468
Uint32::NewFromUnsigned(isolate,
@@ -470,14 +474,7 @@ static MaybeLocal<Object> ConvertHeapStatsToJSObject(
470474
space_statistics_array.size()),
471475
type_names_value};
472476

473-
Local<Object> heap_statistics_object =
474-
Object::New(isolate,
475-
Null(isolate),
476-
heap_statistics_names,
477-
heap_statistics_values,
478-
arraysize(heap_statistics_names));
479-
480-
return heap_statistics_object;
477+
return heap_stats_tmpl->NewInstance(context, heap_statistics_values);
481478
}
482479

483480
static void GetCppHeapStatistics(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)