Skip to content

Commit 3949e0a

Browse files
gurgundayaduh95
authored andcommitted
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: #67502 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5b15842 commit 3949e0a

5 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎src/async_wrap.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
111111
}
112112

113113
void AsyncWrap::EmitTraceAsyncStart() const {
114-
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
115-
TRACING_CATEGORY_NODE1(async_hooks))) {
114+
if (TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE1(async_hooks))) {
116115
tracing::AsyncWrapArgs data(env()->execution_async_id(),
117116
get_trigger_async_id());
118117
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(async_hooks),

‎src/node_contextify.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
10141014

10151015
ContextifyScript* contextify_script = New(env, args.This());
10161016

1017-
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
1018-
TRACING_CATEGORY_NODE2(vm, script)) != 0) {
1017+
if (TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE2(vm, script))) {
10191018
Utf8Value fn(isolate, filename);
10201019
TRACE_EVENT_BEGIN1(TRACING_CATEGORY_NODE2(vm, script),
10211020
"ContextifyScript::New",

‎src/node_dir.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ static const char* get_dir_func_name_by_type(uv_fs_type req_type) {
6161

6262
#define TRACE_NAME(name) "fs_dir.sync." #name
6363
#define GET_TRACE_ENABLED \
64-
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \
65-
TRACING_CATEGORY_NODE2(fs_dir, sync)) != 0)
64+
TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE2(fs_dir, sync))
6665
#define FS_DIR_SYNC_TRACE(syscall) \
6766
if (GET_TRACE_ENABLED) { \
6867
TRACE_EVENT0(TRACING_CATEGORY_NODE2(fs_dir, sync), TRACE_NAME(syscall)); \

‎src/node_file.cc‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
165165

166166
#define TRACE_NAME(name) "fs.sync." #name
167167
#define GET_TRACE_ENABLED \
168-
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \
169-
TRACING_CATEGORY_NODE2(fs, sync)) != 0)
168+
TRACE_EVENT_CATEGORY_ENABLED(TRACING_CATEGORY_NODE2(fs, sync))
170169
// Checks a permission for an fs binding. A denial is reported through
171170
// `req_wrap_async` for an asynchronous call, or thrown for a synchronous call
172171
// (when `req_wrap_async` is nullptr).

‎src/tracing/trace_event_legacy.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,14 @@
10121012
} \
10131013
} while (0)
10141014

1015+
// Expression form of the above, matching Perfetto's macro of the same name.
1016+
#define TRACE_EVENT_CATEGORY_ENABLED(category_group) \
1017+
([] { \
1018+
bool enabled; \
1019+
TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, &enabled); \
1020+
return enabled; \
1021+
}())
1022+
10151023
// Macro to explicitly warm up a given category group. This could be useful in
10161024
// cases where we want to initialize a category group before any trace events
10171025
// for that category group is reported. For example, to have a category group

0 commit comments

Comments
 (0)