Commit dd7bff52cef for nodejs
commit dd7bff52cef7cd3b4c711577bb7f10fa45e2b99a
Author: Gürgün Dayıoğlu <hey@gurgun.day>
Date: Mon Nov 23 21:45:49 2026 +0100
src: cache trace category lookups on hot paths
The sync fs, fs dir, AsyncWrap and vm.Script tracing guards called
TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED directly, which does a
by-name search in the tracing controller on every call. Use
TRACE_EVENT_CATEGORY_ENABLED instead, which caches the category
pointer per call site like the other TRACE_EVENT macros. Perfetto
already provides it; add an equivalent for the legacy tracing build.
Assisted-by: claude:opus-5.5
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
PR-URL: https://github.com/nodejs/node/pull/67502
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/src/async_wrap.cc b/src/async_wrap.cc
index 2030599e079..3869656eb21 100644
--- a/src/async_wrap.cc
+++ b/src/async_wrap.cc
@@ -111,8 +111,7 @@ void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
}
void AsyncWrap::EmitTraceAsyncStart() const {
- if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
- TRACING_CATEGORY_NODE1(async_hooks))) {
+ if (TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE1(async_hooks))) {
tracing::AsyncWrapArgs data(env()->execution_async_id(),
get_trigger_async_id());
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(async_hooks),
diff --git a/src/node_contextify.cc b/src/node_contextify.cc
index 989ce5d6750..ed76490bf28 100644
--- a/src/node_contextify.cc
+++ b/src/node_contextify.cc
@@ -1014,8 +1014,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
ContextifyScript* contextify_script = New(env, args.This());
- if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
- TRACING_CATEGORY_NODE2(vm, script)) != 0) {
+ if (TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE2(vm, script))) {
Utf8Value fn(isolate, filename);
TRACE_EVENT_BEGIN1(TRACING_CATEGORY_NODE2(vm, script),
"ContextifyScript::New",
diff --git a/src/node_dir.cc b/src/node_dir.cc
index ca7fc340bcd..957444eea33 100644
--- a/src/node_dir.cc
+++ b/src/node_dir.cc
@@ -61,8 +61,7 @@ static const char* get_dir_func_name_by_type(uv_fs_type req_type) {
#define TRACE_NAME(name) "fs_dir.sync." #name
#define GET_TRACE_ENABLED \
- (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \
- TRACING_CATEGORY_NODE2(fs_dir, sync)) != 0)
+ TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE2(fs_dir, sync))
#define FS_DIR_SYNC_TRACE(syscall) \
if (GET_TRACE_ENABLED) { \
TRACE_EVENT0(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall)); \
diff --git a/src/node_file.cc b/src/node_file.cc
index e4db8be0232..26be951240a 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -165,8 +165,7 @@ static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
#define TRACE_NAME(name) "fs.sync." #name
#define GET_TRACE_ENABLED \
- (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \
- TRACING_CATEGORY_NODE2(fs, sync)) != 0)
+ TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE2(fs, sync))
// Checks a permission for an fs binding. A denial is reported through
// `req_wrap_async` for an asynchronous call, or thrown for a synchronous call
// (when `req_wrap_async` is nullptr).
diff --git a/src/tracing/trace_event_legacy.h b/src/tracing/trace_event_legacy.h
index 92b2e88d482..9dc218ec671 100644
--- a/src/tracing/trace_event_legacy.h
+++ b/src/tracing/trace_event_legacy.h
@@ -1012,6 +1012,14 @@
} \
} while (0)
+// Expression form of the above, matching Perfetto's macro of the same name.
+#define TRACE_EVENT_CATEGORY_ENABLED(category_group) \
+ ([] { \
+ bool enabled; \
+ TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, &enabled); \
+ return enabled; \
+ }())
+
// Macro to explicitly warm up a given category group. This could be useful in
// cases where we want to initialize a category group before any trace events
// for that category group is reported. For example, to have a category group