https://kotlinlang.org logo
#android
Title
# android
f

Fredrik Larsen

03/25/2019, 9:52 AM
Due to dead code elimination I usually do:
Copy code
if (BuildConfig.DEBUG) Timber.d(...)
Is the compiler smart enough to do the same if I make an inline function for that check?
d

dawidhyzy

03/25/2019, 10:03 AM
Why don’t you plant Timber tree only if BuildConfig.DEBUG?
f

Fredrik Larsen

03/25/2019, 10:22 AM
I have a debug tree, I'm not sure what you're getting at? The point is to remove unused code. Without that if check (and dead code elimination) any log call would be called, but it wouldn't print it.
d

dawidhyzy

03/25/2019, 10:29 AM
maybe you have ProGuard rules like that:
Copy code
-assumenosideeffects class timber.log.Timber* {
    public static *** v(...);
    public static *** d(...);
    public static *** i(...);
}
f

Fredrik Larsen

03/25/2019, 12:35 PM
ProGuard cannot remove the variables that are used indirectly though. So this use case cannot be solved by the rule above:
Copy code
if (BuildConfig.DEBUG) {
    val foo = expensiveBar()
    Timber.d("Critical: ${foo.bar}")
}
So what I'm looking for is some way to do dead code elimination without wrapping it in the explicit
BuildConfig.DEBUG
check 🙂
And the question is if the compiler is smart enough to infer that from an inline function. I'll test it. I was just wondering if someone knew.
d

Dico

03/27/2019, 3:43 PM
I am not sure that the compiler performs dead code elimination on the first example even. The JVM will take care of it nonetheless.
9 Views