Optimize apply for fragmented ByteStrings (#31070) (#31075)

This commit is contained in:
Desmond Yeung 2022-01-24 14:53:42 -05:00 committed by GitHub
parent 160be0be93
commit f8a47eae3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 9 deletions

View file

@ -500,11 +500,13 @@ object ByteString {
if (0 <= idx && idx < length) {
var pos = 0
var seen = 0
while (idx >= seen + bytestrings(pos).length) {
seen += bytestrings(pos).length
var frag = bytestrings(pos)
while (idx >= seen + frag.length) {
seen += frag.length
pos += 1
frag = bytestrings(pos)
}
bytestrings(pos)(idx - seen)
frag(idx - seen)
} else throw new IndexOutOfBoundsException(idx.toString)
}

View file

@ -517,11 +517,13 @@ object ByteString {
if (0 <= idx && idx < length) {
var pos = 0
var seen = 0
while (idx >= seen + bytestrings(pos).length) {
seen += bytestrings(pos).length
var frag = bytestrings(pos)
while (idx >= seen + frag.length) {
seen += frag.length
pos += 1
frag = bytestrings(pos)
}
bytestrings(pos)(idx - seen)
frag(idx - seen)
} else throw new IndexOutOfBoundsException(idx.toString)
}

View file

@ -518,11 +518,13 @@ object ByteString {
if (0 <= idx && idx < length) {
var pos = 0
var seen = 0
while (idx >= seen + bytestrings(pos).length) {
seen += bytestrings(pos).length
var frag = bytestrings(pos)
while (idx >= seen + frag.length) {
seen += frag.length
pos += 1
frag = bytestrings(pos)
}
bytestrings(pos)(idx - seen)
frag(idx - seen)
} else throw new IndexOutOfBoundsException(idx.toString)
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (C) 2014-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.util
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import akka.util.ByteString.{ ByteString1, ByteStrings }
@State(Scope.Benchmark)
@Measurement(timeUnit = TimeUnit.MILLISECONDS)
class ByteString_apply_Benchmark {
val bss = ByteStrings(Vector.fill(1024)(ByteString1(Array(0.toByte))))
/*
akka-bench-jmh/jmh:run -f 1 -wi 3 -i 3 .*ByteString_apply_Benchmark.*
2.12 original
ByteString_apply_Benchmark.bss_apply_best_case thrpt 3 204261596.303 ± 94507102.894 ops/s
ByteString_apply_Benchmark.bss_apply_worst_case thrpt 3 170359.149 ± 102901.206 ops/s
2.12 optimized
ByteString_apply_Benchmark.bss_apply_best_case thrpt 3 206985005.270 ± 7855543.098 ops/s
ByteString_apply_Benchmark.bss_apply_worst_case thrpt 3 437929.845 ± 27264.190 ops/s
2.13 original
ByteString_apply_Benchmark.bss_apply_best_case thrpt 3 206854021.793 ± 81500220.451 ops/s
ByteString_apply_Benchmark.bss_apply_worst_case thrpt 3 237125.194 ± 128394.832 ops/s
2.13 optimized
ByteString_apply_Benchmark.bss_apply_best_case thrpt 3 209266780.913 ± 6821134.296 ops/s
ByteString_apply_Benchmark.bss_apply_worst_case thrpt 3 430348.094 ± 24412.915 ops/s
*/
@Benchmark
def bss_apply_best_case: Byte = bss(0)
@Benchmark
def bss_apply_worst_case: Byte = bss(1023)
}