From e8eaa490af694366f2347282c8f87337a0de0173 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Tue, 10 Jan 2023 00:39:48 +0800 Subject: [PATCH] src: use constant strings for memory info names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/46087 Reviewed-By: Paolo Insogna Reviewed-By: Ben Noordhuis Reviewed-By: Darshan Sen Reviewed-By: Tobias Nießen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/async_wrap.cc | 12 +++++++++--- src/async_wrap.h | 2 +- src/crypto/crypto_util.h | 2 +- src/memory_tracker-inl.h | 4 ++-- src/memory_tracker.h | 6 +++--- src/node_process_methods.cc | 4 ++-- src/node_realm.cc | 2 +- src/tcp_wrap.h | 2 +- 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/async_wrap.cc b/src/async_wrap.cc index ff550fdc1de5d0..e70011f0b7ba4c 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -673,13 +673,19 @@ MaybeLocal AsyncWrap::MakeCallback(const Local cb, return ret; } -std::string AsyncWrap::MemoryInfoName() const { +const char* AsyncWrap::MemoryInfoName() const { return provider_names[provider_type()]; } std::string AsyncWrap::diagnostic_name() const { - return MemoryInfoName() + " (" + std::to_string(env()->thread_id()) + ":" + - std::to_string(static_cast(async_id_)) + ")"; + char buf[64]; + snprintf(buf, + sizeof(buf), + "%s(%" PRIu64 ":%.0f)", + MemoryInfoName(), + env()->thread_id(), + async_id_); + return buf; } Local AsyncWrap::GetOwner() { diff --git a/src/async_wrap.h b/src/async_wrap.h index 5ccf3f86bac532..71a205376d2767 100644 --- a/src/async_wrap.h +++ b/src/async_wrap.h @@ -209,7 +209,7 @@ class AsyncWrap : public BaseObject { v8::Local* argv); virtual std::string diagnostic_name() const; - std::string MemoryInfoName() const override; + const char* MemoryInfoName() const override; static void WeakCallback(const v8::WeakCallbackInfo &info); diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index 716b40be50cdc2..fd014e703454dd 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -398,7 +398,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork { AdditionalParams* params() { return ¶ms_; } - std::string MemoryInfoName() const override { + const char* MemoryInfoName() const override { return CryptoJobTraits::JobName; } diff --git a/src/memory_tracker-inl.h b/src/memory_tracker-inl.h index 4757bee734d738..6d927c72888181 100644 --- a/src/memory_tracker-inl.h +++ b/src/memory_tracker-inl.h @@ -44,7 +44,7 @@ class MemoryRetainerNode : public v8::EmbedderGraph::Node { is_root_node_ = is_root_node; } - const char* Name() override { return name_.c_str(); } + const char* Name() override { return name_; } const char* NamePrefix() override { return "Node /"; } size_t SizeInBytes() override { return size_; } // TODO(addaleax): Merging this with the "official" WrapperNode() method @@ -75,7 +75,7 @@ class MemoryRetainerNode : public v8::EmbedderGraph::Node { // Otherwise (retainer == nullptr), we set these fields in an ad-hoc way bool is_root_node_ = false; - std::string name_; + const char* name_; size_t size_ = 0; v8::EmbedderGraph::Node::Detachedness detachedness_ = v8::EmbedderGraph::Node::Detachedness::kUnknown; diff --git a/src/memory_tracker.h b/src/memory_tracker.h index 878938bc48532e..4682f25916e52d 100644 --- a/src/memory_tracker.h +++ b/src/memory_tracker.h @@ -17,7 +17,7 @@ namespace node { // Set the node name of a MemoryRetainer to klass #define SET_MEMORY_INFO_NAME(Klass) \ - inline std::string MemoryInfoName() const override { return #Klass; } + inline const char* MemoryInfoName() const override { return #Klass; } // Set the self size of a MemoryRetainer to the stack-allocated size of a // certain class @@ -68,7 +68,7 @@ class CleanupHookCallback; * } * * // Or use SET_MEMORY_INFO_NAME(ExampleRetainer) - * std::string MemoryInfoName() const override { + * const char* MemoryInfoName() const override { * return "ExampleRetainer"; * } * @@ -119,7 +119,7 @@ class MemoryRetainer { // where all the edges start from the node of the current retainer, // and point to the nodes as specified by tracker->Track* calls. virtual void MemoryInfo(MemoryTracker* tracker) const = 0; - virtual std::string MemoryInfoName() const = 0; + virtual const char* MemoryInfoName() const = 0; virtual size_t SelfSize() const = 0; virtual v8::Local WrappedObject() const { diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 1dd06664e410fe..c429f1b50ee09a 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -283,14 +283,14 @@ static void GetActiveResourcesInfo(const FunctionCallbackInfo& args) { AsyncWrap* w = req_wrap->GetAsyncWrap(); if (w->persistent().IsEmpty()) continue; resources_info.emplace_back( - OneByteString(env->isolate(), w->MemoryInfoName().c_str())); + OneByteString(env->isolate(), w->MemoryInfoName())); } // Active handles for (HandleWrap* w : *env->handle_wrap_queue()) { if (w->persistent().IsEmpty() || !HandleWrap::HasRef(w)) continue; resources_info.emplace_back( - OneByteString(env->isolate(), w->MemoryInfoName().c_str())); + OneByteString(env->isolate(), w->MemoryInfoName())); } // Active timeouts diff --git a/src/node_realm.cc b/src/node_realm.cc index 42d80ac467fa97..f1c02bdd61855d 100644 --- a/src/node_realm.cc +++ b/src/node_realm.cc @@ -357,7 +357,7 @@ void Realm::VerifyNoStrongBaseObjects() { if (obj->IsNotIndicativeOfMemoryLeakAtExit()) return; fprintf(stderr, "Found bad BaseObject during clean exit: %s\n", - obj->MemoryInfoName().c_str()); + obj->MemoryInfoName()); fflush(stderr); ABORT(); }); diff --git a/src/tcp_wrap.h b/src/tcp_wrap.h index b561fef0d31593..a4684b65d24934 100644 --- a/src/tcp_wrap.h +++ b/src/tcp_wrap.h @@ -50,7 +50,7 @@ class TCPWrap : public ConnectionWrap { SET_NO_MEMORY_INFO() SET_SELF_SIZE(TCPWrap) - std::string MemoryInfoName() const override { + const char* MemoryInfoName() const override { switch (provider_type()) { case ProviderType::PROVIDER_TCPWRAP: return "TCPSocketWrap";