DFGSerializer: supported Java 9 compact strings
This commit is contained in:
+35
-19
@@ -20,8 +20,6 @@ import org.jetbrains.kotlin.backend.konan.Context
|
|||||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||||
import sun.misc.Unsafe
|
import sun.misc.Unsafe
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
import kotlin.reflect.KVisibility
|
|
||||||
import kotlin.reflect.full.memberProperties
|
|
||||||
import kotlin.reflect.full.primaryConstructor
|
import kotlin.reflect.full.primaryConstructor
|
||||||
import kotlin.reflect.jvm.jvmName
|
import kotlin.reflect.jvm.jvmName
|
||||||
|
|
||||||
@@ -70,6 +68,11 @@ internal object DFGSerializer {
|
|||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun writeCharUnsafe(value: Char) {
|
||||||
|
theUnsafe.putChar(array, byteArrayDataOffset + index, value)
|
||||||
|
index += 2
|
||||||
|
}
|
||||||
|
|
||||||
inline fun <T> writeNullable(value: T?, valueWriter: ArraySlice.(T) -> Unit) {
|
inline fun <T> writeNullable(value: T?, valueWriter: ArraySlice.(T) -> Unit) {
|
||||||
writeBoolean(value != null)
|
writeBoolean(value != null)
|
||||||
if (value != null)
|
if (value != null)
|
||||||
@@ -111,6 +114,14 @@ internal object DFGSerializer {
|
|||||||
|
|
||||||
//------------Write arrays------------------------------------------------------------------//
|
//------------Write arrays------------------------------------------------------------------//
|
||||||
|
|
||||||
|
fun writeCharArray(source: CharArray) {
|
||||||
|
writeInt(source.size)
|
||||||
|
val dataSize = source.size * 2
|
||||||
|
ensureSize(dataSize)
|
||||||
|
theUnsafe.copyMemory(source, charArrayDataOffset, array, byteArrayDataOffset + index, dataSize.toLong())
|
||||||
|
index += dataSize
|
||||||
|
}
|
||||||
|
|
||||||
fun writeIntArray(source: IntArray) {
|
fun writeIntArray(source: IntArray) {
|
||||||
writeInt(source.size)
|
writeInt(source.size)
|
||||||
val dataSize = source.size * 4
|
val dataSize = source.size * 4
|
||||||
@@ -120,12 +131,17 @@ internal object DFGSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun writeString(s: String) {
|
fun writeString(s: String) {
|
||||||
val value = theUnsafe.getObject(s, stringValueOffset) as CharArray
|
val value = theUnsafe.getObject(s, stringValueOffset)
|
||||||
writeInt(value.size)
|
val isCompactString = value is ByteArray
|
||||||
val dataSize = value.size * 2
|
if (!isCompactString)
|
||||||
ensureSize(dataSize)
|
writeCharArray(value as CharArray)
|
||||||
theUnsafe.copyMemory(value, charArrayDataOffset, array, byteArrayDataOffset + index, dataSize.toLong())
|
else {
|
||||||
index += dataSize
|
val size = s.length
|
||||||
|
writeInt(size)
|
||||||
|
ensureSize(size * 2)
|
||||||
|
for (i in 0 until size)
|
||||||
|
writeCharUnsafe(s[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T> writeArray(array: Array<T>, itemWriter: ArraySlice.(T) -> Unit) {
|
inline fun <reified T> writeArray(array: Array<T>, itemWriter: ArraySlice.(T) -> Unit) {
|
||||||
@@ -135,6 +151,16 @@ internal object DFGSerializer {
|
|||||||
|
|
||||||
//------------Read arrays------------------------------------------------------------------//
|
//------------Read arrays------------------------------------------------------------------//
|
||||||
|
|
||||||
|
fun readCharArray(): CharArray {
|
||||||
|
val size = readInt()
|
||||||
|
val result = CharArray(size)
|
||||||
|
val dataSize = size * 2
|
||||||
|
checkSize(dataSize)
|
||||||
|
theUnsafe.copyMemory(array, byteArrayDataOffset + index, result, charArrayDataOffset, dataSize.toLong())
|
||||||
|
index += dataSize
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
fun readIntArray(): IntArray {
|
fun readIntArray(): IntArray {
|
||||||
val size = readInt()
|
val size = readInt()
|
||||||
val result = IntArray(size)
|
val result = IntArray(size)
|
||||||
@@ -145,17 +171,7 @@ internal object DFGSerializer {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readString(): String {
|
fun readString() = String(readCharArray())
|
||||||
val size = readInt()
|
|
||||||
val data = CharArray(size)
|
|
||||||
val dataSize = size * 2
|
|
||||||
checkSize(dataSize)
|
|
||||||
theUnsafe.copyMemory(array, byteArrayDataOffset + index, data, charArrayDataOffset, dataSize.toLong())
|
|
||||||
index += dataSize
|
|
||||||
val str = theUnsafe.allocateInstance(String::class.java) as String
|
|
||||||
theUnsafe.putObject(str, stringValueOffset, data)
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <reified T> readArray(itemReader: ArraySlice.() -> T) =
|
inline fun <reified T> readArray(itemReader: ArraySlice.() -> T) =
|
||||||
Array(readInt()) { this.itemReader() }
|
Array(readInt()) { this.itemReader() }
|
||||||
|
|||||||
Reference in New Issue
Block a user