Skip to content

Commit 8e6501b

Browse files
authored
small optimisation on indexOf (#2863)
1 parent be3a411 commit 8e6501b

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

actor/src/main/scala/org/apache/pekko/util/ByteString.scala

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,18 @@ object ByteString {
234234
else toByteString1.drop(n)
235235

236236
override def indexOf[B >: Byte](elem: B, from: Int): Int = {
237-
if (from >= length) -1
238-
else {
239-
var i = math.max(from, 0)
240-
while (i < length) {
241-
if (bytes(i) == elem) return i
242-
i += 1
243-
}
244-
-1
237+
elem match {
238+
case byte: Byte => indexOf(byte, from)
239+
case _ =>
240+
if (from >= length) -1
241+
else {
242+
var i = math.max(from, 0)
243+
while (i < length) {
244+
if (bytes(i) == elem) return i
245+
i += 1
246+
}
247+
-1
248+
}
245249
}
246250
}
247251

@@ -562,14 +566,18 @@ object ByteString {
562566
}
563567

564568
override def indexOf[B >: Byte](elem: B, from: Int): Int = {
565-
if (from >= length) -1
566-
else {
567-
var i = math.max(from, 0)
568-
while (i < length) {
569-
if (bytes(startIndex + i) == elem) return i
570-
i += 1
571-
}
572-
-1
569+
elem match {
570+
case byte: Byte => indexOf(byte, from)
571+
case _ =>
572+
if (from >= length) -1
573+
else {
574+
var i = math.max(from, 0)
575+
while (i < length) {
576+
if (bytes(startIndex + i) == elem) return i
577+
i += 1
578+
}
579+
-1
580+
}
573581
}
574582
}
575583

0 commit comments

Comments
 (0)