Optimize const vals by inlining them at use sites

#KT-11734 Fixed
 #KT-13570 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2016-12-05 12:02:52 +03:00
parent 8c1e165f18
commit 0134b8819b
28 changed files with 210 additions and 136 deletions
@@ -1,43 +0,0 @@
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS
//WITH_RUNTIME
//FULL_JDK
//NOTE this test should be removed if Kotlin const properties will became inlined
import java.lang.reflect.Field
import java.lang.reflect.Modifier
object ThingTemplate {
const val prop = 0
}
class ThingVal {
val prop = ThingTemplate.prop
}
class ThingVar {
var prop = ThingTemplate.prop
}
fun box() : String {
val template = ThingTemplate;
val javaClass = ThingTemplate::class.java
val field = javaClass.getDeclaredField("prop")!!
field.isAccessible = true
val modifiersField = Field::class.java!!.getDeclaredField("modifiers")
modifiersField.isAccessible = true
modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
field.set(null, 1)
val thingVal = ThingVal()
if (thingVal.prop != 1) return "fail 1"
val thingVar = ThingVar()
if (thingVar.prop != 1) return "fail 2"
return "OK"
}
@@ -1,3 +1,5 @@
// LANGUAGE_VERSION: 1.0
const val z = 0
fun a() {
@@ -1,3 +1,5 @@
// LANGUAGE_VERSION: 1.0
const val z = 0
fun a() {
@@ -0,0 +1,9 @@
const val one = 1
const val two = 2
fun test1() {
if (!(one < two)) {
val p = 1
}
}
// 0 IF
@@ -1,3 +1,5 @@
// LANGUAGE_VERSION: 1.0
const val one = 1
const val two = 2
@@ -0,0 +1,17 @@
const val A = 10
private const val B = 20
object Constants {
const val C = 30
}
fun foo(state: Int) {
when (state) {
A -> return
B -> return
Constants.C -> return
else -> return
}
}
// 1 LOOKUPSWITCH
@@ -0,0 +1,20 @@
object Constants {
const val A = 30
const val B = 40
}
class ClassConstants {
companion object {
const val C = 50
}
}
fun foo(state: Int) {
when (state) {
Constants.A -> return
Constants.B -> return
ClassConstants.C -> return
else -> return
}
}
// 1 LOOKUPSWITCH
@@ -0,0 +1,16 @@
const val A = 10
private const val B = 20
object Constants {
const val C = 30
}
fun foo(state: Int) {
when (state) {
A -> return
B -> return
else -> return
}
}
// 1 LOOKUPSWITCH
@@ -0,0 +1,12 @@
const val y = "cde"
fun foo(x : String) : String {
when (x) {
"abc", "${y}" -> return "abc_cde"
"e" + "fg", "ghi" -> return "efg_ghi"
}
return "other"
}
// 1 LOOKUPSWITCH
@@ -1,3 +1,5 @@
// LANGUAGE_VERSION: 1.0
const val y = "cde"
fun foo(x : String) : String {
@@ -38,8 +38,7 @@
{"visibility": "public", "declaration": "fun publicFun(): kotlin.Unit", "name": "publicFun", "desc": "()V"},
{"visibility": "internal", "declaration": "fun internalFun(param1: kotlin.Int): kotlin.Unit", "name": "internalFun", "desc": "(I)V"},
{"visibility": "private", "declaration": "fun privateFun(x: kotlin.Any): kotlin.Unit", "name": "privateFun$MultifileFacade__Part2Kt", "desc": "(Ljava/lang/Object;)V"},
{"visibility": "private", "declaration": "fun privateFun(x: kotlin.Any): kotlin.Unit", "name": "access$privateFun", "desc": "(Ljava/lang/Object;)V"},
{"visibility": "private", "declaration": "fun <get-privateConst>(): kotlin.Int", "name": "access$getPrivateConst$p", "desc": "()I"}
{"visibility": "private", "declaration": "fun privateFun(x: kotlin.Any): kotlin.Unit", "name": "access$privateFun", "desc": "(Ljava/lang/Object;)V"}
]
},
{
@@ -42,8 +42,7 @@
{"visibility": "private", "declaration": "fun privateFun(x: kotlin.Any?): kotlin.Unit", "name": "access$privateFun", "desc": "(Ljava/lang/Object;)V"},
{"visibility": "private", "declaration": "fun <get-privateVal>(): kotlin.Any?", "name": "access$getPrivateVal$p", "desc": "()Ljava/lang/Object;"},
{"visibility": "private", "declaration": "fun <get-privateVar>(): kotlin.Any?", "name": "access$getPrivateVar$p", "desc": "()Ljava/lang/Object;"},
{"visibility": "private", "declaration": "fun <set-privateVar>(<set-?>: kotlin.Any?): kotlin.Unit", "name": "access$setPrivateVar$p", "desc": "(Ljava/lang/Object;)V"},
{"visibility": "private", "declaration": "fun <get-privateConst>(): kotlin.Int", "name": "access$getPrivateConst$p", "desc": "()I"}
{"visibility": "private", "declaration": "fun <set-privateVar>(<set-?>: kotlin.Any?): kotlin.Unit", "name": "access$setPrivateVar$p", "desc": "(Ljava/lang/Object;)V"}
]
}
]