From 2c1fa5e965b37248e637a3ad9762eef6af59ca59 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Wed, 26 Apr 2017 18:29:48 +0300 Subject: [PATCH] Removed indices from the descriptor serializations. The reason they have been introduced initially was because those indices were calcuated irrelative to the descriptros themselves. The current scheme with hashing the mangled name allows to calcuate the indices for the descriptors, rather than storing them. The measurements didn't reveal any slowdowns because of hash recalculations, rather there may be even a minor improvement because of a 2% smaller volume of module protobuf. --- .../serialization/IrDescriptorDeserializer.kt | 16 +++++------- .../backend/konan/serialization/KonanIr.proto | 4 +++ .../konan/serialization/KonanLinkData.proto | 10 ------- .../serialization/KonanSerializerExtension.kt | 12 --------- .../konan/serialization/ProtobufUtil.kt | 26 ++----------------- 5 files changed, 13 insertions(+), 55 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt index f8435a16e90..44d1e6b4316 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt @@ -79,11 +79,11 @@ internal class IrDescriptorDeserializer(val context: Context, context.log{"### deserialized Kotlin Type index=$index, text=$text:\t$realType"} return realType } - fun deserializeLocalDeclaration(proto: KonanLinkData.Descriptor): DeclarationDescriptor { + fun deserializeLocalDeclaration(irProto: KonanIr.KotlinDescriptor, proto: KonanLinkData.Descriptor): DeclarationDescriptor { when { proto.hasFunction() -> { val functionProto = proto.function - val index = functionProto.functionIndex + val index = irProto.index val descriptor = localDeserializer.deserializeFunction(functionProto) descriptorIndex.put(index, descriptor) return descriptor @@ -91,7 +91,7 @@ internal class IrDescriptorDeserializer(val context: Context, proto.hasProperty() -> { val propertyProto = proto.property - val index = propertyProto.propertyIndex + val index = irProto.index val descriptor = localDeserializer.deserializeProperty(propertyProto) descriptorIndex.put(index, descriptor) return descriptor @@ -136,7 +136,7 @@ internal class IrDescriptorDeserializer(val context: Context, val descriptor = if (proto.hasIrLocalDeclaration()) { val realDescriptor = proto.irLocalDeclaration.descriptor - deserializeLocalDeclaration(realDescriptor) + deserializeLocalDeclaration(proto, realDescriptor) } else deserializeKnownDescriptor(proto) @@ -345,8 +345,7 @@ internal class IrDescriptorDeserializer(val context: Context, val originalIndex = descriptorProto.originalIndex val match = functions.singleOrNull() { - val proto = (it as DeserializedSimpleFunctionDescriptor).proto - proto.functionIndex == originalIndex + it.uniqId == originalIndex } as? DeserializedSimpleFunctionDescriptor if (match != null) return match @@ -370,8 +369,7 @@ internal class IrDescriptorDeserializer(val context: Context, val originalIndex = descriptorProto.originalIndex return constructors.single { - val proto = (it as DeserializedClassConstructorDescriptor).proto - proto.constructorIndex == originalIndex + it.uniqId == originalIndex } as DeserializedClassConstructorDescriptor } @@ -389,7 +387,7 @@ internal class IrDescriptorDeserializer(val context: Context, val originalIndex = proto.originalIndex return functions.single { val property = (it as PropertyAccessorDescriptor).correspondingProperty as DeserializedPropertyDescriptor - property.proto.propertyIndex == originalIndex + property.uniqId == originalIndex } as PropertyAccessorDescriptor } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto index 0eb16a132c3..4cb3eb2061d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIr.proto @@ -37,6 +37,10 @@ message KotlinDescriptor { optional LocalDeclaration ir_local_declaration = 12; } +message UniqId { + required int64 index = 1; +} + message LocalDeclaration { // This is Descriptor message from KonanLinkData required Descriptor descriptor = 1; diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanLinkData.proto b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanLinkData.proto index 1145838f9a4..a452ec23b7b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanLinkData.proto +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanLinkData.proto @@ -15,35 +15,27 @@ option optimize_for = LITE_RUNTIME; // Konan extensions to the "descriptors" protobuf. -message UniqId { - required int64 index = 1; -} - extend Package { optional int32 package_fq_name = 171; } extend Class { repeated Annotation class_annotation = 170; - optional UniqId class_index = 171; } extend Constructor { repeated Annotation constructor_annotation = 170; - optional UniqId constructor_index = 171; optional int32 constructor_parent = 172; } extend Function { repeated Annotation function_annotation = 170; - optional UniqId function_index = 171; optional int32 function_parent = 172; optional InlineIrBody inline_ir_body = 174; } extend Property { repeated Annotation property_annotation = 170; - optional UniqId property_index = 171; optional int32 property_parent = 172; optional bool used_as_variable = 173; optional Annotation.Argument.Value compile_time_value = 174; @@ -53,7 +45,6 @@ extend Property { extend EnumEntry { repeated Annotation enum_entry_annotation = 170; - optional UniqId enum_entry_index = 171; } extend ValueParameter { @@ -62,7 +53,6 @@ extend ValueParameter { extend Type { repeated Annotation type_annotation = 170; - optional UniqId type_index = 171; optional string type_text = 172; // TODO: remove me } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt index 639035e785d..e3e12b3e5b3 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanSerializerExtension.kt @@ -66,8 +66,6 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer override fun serializeConstructor(descriptor: ConstructorDescriptor, proto: ProtoBuf.Constructor.Builder) { - proto.setConstructorIndex( - inlineDescriptorTable.indexByValue(descriptor)) val parentIndex = descriptor.parentFqNameIndex() if (parentIndex != null) proto.setExtension(KonanLinkData.constructorParent, parentIndex) @@ -76,15 +74,11 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder) { - proto.setClassIndex( - inlineDescriptorTable.indexByValue(descriptor)) super.serializeClass(descriptor, proto) } override fun serializeFunction(descriptor: FunctionDescriptor, proto: ProtoBuf.Function.Builder) { - proto.setFunctionIndex( - inlineDescriptorTable.indexByValue(descriptor)) val parentIndex = descriptor.parentFqNameIndex() if (parentIndex != null) proto.setExtension(KonanLinkData.functionParent, parentIndex) super.serializeFunction(descriptor, proto) @@ -102,12 +96,6 @@ internal class KonanSerializerExtension(val context: Context, val util: KonanSer val variable = originalVariables[descriptor] if (variable != null) { proto.setExtension(KonanLinkData.usedAsVariable, true) - proto.setPropertyIndex( - inlineDescriptorTable.indexByValue(variable)) - - } else { - proto.setPropertyIndex( - inlineDescriptorTable.indexByValue(descriptor)) } super.serializeProperty(descriptor, proto) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt index 8963efd7943..5bed09ba876 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/ProtobufUtil.kt @@ -6,30 +6,8 @@ import org.jetbrains.kotlin.serialization.KonanLinkData.* import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.protobuf.GeneratedMessageLite -fun newUniqId(index: Long): KonanLinkData.UniqId = - KonanLinkData.UniqId.newBuilder().setIndex(index).build() - -val ProtoBuf.Function.functionIndex: Long - get() = this.getExtension(KonanLinkData.functionIndex).index - -val ProtoBuf.Constructor.constructorIndex: Long - get() = this.getExtension(KonanLinkData.constructorIndex).index - -val ProtoBuf.Property.propertyIndex: Long - get() = this.getExtension(KonanLinkData.propertyIndex).index - - -fun ProtoBuf.Function.Builder.setFunctionIndex(index: Long) - = this.setExtension(KonanLinkData.functionIndex, newUniqId(index)) - -fun ProtoBuf.Constructor.Builder.setConstructorIndex(index: Long) - = this.setExtension(KonanLinkData.constructorIndex, newUniqId(index)) - -fun ProtoBuf.Property.Builder.setPropertyIndex(index: Long) - = this.setExtension(KonanLinkData.propertyIndex, newUniqId(index)) - -fun ProtoBuf.Class.Builder.setClassIndex(index: Long) - = this.setExtension(KonanLinkData.classIndex, newUniqId(index)) +fun newUniqId(index: Long): KonanIr.UniqId = + KonanIr.UniqId.newBuilder().setIndex(index).build() // -----------------------------------------------------------