如果在渲染循环中使用内联函数,会不会有性能优势?
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?
是的。
排队
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.
您的函数不是高阶函数,因为它位于
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.
关于android - SceneRenderer中的Kotlin内联函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257967/