android – Kotlin Inline function in SceneRenderer-ThrowExceptions

Exception or error:

If an inline function is used in a render loop, will there be any performance benefit?

class SceneRenderer(val f: () -> Unit): GLSurfaceView.Renderer {
  override fun onSurfaceCreated(glUnused: GL10, config: EGLConfig) { ... }

  override fun onSurfaceChanged(glUnused: GL10, width: Int, height: Int) { ... }

  override fun onDrawFrame(glUnused: GL10) {
    ...
    inlineFun(f)
    ...
  }

  private inline fun inlineFun(f: () -> Unit) {
    f.invoke()
  }
}

Or is it enough to use a direct function call?

override fun onDrawFrame(glUnused: GL10) {
  ...
  f.invoke()
  ...
}

Thanks in advance!

How to solve:

TL;DR

Or is it enough to use a direct function call?

Yes, it is.

Inline

Kotlinlang:

Using higher-order functions imposes certain runtime penalties: each
function is an object, and it captures a closure, i.e. those variables
that are accessed in the body of the function. Memory allocations
(both for function objects and classes) and virtual calls introduce
runtime overhead.

Your function is not high-order, since it is inside the class SceneRenderer and there will be no reasonable impact making it inline.

Inlining may cause the generated code to grow; however, if we do it in
a reasonable way (i.e. avoiding inlining large functions), it will pay
off in performance, especially at “megamorphic” call-sites inside
loops.

Leave a Reply

Your email address will not be published. Required fields are marked *