TracedOrdinal per-frame optimizations

Optimizes TracedOrdinal by only doing work inside the trace method when
tracing is enabled, and by deferring the generation of the *Negative
variant (which involves a StringPrintf) until the first time the
variable is traced.

Test: atest libsurfaceflinger_unittest
Bug: 153112939
Change-Id: I14e316797c032b78fb2ed99c3a7db5b59a470845
diff --git a/services/surfaceflinger/TracedOrdinal.h b/services/surfaceflinger/TracedOrdinal.h
index c145a39..4e7f67d 100644
--- a/services/surfaceflinger/TracedOrdinal.h
+++ b/services/surfaceflinger/TracedOrdinal.h
@@ -16,10 +16,13 @@
 
 #pragma once
 #include <android-base/stringprintf.h>
+#include <cutils/compiler.h>
 #include <utils/Trace.h>
 #include <cmath>
 #include <string>
 
+namespace android {
+
 template <typename T>
 class TracedOrdinal {
 public:
@@ -27,9 +30,8 @@
                   "Type is not supported. Please test it with systrace before adding "
                   "it to the list.");
 
-    TracedOrdinal(const std::string& name, T initialValue)
-          : mName(name),
-            mNameNegative(android::base::StringPrintf("%sNegative", name.c_str())),
+    TracedOrdinal(std::string name, T initialValue)
+          : mName(std::move(name)),
             mHasGoneNegative(std::signbit(initialValue)),
             mData(initialValue) {
         trace();
@@ -46,6 +48,14 @@
 
 private:
     void trace() {
+        if (CC_LIKELY(!ATRACE_ENABLED())) {
+            return;
+        }
+
+        if (mNameNegative.empty()) {
+            mNameNegative = base::StringPrintf("%sNegative", mName.c_str());
+        }
+
         if (!std::signbit(mData)) {
             ATRACE_INT64(mName.c_str(), int64_t(mData));
             if (mHasGoneNegative) {
@@ -58,7 +68,9 @@
     }
 
     const std::string mName;
-    const std::string mNameNegative;
+    std::string mNameNegative;
     bool mHasGoneNegative;
     T mData;
 };
+
+} // namespace android