kotlin 内联_Kotlin中的内联和优化类型参数

kotlin 内联

Inline functions replace the function call with the body of the function itself. Hence the overhead of the function call is saved. The overhead is much more when the function is a higher order function, i.e, contains a lambda as a function parameter. Inline functions come to the rescue and inline the called function as well as lambda body.

内联函数将函数调用替换为函数本身的主体。 因此,节省了函数调用的开销。 当函数是高阶函数(即包含lambda作为函数参数)时,开销会更大。 内联函数可以解救并内联被调用函数以及lambda主体。

Reified Type Parameters along with inline keyword in Kotlin are used in the context of generics. At runtime, any information about the actual type arguments used by the generic class instances are erased, which means we cannot access the generic type <T> during runtime. Type Erasure exists in Java as well. However, Kotlin provides a way to access the type parameter during runtime using reified with inline.

泛型的上下文中使用了Kotlin中的Reified Type参数和inline关键字。 在运行时,将删除有关通用类实例使用的实际类型参数的所有信息,这意味着我们无法在运行时访问通用类型<T>。 Java中也存在类型擦除。 但是,Kotlin提供了一种使用内联的方式在运行时访问type参数的方法。

inline fun execute() {print("Hello")
}

The decompiled Kotlin byte code(Tools->Kotlin->Show Kotlin Bytecode->Decompile) for the execute() function call is somewhat as shown below:

为execute()函数调用反编译的Kotlin字节码(工具-> Kotlin-> Show Kotlin字节码->反编译)如下所示:

int $i$f$execute = false;
String var4 = "Hello";
boolean var5 = false;
System.out.print(var4);

To elaborate, using inline keyword, the compiler replaces the call to the inline function with the body of the function.

详细地说,使用inline关键字,编译器将对inline函数的调用替换为该函数的主体。

The higher order functions in Kotlin are converted into class objects, implementing the lambda function, at compilation time and a virtual call to the function is made when the lambda is invoked. This is all over an expensive operation and introduces run time overhead due to memory allocations for class and object and the virtual function call.

Kotlin中的高阶函数会在编译时转换为类对象,以实现lambda函数,并且在调用lambda时会对该函数进行虚拟调用。 这遍及了所有昂贵的操作,并且由于为类和对象以及虚拟函数调用分配内存而导致运行时开销。

fun execute(myfun : () -> Unit) {
myfun()
}

The decompiled Kotlin bytecode for the above function is :

以上功能的反编译Kotlin字节码为:

public final void execute(@NotNull Function0 myfun) {
Intrinsics.checkParameterIsNotNull(myfun, "myfun");
myfun.invoke();
}

Here, we can see that the invoke() function is being called on the created myfun object. It works like shown below in Java :

在这里,我们可以看到在创建的myfun对象上调用了invoke()函数。 它的工作方式类似于下面的Java所示:

execute(new Function0() {
@Override
public void invoke() {print(“Hello”);}});

The overhead due to memory allocations would be larger if multiple lambdas are high.

如果多个lambda高,则由于内存分配而导致的开销会更大。

We can overcome this runtime overhead by using the inline keyword as shown below:

我们可以通过使用inline关键字来克服这种运行时开销,如下所示:

inline fun execute(myfun : () -> Unit) {
myfun()
}

For the execute function call,

对于执行函数调用,

execute { print("Hello") }

the decompiled Kotlin bytecode for the execute() function call now becomes as shown below:

现在,用于execute()函数调用的反编译Kotlin字节码如下所示:

int $i$f$execute = false;int var4 = false;
String var5 = "Hello";boolean var6 = false;
System.out.print(var5);

Here, we can see that the call to the execute function is replaced by the code in the body of the execute function.

在这里,我们可以看到对execute函数的调用已替换为execute函数主体中的代码。

修饰类型参数 (Reified Type Parameters)

As said above, any information about the actual type arguments used by the generic class instances are erased and not available at runtime. Sometimes, we need to access the generic type parameter at runtime. This can be done by using inline keyword combined with reified modifier with the type parameter. To use reified type parameters, a function has to be an inline function.

如上所述,有关通用类实例使用的实际类型参数的任何信息都将被擦除,并且在运行时不可用。 有时,我们需要在运行时访问通用类型参数。 这可以通过将inline关键字与类型参数与reified修饰符结合使用来完成。 要使用修饰的类型参数,一个函数必须是一个内联函数。

See the example below for reference.

请参阅下面的示例以供参考。

inline fun <reified T> execute(input: T) {if(T::class == String::class) {println((input as String).length)
} else if(T::class == Int::class){println(input)
}
}

翻译自: https://medium.com/swlh/inline-and-reified-type-parameters-in-kotlin-c7585490e103

kotlin 内联

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值