Eliminate lambdas from tracing helper macro
In the change with ID Ibee1e7dc5021296bdb5871dec59d8d4978fcf0c9,
we introduced lambdas at the tracing sites so that we could
conditionally execute the code to generate the tracing string only when
tracing was enabled. However, it seems like the introduction of the
lambdas, especially ones that capture everything by reference, have
added a performance burden, causing ~5% increase in latency in the input
pipeline.
In this CL, we remove the lambdas from the tracing sites, and instead
rely on the expression evaluation guarantees of the ternary to ensure
that the expression to generate the trace message will only be evaluated
when the condition is true.
Bug: 297462790
Test: Will evaluate perf metric after submitting
Change-Id: I69b37551c4b23256c64544e0ddf4b5b39a9403fb
diff --git a/include/input/TraceTools.h b/include/input/TraceTools.h
index 70b23c5..81fc7af 100644
--- a/include/input/TraceTools.h
+++ b/include/input/TraceTools.h
@@ -19,7 +19,11 @@
#include <utils/Trace.h>
#include <optional>
-#define ATRACE_NAME_IF(condition, messageProvider) \
- const auto _trace_token = condition \
- ? std::make_optional<android::ScopedTrace>(ATRACE_TAG, messageProvider().c_str()) \
+// A macro for tracing when the given condition is true.
+// This macro relies on the fact that only one branch of the ternary operator is evaluated. That
+// means if `message` is an expression that evaluates to a std::string value, the value will
+// not be computed unless the condition is true.
+#define ATRACE_NAME_IF(condition, message) \
+ const auto _trace_token = condition \
+ ? std::make_optional<android::ScopedTrace>(ATRACE_TAG, (message).c_str()) \
: std::nullopt