diff options
author | Nicolo' Mazzucato <nicomazz@google.com> | 2023-11-28 12:40:44 +0000 |
---|---|---|
committer | Nicolo' Mazzucato <nicomazz@google.com> | 2023-11-28 12:40:44 +0000 |
commit | 860650f33a3702debee4acffebce0b7ee5c3828f (patch) | |
tree | 235fff07e210128d05a59a35ca1794e7c20b9eb3 | |
parent | a171c59cead158763d9f6b3fdde8da71dcfe0988 (diff) | |
download | systemui-860650f33a3702debee4acffebce0b7ee5c3828f.tar.gz |
Reduce odex size by calling block inside traceSection only once
This pays a small performance penality as the try/finally block is always executed even if tracing is disabled, but reduces the bytecode generated by half for each traceSection usage.
Bug: 313614506
Test: presubmits
Flag: None
Change-Id: I9f7014a7f6d01ec4f1ca8949d3a46fba4cc6804f
-rw-r--r-- | tracinglib/src/com/android/app/tracing/TraceUtils.kt | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/tracinglib/src/com/android/app/tracing/TraceUtils.kt b/tracinglib/src/com/android/app/tracing/TraceUtils.kt index 744b006..6bd3858 100644 --- a/tracinglib/src/com/android/app/tracing/TraceUtils.kt +++ b/tracinglib/src/com/android/app/tracing/TraceUtils.kt @@ -39,33 +39,31 @@ import kotlinx.coroutines.withContext * Run a block within a [Trace] section. Calls [Trace.beginSection] before and [Trace.endSection] * after the passed block. */ -inline fun <T> traceSection(tag: String, block: () -> T): T = - if (Trace.isTagEnabled(Trace.TRACE_TAG_APP)) { - Trace.traceBegin(Trace.TRACE_TAG_APP, tag) - try { - block() - } finally { - Trace.traceEnd(Trace.TRACE_TAG_APP) - } - } else { +inline fun <T> traceSection(tag: String, block: () -> T): T { + val tracingEnabled = Trace.isTagEnabled(Trace.TRACE_TAG_APP) + if (tracingEnabled) Trace.traceBegin(Trace.TRACE_TAG_APP, tag) + return try { + // Note that as this is inline, the block section would be duplicated if it is called + // several times. For this reason, we're using the try/finally even if tracing is disabled. block() + } finally { + if (tracingEnabled) Trace.traceEnd(Trace.TRACE_TAG_APP) } +} /** * Same as [traceSection], but the tag is provided as a lambda to help avoiding creating expensive * strings when not needed. */ -inline fun <T> traceSection(tag: () -> String, block: () -> T): T = - if (Trace.isTagEnabled(Trace.TRACE_TAG_APP)) { - Trace.traceBegin(Trace.TRACE_TAG_APP, tag()) - try { - block() - } finally { - Trace.traceEnd(Trace.TRACE_TAG_APP) - } - } else { +inline fun <T> traceSection(tag: () -> String, block: () -> T): T { + val tracingEnabled = Trace.isTagEnabled(Trace.TRACE_TAG_APP) + if (tracingEnabled) Trace.traceBegin(Trace.TRACE_TAG_APP, tag()) + return try { block() + } finally { + if (tracingEnabled) Trace.traceEnd(Trace.TRACE_TAG_APP) } +} class TraceUtils { companion object { |