Optimize runtime representation for callable reference subclasses

Instead of generating overrides for getOwner/getName/getSignature in
each anonymous class representing a callable reference, pass them to the
superclass' constructor and store as fields. This occupies some small
memory but helps to reduce the size of the generated class files, and
will be helpful for adding further runtime information to callable
references, such as information about implicit conversions this
reference has been subject to.

Represent owner as java.lang.Class + boolean instead of
KDeclarationContainer, so that the unnecessary wrapping Class->KClass
wouldn't happen before it's needed, and also to make sure all callable
references remain serializable.

Note that the argument type where the "is declaration container a class"
is passed is int instead of boolean. The plan is to pass the
aforementioned implicit conversion information as bits of this same
integer value.

 #KT-27362 Fixed
This commit is contained in:
Alexander Udalov
2020-02-14 20:28:43 +01:00
parent 8d7c8672ac
commit 98aecbef6b
38 changed files with 502 additions and 262 deletions
@@ -5,12 +5,12 @@
package org.jetbrains.kotlin.resolve.jvm;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.PrimitiveType;
import org.jetbrains.org.objectweb.asm.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
public class AsmTypes {
private static final Map<Class<?>, Type> TYPES_MAP = new HashMap<>();
@@ -29,7 +29,10 @@ public class AsmTypes {
public static final Type UNIT_TYPE = Type.getObjectType("kotlin/Unit");
public static final Type LAMBDA = Type.getObjectType("kotlin/jvm/internal/Lambda");
public static final Type FUNCTION_REFERENCE = Type.getObjectType("kotlin/jvm/internal/FunctionReference");
public static final Type FUNCTION_REFERENCE_IMPL = Type.getObjectType("kotlin/jvm/internal/FunctionReferenceImpl");
public static final Type PROPERTY_REFERENCE0 = Type.getObjectType("kotlin/jvm/internal/PropertyReference0");
public static final Type PROPERTY_REFERENCE1 = Type.getObjectType("kotlin/jvm/internal/PropertyReference1");
public static final Type PROPERTY_REFERENCE2 = Type.getObjectType("kotlin/jvm/internal/PropertyReference2");
@@ -37,8 +40,6 @@ public class AsmTypes {
public static final Type MUTABLE_PROPERTY_REFERENCE1 = Type.getObjectType("kotlin/jvm/internal/MutablePropertyReference1");
public static final Type MUTABLE_PROPERTY_REFERENCE2 = Type.getObjectType("kotlin/jvm/internal/MutablePropertyReference2");
public static final Type RESULT_FAILURE = Type.getObjectType("kotlin/Result$Failure");
public static final Type FUNCTION0 = Type.getObjectType("kotlin/jvm/functions/Function0");
public static final Type FUNCTION1 = Type.getObjectType("kotlin/jvm/functions/Function1");
@@ -124,6 +125,16 @@ public class AsmTypes {
return TYPES_MAP.computeIfAbsent(javaClass, k -> Type.getType(javaClass));
}
public static final List<Type> OPTIMIZED_PROPERTY_REFERENCE_SUPERTYPES =
CollectionsKt.flatten(Arrays.asList(
Arrays.asList(PROPERTY_REFERENCE_IMPL),
Arrays.asList(MUTABLE_PROPERTY_REFERENCE_IMPL)
));
public static boolean isOptimizedPropertyReferenceSupertype(@NotNull Type type) {
return OPTIMIZED_PROPERTY_REFERENCE_SUPERTYPES.contains(type);
}
private AsmTypes() {
}
}