From 30b5adf28bc60622bc4f6c180fcac5e9586e70c4 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Fri, 29 Sep 2023 23:35:20 +0300 Subject: [PATCH] feat: avoid std::string --- src/native_ext/metrics.cpp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/native_ext/metrics.cpp b/src/native_ext/metrics.cpp index 8459799f..d2b9d63f 100644 --- a/src/native_ext/metrics.cpp +++ b/src/native_ext/metrics.cpp @@ -44,9 +44,18 @@ int32_t GetGcStatsIndex(v8::GCType type) { return -1; } } + +enum GcType { + GcTypeAll, + GcTypeScavenge, + GcTypeMarkSweepCompact, + GcTypeIncrementalMarking, + GcTypeProcessWeakCallbacks, +}; + struct GcCounters { - GcCounters(const std::string& type) : type(type) {} - std::string type; + GcCounters(GcType type) : type(type) {} + GcType type; Counters time; Counters amount; }; @@ -72,11 +81,11 @@ const size_t kGcTypes = 5; struct { Counters eventLoop; GcCounters gcCounters[kGcTypes] = { - {"all"}, - {"scavenge"}, - {"mark_sweep_compact"}, - {"incremental_marking"}, - {"process_weak_callbacks"}}; + {GcTypeAll}, + {GcTypeScavenge}, + {GcTypeMarkSweepCompact}, + {GcTypeIncrementalMarking}, + {GcTypeProcessWeakCallbacks}}; } stats; void EventLoopPrepareCallback(uv_prepare_t* handle) { @@ -102,7 +111,7 @@ void EventLoopCheckCallback(uv_check_t* handle) { #endif } void WriteCounters( - v8::Local& parent, const std::string& key, const Counters& counters) { + v8::Local& parent, const char* key, const Counters& counters) { auto obj = Nan::New(); Nan::Set(obj, Nan::New("min").ToLocalChecked(), Nan::New(double(counters.min))); Nan::Set(obj, Nan::New("max").ToLocalChecked(), Nan::New(double(counters.max))); @@ -113,6 +122,16 @@ void WriteCounters( Nan::Set(parent, Nan::New(key).ToLocalChecked(), obj); } +Nan::MaybeLocal GcTypeString(GcType type) { + switch (type) { + case GcTypeScavenge: return Nan::New("scavenge"); + case GcTypeMarkSweepCompact: return Nan::New("mark_sweep_compact"); + case GcTypeIncrementalMarking: return Nan::New("incremental_marking"); + case GcTypeProcessWeakCallbacks: return Nan::New("process_weak_callbacks"); + default: return Nan::New("all"); + } +} + NAN_GC_CALLBACK(GcPrologue) { state.gc.startTime = uv_hrtime(); v8::HeapStatistics heapStats; @@ -151,7 +170,7 @@ NAN_METHOD(CollectCounters) { auto typeObj = Nan::New(); WriteCounters(typeObj, "collected", gcStats.amount); WriteCounters(typeObj, "duration", gcStats.time); - Nan::Set(gcObj, Nan::New(gcStats.type).ToLocalChecked(), typeObj); + Nan::Set(gcObj, GcTypeString(gcStats.type).ToLocalChecked(), typeObj); } Nan::Set(obj, Nan::New("gc").ToLocalChecked(), gcObj);