diff --git a/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_indexOf_Benchmark.scala b/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_indexOf_Benchmark.scala index 4eab26438e..1b6c6d2a81 100644 --- a/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_indexOf_Benchmark.scala +++ b/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_indexOf_Benchmark.scala @@ -24,6 +24,8 @@ class ByteString_indexOf_Benchmark { val bss = start ++ start ++ start ++ start ++ start ++ ByteString("xyz") val bs = bss.compact // compacted + val oByte = 'o'.toByte + val zByte = 'z'.toByte /* original @@ -46,10 +48,16 @@ class ByteString_indexOf_Benchmark { @Benchmark def bss_indexOf_from_far_index_case: Int = bss.indexOf('z', 109) + @Benchmark + def bss_indexOf_from_far_index_case_byte: Int = bss.indexOf(zByte, 109) + @Benchmark def bss_indexOf_from_best_case: Int = bss.indexOf('a', 0) @Benchmark - def bs1_indexOf_from: Int = bs.indexOf('รถ', 5) + def bs1_indexOf_from: Int = bs.indexOf('o', 5) + + @Benchmark + def bs1_indexOf_from_byte: Int = bs.indexOf(oByte, 5) } diff --git a/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_lastIndexOf_Benchmark.scala b/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_lastIndexOf_Benchmark.scala new file mode 100644 index 0000000000..b809c0bf5b --- /dev/null +++ b/bench-jmh/src/main/scala/org/apache/pekko/util/ByteString_lastIndexOf_Benchmark.scala @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * license agreements; and to You under the Apache License, version 2.0: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * This file is part of the Apache Pekko project, which was derived from Akka. + */ + +/* + * Copyright (C) 2014-2022 Lightbend Inc. + */ + +package org.apache.pekko.util + +import java.util.concurrent.TimeUnit + +import org.openjdk.jmh.annotations._ + +@State(Scope.Benchmark) +@Measurement(timeUnit = TimeUnit.MILLISECONDS) +class ByteString_lastIndexOf_Benchmark { + val start = ByteString("abcdefg") ++ ByteString("hijklmno") ++ ByteString("pqrstuv") + val bss = start ++ start ++ start ++ start ++ start ++ ByteString("xyz") + + val bs = bss.compact // compacted + val bsLen = bs.length + val aByte = 'a'.toByte + val oByte = 'o'.toByte + + @Benchmark + def bss_lastIndexOf_worst_case: Int = bss.lastIndexOf('a') + + @Benchmark + def bss_lastIndexOf_far_index_case: Int = bss.lastIndexOf('a', 10) + + @Benchmark + def bss_lastIndexOf_far_index_case_byte: Int = bss.lastIndexOf(aByte, 10) + + @Benchmark + def bss_lastIndexOf_best_case: Int = bss.lastIndexOf('z') + + @Benchmark + def bs1_lastIndexOf: Int = bs.lastIndexOf('o', bsLen - 5) + + @Benchmark + def bs1_lastIndexOf_byte: Int = bs.lastIndexOf(oByte, bsLen - 5) + +}