Solved: KMM is slower on iOS then Android

In one of the applications, we use KMM to share code on both Android and iOS, but we find that some functions are much slower on iOS than…

Solved: KMM is slower on iOS then Android

In one of the applications, we use KMM to share code on both Android and iOS, but we find that some functions are much slower on iOS than on Android, this is what I have done to find the solution.

First try

I added a function to log method call time, the log method is like this:@Throws(Throwable::class)
@OptIn(ExperimentalTime::class)
internal inline fun <reified T> logTime(name: String, block: (() -> T)): T {
   Napier.d("[logTime] - Start method $name")
   val value: T
   val duration = measureTime {
       value = block.invoke()
   }
   Napier.d("[logTime] - The time duration for $name is $duration")
   return value
}

With this code, we can find that KMM methods are slower than Android, but I still can’t find the reason. I think the reason is the method call costs most of the time.

Find the reason with Instruments

One colleague suggests that we can use profiles to investigate the time spent on the functions, and on Xcode, there is an Instruments we can analyze the time of the functions.

After use the Instruments , I find that the Napier log methods spend a lot of time, actually is the performTag method cost the most of time, this method is when we call log method without the tag, it will calculate the class name and method of the function call the function, the implementation of the performTag is like below:// find stack trace
   private fun performTag(tag: String): String {
       val thread = NSThread.callStackSymbolsreturn if (thread.size >= CALL_STACK_INDEX) {
           createStackElementTag(thread[CALL_STACK_INDEX] as String)
       } else {
           tag
       }
   }

Solution

After find the reason, I added the tag to all the log methods, and at last, the KMM on iOS no longer slower.