Nested Inline Functions - Any Negative Impacts?

I have some nested inline calls in my code base (Ktor Server), just wanted to know does these nested calls have any performance impact or any negative impact? if yes, how do i make sure that i’m keeping them as minimum possible.

Thanks in advance!

suspend inline fun <reified T:Request> withRequest(block: (T) -> Unit){
        val request =  receive<T>()
        if(request.isValidRequest()){
            block(request)
        }else{
            throw InvalidInputs()
        }
    }

suspend inline fun computeResponse(...) {
  .. Measure Time...
   ...Compute Logic ...
}

suspend inline fun authorizedUser(
    status: Int
    requiredPrivilege: Int,
    block: (UserPrincipal, Int) -> ApiResponse
) {
    .... Check Privilege and ExecuteBlock
}

authorizedUser(200){ prinicpal,status
withRequest<DataRequest>{ request ->
computeResponse{
dataProviderService.getData(principal.userID,request.dataID).toApiResponse();
}
}
}

Inline functions could be nested. There are some bizarre compiler bugs about nesting, but it works in general. On the other hand, manual inlining on JVM usually does not give performance boost. On the opposite, JVM internal inlining is smarter and faster than the manual one. So if you are doinig inlines to improve performance, you must measure it to be sure it became better.

1 Like

As @darksnake says, inlining doesn’t necessarily give a benefit on the JVM. It can give real improvements for functions that accept and use lambdas, especially if they’re small; and it’s needed for non-local returns and for reified generics (as in the withRequest() function); but otherwise the JVM will often do a better job without. So I wouldn’t recommend it for the other two functions.

If you use IDEA, it will probably warn you where inline may not be a good idea. (Many of IDEA’s warnings are well worth paying attention to.)

1 Like