Skip to content

Commit

Permalink
[Wasm] Serialize and deserialize strings with a reference cache
Browse files Browse the repository at this point in the history
  • Loading branch information
igoriakovlev authored and qodana-bot committed Sep 21, 2024
1 parent 8a32989 commit 9cd72bd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,33 +533,31 @@ class WasmDeserializer(inputStream: InputStream, private val skipLocalNames: Boo
}

private fun deserializeString(): String {
return withFlags {
if (it.consume()) {
val length = b.readUInt32().toInt()
val bytes = b.readBytes(length)
String(bytes)
} else {
val lengthBytes = b.readUInt32().toInt()
val bytes = b.readBytes(lengthBytes)
val length = lengthBytes / Char.SIZE_BYTES
val charArray = CharArray(length)
for (i in 0..<length) {
val hi = bytes[i * Char.SIZE_BYTES].toInt() and 0xFF
val lo = bytes[i * Char.SIZE_BYTES + 1].toInt() and 0xFF
val code = hi or (lo shl Byte.SIZE_BITS)
charArray[i] = code.toChar()
return deserializeReference {
withFlags {
if (it.consume()) {
val length = b.readUInt32().toInt()
val bytes = b.readBytes(length)
String(bytes)
} else {
val lengthBytes = b.readUInt32().toInt()
val bytes = b.readBytes(lengthBytes)
val length = lengthBytes / Char.SIZE_BYTES
val charArray = CharArray(length)
for (i in 0..<length) {
val hi = bytes[i * Char.SIZE_BYTES].toInt() and 0xFF
val lo = bytes[i * Char.SIZE_BYTES + 1].toInt() and 0xFF
val code = hi or (lo shl Byte.SIZE_BITS)
charArray[i] = code.toChar()
}
String(charArray)
}
String(charArray)
}
}
}

private fun skipString() {
withFlags {
it.consume()
val length = b.readUInt32().toInt()
b.skip(length)
}
skipInt()
}

private fun deserializeInt() = b.readUInt32().toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,22 +556,24 @@ class WasmSerializer(outputStream: OutputStream) {
}

private fun serialize(str: String) {
val chars = str.toCharArray()
if (chars.none { it.isSurrogate() }) {
withFlags(true) {
serialize(str.toByteArray())
}
} else {
val charsByteArray = ByteArray(chars.size * Char.SIZE_BYTES)
var index = 0
for (char in chars) {
val code = char.code
charsByteArray[index * Char.SIZE_BYTES] = (code and 0xFF).toByte()
charsByteArray[index * Char.SIZE_BYTES + 1] = (code ushr Byte.SIZE_BITS).toByte()
index++
}
withFlags(false) {
serialize(charsByteArray)
serializeAsReference(str) {
val chars = str.toCharArray()
if (chars.none { it.isSurrogate() }) {
withFlags(true) {
serialize(str.toByteArray())
}
} else {
val charsByteArray = ByteArray(chars.size * Char.SIZE_BYTES)
var index = 0
for (char in chars) {
val code = char.code
charsByteArray[index * Char.SIZE_BYTES] = (code and 0xFF).toByte()
charsByteArray[index * Char.SIZE_BYTES + 1] = (code ushr Byte.SIZE_BITS).toByte()
index++
}
withFlags(false) {
serialize(charsByteArray)
}
}
}
}
Expand Down

0 comments on commit 9cd72bd

Please sign in to comment.