SceneRenderer 中的 Kotlin 内联函数
Kotlin Inline function in SceneRenderer
如果在渲染循环中使用内联函数,是否会有任何性能优势?
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()
}
}
还是直接调用函数就够了?
override fun onDrawFrame(glUnused: GL10) {
...
f.invoke()
...
}
提前致谢!
TL;DR
Or is it enough to use a direct function call?
是的,是的。
内联
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.
您的函数不是高阶函数,因为它在 class SceneRenderer
内部,因此不会对 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.
如果在渲染循环中使用内联函数,是否会有任何性能优势?
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()
}
}
还是直接调用函数就够了?
override fun onDrawFrame(glUnused: GL10) {
...
f.invoke()
...
}
提前致谢!
TL;DR
Or is it enough to use a direct function call?
是的,是的。
内联
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.
您的函数不是高阶函数,因为它在 class SceneRenderer
内部,因此不会对 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.