You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "pjfanning (via GitHub)" <gi...@apache.org> on 2026/04/15 09:17:17 UTC

[PR] small optimisation on indexOf [pekko]

pjfanning opened a new pull request, #2863:
URL: https://github.com/apache/pekko/pull/2863

   * small change from #2859
   * lastIndexOf already works this way
   * We have an overload for indexOf that accepts Byte input directly and the compiler will prefer this if you have a Byte that you want to match
   * but a number of Seq methods use indexOf under the hood and they will use the Seq API call so it is good to pattern match and forward Byte inputs to the dedicated overload method


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] small optimisation on indexOf [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #2863:
URL: https://github.com/apache/pekko/pull/2863#discussion_r3086059220


##########
actor/src/main/scala/org/apache/pekko/util/ByteString.scala:
##########
@@ -234,14 +234,18 @@ object ByteString {
       else toByteString1.drop(n)
 
     override def indexOf[B >: Byte](elem: B, from: Int): Int = {
-      if (from >= length) -1
-      else {
-        var i = math.max(from, 0)
-        while (i < length) {
-          if (bytes(i) == elem) return i
-          i += 1
-        }
-        -1
+      elem match {
+        case byte: Byte => indexOf(byte, from)

Review Comment:
   So if your code uses indexOf directly, the compiler will bind directly to `indexOf(byte, from)`.
   I don't think this will have a big slowdown for other use cases. We already have this code in lastIndexOf so in the end, we need to keep them consistent. That means adding this change or removing the code from lastIndexOf. In the end, it was Copilot/Claude that suggested this in #2859 and it documented it as 'improving performance for Byte inputs (avoids boxing)'.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] small optimisation on indexOf [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #2863:
URL: https://github.com/apache/pekko/pull/2863#discussion_r3085886064


##########
actor/src/main/scala/org/apache/pekko/util/ByteString.scala:
##########
@@ -234,14 +234,18 @@ object ByteString {
       else toByteString1.drop(n)
 
     override def indexOf[B >: Byte](elem: B, from: Int): Int = {
-      if (from >= length) -1
-      else {
-        var i = math.max(from, 0)
-        while (i < length) {
-          if (bytes(i) == elem) return i
-          i += 1
-        }
-        -1
+      elem match {
+        case byte: Byte => indexOf(byte, from)

Review Comment:
   Will this reduce the performance? as it add another instance Of  and checkedcast call



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] small optimisation on indexOf [pekko]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning merged PR #2863:
URL: https://github.com/apache/pekko/pull/2863


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org