android - SceneRenderer中的Kotlin内联函数

标签 android kotlin opengl-es

如果在渲染循环中使用内联函数,会不会有性能优势?

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/

相关文章:

java - 在房间版本 1.1.1 中创建复合主键时无法使用 Kotlin 编译器构建 android studio 项目

android - 为 Kotlin 多平台项目运行单元测试时出错

具有可绘制背景的 Android GLSurfaceView

android - 如何在包括平板电脑在内的屏幕分辨率范围内保持 ImageView 大小和字体大小

android - libGDX 警报对话框

spring - Kotlin application.yml 数据类寻找 beans

android - 从服务器 : Forbidden 收到状态码 403

opengl-es - 将负值混合到帧缓冲区 0 opengl

android - 我可以在 android 中使用 GLSurfaceView 录制视频吗?

android - 按下主页按钮时的调用方法