![3c83866a64780135b3a7926422b16b61.png](https://img-blog.csdnimg.cn/img_convert/3c83866a64780135b3a7926422b16b61.png)
[译]探索Kotlin中隐藏的性能开销-Part 3
翻译说明:
原标题# Exploring Kotlin’s hidden costs — Part 3
原文地址: https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-2-324a4a50b70
原文作者: Christophe Beyls
代理属性和Range
在发布有关Kotlin编程语言的性能开销系列的前两篇文章之后,我收到了很多不错的反馈,甚至还包括 Jake Wharton 大神他自己。所以你还没看前两篇文章,千万不要错过哦。
在第3部分中,我们将揭开更多有关Kotlin编译器的秘密,并提供如何编写更高效代码的新技巧。
![2236607e536f4e22a214cfd7f42a83db.png](https://img-blog.csdnimg.cn/img_convert/2236607e536f4e22a214cfd7f42a83db.png)
一、代理属性
代理属性是一种其getter和可选的setter的内部实现可由代理的外部对象提供的属性。它可以允许复用自定义属性的内部实现。
class Example {
var p: String by Delegate()
}
这个代理对象必须实现一个 operator getVlue()
函数,以及一个 setValue()
函数来用于属性的读/写. 这些函数将接收包含对象实例 以及属性的metadata元数据 作为额外参数(比如它的属性名)。
当类中声明一个代理属性时,编译将生成以下代码(下面是反编译后的Java代码):
public final class Example {
@NotNull
private final Delegate p$delegate = new Delegate();
// $FF: synthetic field
static final KProperty[] $$delegatedProperties = new KProperty[]{(KProperty)Reflection.mutableProperty1(new MutablePropertyReference1Impl(Reflection.getOrCreateKotlinClass(Example.class), "p", "getP()Ljava/lang/String;"))};
@NotNull
public final String getP() {
return this.p$delegate.getValue(this, $$delegatedProperties[0]);
}
public final void setP(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.p$delegate.setValue(this, $$delegatedProperties[0], var1);
}
}
一些静态属性metadata元数据被添加到类中。代理将在类的构造器中进行初始化,然后在每次读取或写入属性时都调用该代理。
代理实例
在上述例子中,将会创建一个新的代理对象的实例来实现该属性。当代理实例是有状态的时候, 这就是必需的,例如在计算本地缓存属性的值时.
class StringDelegate {
private var cache: String? = null
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
var result = cache
if (result == null) {
result = someOperation()
cache = result
}
return result
}
}
如果还需要通过其构造函数传递的额外参数,则还需要创建一个新的代理实例:
class Example {
private val nameView by BindViewDelegate<TextView>(R.id.name)
}
但是在某些情况下,只需要一个代理实例就可以实现任意属性: 当代理实例是无状态的时候,并且它执行所需的唯一变量就是对象实例和属性名称(然而这些编译器都直接提供了)。在这种情况下,可以通过将代理实例声明成object对象表达式而不是一个类来使得成为单例。