Conversation
pjfanning
commented
Apr 15, 2026
- small change from ByteString: extract shared private helpers to reduce code duplication #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
| } | ||
| -1 | ||
| elem match { | ||
| case byte: Byte => indexOf(byte, from) |
There was a problem hiding this comment.
Will this reduce the performance? as it add another instance Of and checkedcast call
There was a problem hiding this 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)'.
He-Pin
left a comment
There was a problem hiding this comment.
LGTM. The pattern match routing Byte inputs to the SWAR-optimized indexOf(byte, from) is a valid optimization, consistent with lastIndexOf's existing approach. The isInstanceOf overhead for non-Byte inputs is negligible compared to the 8x speedup for the common Byte case.