GithubHelp home page GithubHelp logo

Comments (3)

Quyunshuo avatar Quyunshuo commented on July 23, 2024

我在知乎上看到这么一段话

Kotlin 并不建议将非高阶函数定义为内联函数:

inline fun foo() = println("foo")

编译,Kotlin 编译器会给我们一个警告:

Warning:(1, 1) 
Kotlin: Expected performance impact of inlining 'public inline fun foo(): Unit defined in root package' can be insignificant. 
Inlining works best for functions with lambda parameters

编译器告诉我们,内联最好与有 Lambda 表达式参数的函数配合使用,内联这里的 foo() 函数对性能的影响微乎其微。

from ktkit.

hi-dhl avatar hi-dhl commented on July 23, 2024

Inline (内联函数) 的作用:提升运行效率,调用被 inline 修饰符的函数,会把里面的代码放到我调用的地方。

Inline 修饰符适用于下面两种情况,编译器才不会有警告

  • 将函数类型作为参数
  • 结合 reified 实化类型参数参数

很多扩展方法代码段很短,使用 Inline 修饰符, 将方法体的代码段放到调用处,来提升性能(虽然微乎其微),但是编译器会给出警告,所以添加 InlineOnly 注解有以下作用

  • 消除警告
  • 修改内联函数的可见性,默认是public, 加上修饰符,编译时改成 private

from ktkit.

Quyunshuo avatar Quyunshuo commented on July 23, 2024

学习了

from ktkit.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.