-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Further improve stream decoding performance #2101
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
formats/json/jvmMain/src/kotlinx/serialization/json/internal/ByteArrayPool.kt
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
formats/json/jvmMain/src/kotlinx/serialization/json/internal/CharsetReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package kotlinx.serialization.json.internal | ||
|
||
import java.io.* | ||
import java.nio.* | ||
import java.nio.charset.* | ||
|
||
internal class CharsetReader( | ||
private val inputStream: InputStream, | ||
private val charset: Charset | ||
) { | ||
private val decoder: CharsetDecoder | ||
private val byteBuffer: ByteBuffer | ||
|
||
// Surrogate-handling in cases when a single char is requested, but two were read | ||
private var hasLeftoverPotentiallySurrogateChar = false | ||
private var leftoverChar = 0.toChar() | ||
|
||
init { | ||
decoder = charset.newDecoder() | ||
.onMalformedInput(CodingErrorAction.REPLACE) | ||
.onUnmappableCharacter(CodingErrorAction.REPLACE) | ||
byteBuffer = ByteBuffer.wrap(ByteArrayPool8k.take()) | ||
byteBuffer.flip() // Make empty | ||
} | ||
|
||
@Suppress("NAME_SHADOWING") | ||
fun read(array: CharArray, offset: Int, length: Int): Int { | ||
if (length == 0) return 0 | ||
require(offset in 0 until array.size && length >= 0 && offset + length <= array.size) { | ||
"Unexpected arguments: $offset, $length, ${array.size}" | ||
} | ||
|
||
var offset = offset | ||
var length = length | ||
var bytesRead = 0 | ||
if (hasLeftoverPotentiallySurrogateChar) { | ||
array[offset] = leftoverChar | ||
offset++ | ||
length-- | ||
hasLeftoverPotentiallySurrogateChar = false | ||
bytesRead = 1 | ||
if (length == 0) return bytesRead | ||
} | ||
if (length == 1) { | ||
// Treat single-character array reads just like read() | ||
val c = oneShotReadSlowPath() | ||
if (c == -1) return if (bytesRead == 0) -1 else bytesRead | ||
array[offset] = c.toChar() | ||
return bytesRead + 1 | ||
} | ||
return doRead(array, offset, length) + bytesRead | ||
} | ||
|
||
private fun doRead(array: CharArray, offset: Int, length: Int): Int { | ||
var charBuffer = CharBuffer.wrap(array, offset, length) | ||
if (charBuffer.position() != 0) { | ||
charBuffer = charBuffer.slice() | ||
} | ||
var isEof = false | ||
while (true) { | ||
val cr = decoder.decode(byteBuffer, charBuffer, isEof) | ||
if (cr.isUnderflow) { | ||
if (isEof) break | ||
if (!charBuffer.hasRemaining()) break | ||
val n = fillByteBuffer() | ||
if (n < 0) { | ||
isEof = true | ||
if (charBuffer.position() == 0 && !byteBuffer.hasRemaining()) break | ||
decoder.reset() | ||
} | ||
continue | ||
} | ||
if (cr.isOverflow) { | ||
assert(charBuffer.position() > 0) | ||
break | ||
} | ||
cr.throwException() | ||
} | ||
if (isEof) decoder.reset() | ||
return if (charBuffer.position() == 0) -1 | ||
else charBuffer.position() | ||
} | ||
|
||
private fun fillByteBuffer(): Int { | ||
byteBuffer.compact() | ||
try { | ||
// Read from the input stream, and then update the buffer | ||
val limit = byteBuffer.limit() | ||
val position = byteBuffer.position() | ||
val remaining = if (position <= limit) limit - position else 0 | ||
val bytesRead = inputStream.read(byteBuffer.array(), byteBuffer.arrayOffset() + position, remaining) | ||
if (bytesRead < 0) return bytesRead | ||
byteBuffer.position(position + bytesRead) | ||
} finally { | ||
byteBuffer.flip() | ||
} | ||
return byteBuffer.remaining() | ||
} | ||
|
||
private fun oneShotReadSlowPath(): Int { | ||
// Return the leftover char, if there is one | ||
if (hasLeftoverPotentiallySurrogateChar) { | ||
hasLeftoverPotentiallySurrogateChar = false | ||
return leftoverChar.code | ||
} | ||
|
||
val array = CharArray(2) | ||
qwwdfsad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val bytesRead = read(array, 0, 2) | ||
return when (bytesRead) { | ||
-1 -> -1 | ||
1 -> array[0].code | ||
2 -> { | ||
leftoverChar = array[1] | ||
hasLeftoverPotentiallySurrogateChar = true | ||
array[0].code | ||
} | ||
else -> error("Unreachable state: $bytesRead") | ||
} | ||
} | ||
|
||
public fun release() { | ||
ByteArrayPool8k.release(byteBuffer.array()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Rewrite this code to remove many synchronized calls and speed up code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also ByteArray, CharArray can replace with template T :)