Move some serialization helpers to metadata/metadata.jvm
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.metadata.jvm.serialization
|
||||
|
||||
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf
|
||||
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf.StringTableTypes.Record
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmNameResolver
|
||||
import org.jetbrains.kotlin.metadata.serialization.StringTable
|
||||
import java.io.OutputStream
|
||||
|
||||
// TODO: optimize by reordering records to minimize storage of 'range' fields
|
||||
open class JvmStringTable(nameResolver: JvmNameResolver? = null) : StringTable {
|
||||
val strings = ArrayList<String>()
|
||||
private val records = ArrayList<Record.Builder>()
|
||||
private val map = HashMap<String, Int>()
|
||||
private val localNames = LinkedHashSet<Int>()
|
||||
|
||||
init {
|
||||
if (nameResolver != null) {
|
||||
strings.addAll(nameResolver.strings)
|
||||
nameResolver.records.mapTo(records, JvmProtoBuf.StringTableTypes.Record::toBuilder)
|
||||
for (index in strings.indices) {
|
||||
map[nameResolver.getString(index)] = index
|
||||
}
|
||||
localNames.addAll(nameResolver.types.localNameList)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getStringIndex(string: String): Int =
|
||||
map.getOrPut(string) {
|
||||
strings.size.apply {
|
||||
strings.add(string)
|
||||
|
||||
val lastRecord = records.lastOrNull()
|
||||
if (lastRecord != null && lastRecord.isTrivial()) {
|
||||
lastRecord.range = lastRecord.range + 1
|
||||
}
|
||||
else records.add(Record.newBuilder())
|
||||
}
|
||||
}
|
||||
|
||||
private fun Record.Builder.isTrivial(): Boolean {
|
||||
return !hasPredefinedIndex() && !hasOperation() && substringIndexCount == 0 && replaceCharCount == 0
|
||||
}
|
||||
|
||||
// We use the following format to encode ClassId: "pkg/Outer.Inner".
|
||||
// It represents a unique name, but such names don't usually appear in the constant pool, so we're writing "Lpkg/Outer$Inner;"
|
||||
// instead and an instruction to drop the first and the last character in this string and replace all '$' with '.'.
|
||||
// This works most of the time, except in two rare cases:
|
||||
// - the name of the class or any of its outer classes contains dollars. In this case we're just storing the described
|
||||
// string literally: "pkg/Outer.Inner$with$dollars"
|
||||
// - the class is local or nested in local. In this case we're also storing the literal string, and also storing the fact that
|
||||
// this name represents a local class in a separate list
|
||||
override fun getQualifiedClassNameIndex(className: String, isLocal: Boolean): Int {
|
||||
map[className]?.let { recordedIndex ->
|
||||
// If we already recorded such string, we only return its index if it's local and our name is local
|
||||
// OR it's not local and our name is not local as well
|
||||
if (isLocal == (recordedIndex in localNames)) {
|
||||
return recordedIndex
|
||||
}
|
||||
}
|
||||
|
||||
val index = strings.size
|
||||
if (isLocal) {
|
||||
localNames.add(index)
|
||||
}
|
||||
|
||||
val record = Record.newBuilder()
|
||||
|
||||
// If the class is local or any of its outer class names contains '$', store a literal string
|
||||
if (isLocal || '$' in className) {
|
||||
strings.add(className)
|
||||
}
|
||||
else {
|
||||
val predefinedIndex = JvmNameResolver.getPredefinedStringIndex(className)
|
||||
if (predefinedIndex != null) {
|
||||
record.predefinedIndex = predefinedIndex
|
||||
// TODO: move all records with predefined names to the end and do not write associated strings for them (since they are ignored)
|
||||
strings.add("")
|
||||
}
|
||||
else {
|
||||
record.operation = Record.Operation.DESC_TO_CLASS_ID
|
||||
strings.add("L${className.replace('.', '$')};")
|
||||
}
|
||||
}
|
||||
|
||||
records.add(record)
|
||||
|
||||
map[className] = index
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
override fun serializeTo(output: OutputStream) {
|
||||
with(JvmProtoBuf.StringTableTypes.newBuilder()) {
|
||||
addAllRecord(records.map { it.build() })
|
||||
addAllLocalName(localNames)
|
||||
build().writeDelimitedTo(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.metadata.serialization
|
||||
|
||||
class Interner<T>(private val parent: Interner<T>? = null) {
|
||||
private val firstIndex: Int = parent?.run { interned.size + firstIndex } ?: 0
|
||||
private val interned = hashMapOf<T, Int>()
|
||||
|
||||
val allInternedObjects: List<T>
|
||||
get() = interned.keys.sortedBy(interned::get)
|
||||
|
||||
val isEmpty: Boolean
|
||||
get() = interned.isEmpty() && parent?.isEmpty != false
|
||||
|
||||
private fun find(obj: T): Int? {
|
||||
assert(parent == null || parent.interned.size + parent.firstIndex == firstIndex) {
|
||||
"Parent changed in parallel with child: indexes will be wrong"
|
||||
}
|
||||
return parent?.find(obj) ?: interned[obj]
|
||||
}
|
||||
|
||||
fun intern(obj: T): Int =
|
||||
find(obj) ?: (firstIndex + interned.size).also {
|
||||
interned[obj] = it
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("FINITE_BOUNDS_VIOLATION_IN_JAVA")
|
||||
package org.jetbrains.kotlin.metadata.serialization
|
||||
|
||||
import org.jetbrains.kotlin.metadata.ProtoBuf
|
||||
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
|
||||
|
||||
private class TableElementWrapper<Element : GeneratedMessageLite.Builder<*, Element>>(val builder: Element) {
|
||||
// If you'll try to optimize it using structured equals/hashCode, pay attention to extensions present in proto messages
|
||||
private val bytes: ByteArray = builder.build().toByteArray()
|
||||
private val hashCode: Int = bytes.contentHashCode()
|
||||
|
||||
override fun hashCode() = hashCode
|
||||
|
||||
override fun equals(other: Any?) = other is TableElementWrapper<*> && bytes.contentEquals(other.bytes)
|
||||
}
|
||||
|
||||
abstract class MutableTable<Element, Table, TableBuilder>
|
||||
where Element : GeneratedMessageLite.Builder<*, Element>,
|
||||
Table : GeneratedMessageLite,
|
||||
TableBuilder : GeneratedMessageLite.Builder<Table, TableBuilder> {
|
||||
|
||||
private val interner = Interner<TableElementWrapper<Element>>()
|
||||
|
||||
protected abstract fun createTableBuilder(): TableBuilder
|
||||
|
||||
protected abstract fun addElement(builder: TableBuilder, element: Element)
|
||||
|
||||
operator fun get(type: Element): Int =
|
||||
interner.intern(TableElementWrapper(type))
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun serialize(): Table? =
|
||||
if (interner.isEmpty) null
|
||||
else createTableBuilder().apply {
|
||||
for (obj in interner.allInternedObjects) {
|
||||
addElement(this, obj.builder)
|
||||
}
|
||||
}.build() as Table
|
||||
}
|
||||
|
||||
class MutableTypeTable : MutableTable<ProtoBuf.Type.Builder, ProtoBuf.TypeTable, ProtoBuf.TypeTable.Builder>() {
|
||||
override fun createTableBuilder(): ProtoBuf.TypeTable.Builder = ProtoBuf.TypeTable.newBuilder()
|
||||
|
||||
override fun addElement(builder: ProtoBuf.TypeTable.Builder, element: ProtoBuf.Type.Builder) {
|
||||
builder.addType(element)
|
||||
}
|
||||
}
|
||||
|
||||
class MutableVersionRequirementTable : MutableTable<ProtoBuf.VersionRequirement.Builder, ProtoBuf.VersionRequirementTable, ProtoBuf.VersionRequirementTable.Builder>() {
|
||||
override fun createTableBuilder(): ProtoBuf.VersionRequirementTable.Builder = ProtoBuf.VersionRequirementTable.newBuilder()
|
||||
|
||||
override fun addElement(builder: ProtoBuf.VersionRequirementTable.Builder, element: ProtoBuf.VersionRequirement.Builder) {
|
||||
builder.addRequirement(element)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.metadata.serialization
|
||||
|
||||
import java.io.OutputStream
|
||||
|
||||
interface StringTable {
|
||||
fun getStringIndex(string: String): Int
|
||||
|
||||
/**
|
||||
* @param className the fully qualified name of some class in the format: `org/foo/bar/Test.Inner`
|
||||
*/
|
||||
fun getQualifiedClassNameIndex(className: String, isLocal: Boolean): Int
|
||||
|
||||
fun serializeTo(output: OutputStream)
|
||||
}
|
||||
Reference in New Issue
Block a user