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

Commit 860650f3 authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato
Browse files

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
parent a171c59c
Loading
Loading
Loading
Loading
+16 −18
Original line number Diff line number Diff line
@@ -39,32 +39,30 @@ 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 {
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 {
            Trace.traceEnd(Trace.TRACE_TAG_APP)
        if (tracingEnabled) Trace.traceEnd(Trace.TRACE_TAG_APP)
    }
    } else {
        block()
}

/**
 * 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 {
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 {
            Trace.traceEnd(Trace.TRACE_TAG_APP)
        if (tracingEnabled) Trace.traceEnd(Trace.TRACE_TAG_APP)
    }
    } else {
        block()
}

class TraceUtils {