-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add a benchmark on individual Unicode codepoints reading/writing #320
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ package kotlinx.io.benchmarks | |
import kotlinx.benchmark.* | ||
import kotlinx.io.* | ||
import kotlinx.io.bytestring.ByteString | ||
import kotlin.random.Random | ||
|
||
@State(Scope.Benchmark) | ||
abstract class BufferRWBenchmarkBase { | ||
|
@@ -415,3 +416,67 @@ open class IndexOfByteString { | |
@Benchmark | ||
fun benchmark() = buffer.indexOf(byteString) | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
open class Utf8CodePointsBenchmark : BufferRWBenchmarkBase() { | ||
private val codePointsCount = 128 | ||
|
||
// Encoding names follow naming from Utf8StringBenchmark | ||
@Param("ascii", "utf8", "sparse", "2bytes", "3bytes", "4bytes", "bad") | ||
var encoding: String = "ascii" | ||
|
||
override fun padding(): ByteArray { | ||
return ByteArray(minGap) { '.'.code.toByte() } | ||
} | ||
|
||
private val codePoints = IntArray(codePointsCount) | ||
private var codePointIdx = 0 | ||
|
||
@Setup | ||
fun fillCodePointsArray() { | ||
fun IntArray.fill(generator: () -> Int) { | ||
for (idx in this.indices) { | ||
this[idx] = generator() | ||
} | ||
} | ||
|
||
when (encoding) { | ||
"ascii" -> codePoints.fill { Random.nextInt(' '.code, '~'.code) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are codes less than 0x20 ignored?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a personal preference :) |
||
"utf8" -> codePoints.fill { | ||
var cp: Int | ||
do { | ||
cp = Random.nextInt(0, 0x10ffff) | ||
} while (cp in 0xd800 .. 0xdfff) | ||
cp | ||
} | ||
"sparse" -> { | ||
codePoints.fill { Random.nextInt(' '.code, '~'.code) } | ||
codePoints[42] = '⌛'.code | ||
} | ||
"2bytes" -> codePoints.fill { Random.nextInt(0x80, 0x800) } | ||
"3bytes" -> codePoints.fill { | ||
var cp: Int | ||
do { | ||
cp = Random.nextInt(0x800, 0x10000) | ||
} while (cp in 0xd800 .. 0xdfff) | ||
cp | ||
} | ||
"4bytes" -> codePoints.fill { Random.nextInt(0x10000, 0x10ffff) } | ||
"bad" -> codePoints.fill { Random.nextInt(0xd800, 0xdfff) } | ||
} | ||
} | ||
|
||
|
||
private fun nextCodePoint(): Int { | ||
val idx = codePointIdx | ||
val cp = codePoints[idx] | ||
codePointIdx = (idx + 1) % codePointsCount | ||
return cp | ||
} | ||
|
||
@Benchmark | ||
fun benchmark(): Int { | ||
buffer.writeCodePointValue(nextCodePoint()) | ||
return buffer.readCodePointValue() | ||
} | ||
} |
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 it's better to use enum in such cases?
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.
I decided to use the same values as for the old read/writeString benchmark. I can replace the parameter with enum, but then the old value need to be replaced (especially
2|3|4bytes
), and that would complicate results comparison against older releases.