Skip to content

Instantly share code, notes, and snippets.

@tateisu
Last active Dec 29, 2019
Embed
What would you like to do?
Is Kotlin's T?.apply{...} slow?
class Holder : HashMap<String, String>()
class PrefKey(private val key: String) {
fun put(holder: Holder, value: String) {
holder[key] = value
}
}
fun Holder.putA(pref: PrefKey, value: String): Holder {
pref.put(this, value)
return this
}
fun Holder.putB(pref: PrefKey, value: String): Holder =
apply {
pref.put(this, value)
}
fun main() {
val repeats = 1..1000000000
val pref = PrefKey("foo")
bench("putA") {
val holder = Holder()
for (i in repeats) {
holder.putA(pref, "zap")
}
}
bench("putB") {
val holder = Holder()
for (i in repeats) {
holder.putB(pref, "zap")
}
}
}
fun <T : Any?> bench(caption: String, body: () -> T): T {
val start = System.currentTimeMillis()
try {
return body()
} finally {
val end = System.currentTimeMillis()
println("${end - start}ms $caption")
}
}
/*
1522ms putA
4242ms putB
@NotNull
public static final Holder putA(@NotNull Holder $this$putA, @NotNull Pref pref, @NotNull String value) {
Intrinsics.checkParameterIsNotNull($this$putA, "$this$putA");
Intrinsics.checkParameterIsNotNull(pref, "pref");
Intrinsics.checkParameterIsNotNull(value, "value");
pref.put($this$putA, value);
return $this$putA;
}
@NotNull
public static final Holder putB(@NotNull Holder $this$putB, @NotNull Pref pref, @NotNull String value) {
Intrinsics.checkParameterIsNotNull($this$putB, "$this$putB");
Intrinsics.checkParameterIsNotNull(pref, "pref");
Intrinsics.checkParameterIsNotNull(value, "value");
boolean var4 = false;
boolean var5 = false;
int var7 = false;
pref.put($this$putB, value);
return $this$putB;
}
*/
// access flags 0x19
public final static putA(LHolder;LPrefKey;Ljava/lang/String;)LHolder;
@Lorg/jetbrains/annotations/NotNull;() // invisible
// annotable parameter count: 3 (visible)
// annotable parameter count: 3 (invisible)
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 1
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 2
L0
LINENUMBER 10 L0
ALOAD 1
ALOAD 0
ALOAD 2
INVOKEVIRTUAL PrefKey.put (LHolder;Ljava/lang/String;)V
L1
LINENUMBER 11 L1
ALOAD 0
ARETURN
L2
LOCALVARIABLE $this$putA LHolder; L0 L2 0
LOCALVARIABLE pref LPrefKey; L0 L2 1
LOCALVARIABLE value Ljava/lang/String; L0 L2 2
MAXSTACK = 3
MAXLOCALS = 3
// access flags 0x19
public final static putB(LHolder;LPrefKey;Ljava/lang/String;)LHolder;
@Lorg/jetbrains/annotations/NotNull;() // invisible
// annotable parameter count: 3 (visible)
// annotable parameter count: 3 (invisible)
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 1
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 2
L0
LINENUMBER 15 L0
ALOAD 0
ASTORE 3
L1
ICONST_0
ISTORE 4
L2
ICONST_0
ISTORE 5
L3
ALOAD 3
ASTORE 6
L4
ICONST_0
ISTORE 7
L5
LINENUMBER 16 L5
ALOAD 1
ALOAD 6
ALOAD 2
INVOKEVIRTUAL PrefKey.put (LHolder;Ljava/lang/String;)V
L6
LINENUMBER 17 L6
L7
NOP
L8
LINENUMBER 15 L8
L9
ALOAD 3
L10
LINENUMBER 17 L10
ARETURN
L11
LOCALVARIABLE $this$apply LHolder; L4 L7 6
LOCALVARIABLE $i$a$-apply-Test1Kt$putB$1 I L5 L7 7
LOCALVARIABLE $this$putB LHolder; L0 L11 0
LOCALVARIABLE pref LPrefKey; L0 L11 1
LOCALVARIABLE value Ljava/lang/String; L0 L11 2
MAXSTACK = 3
MAXLOCALS = 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment