Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8ea808c0 authored by Dan Stoza's avatar Dan Stoza
Browse files

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
parent 28d46a54
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -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 @@ public:
                  "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 @@ public:

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 @@ private:
    }

    const std::string mName;
    const std::string mNameNegative;
    std::string mNameNegative;
    bool mHasGoneNegative;
    T mData;
};

} // namespace android