Skip to content

small optimisation on indexOf#2863

Merged
pjfanning merged 1 commit intoapache:mainfrom
pjfanning:indexOf
Apr 16, 2026
Merged

small optimisation on indexOf#2863
pjfanning merged 1 commit intoapache:mainfrom
pjfanning:indexOf

Conversation

@pjfanning
Copy link
Copy Markdown
Member

  • 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)'.

Copy link
Copy Markdown
Member

@He-Pin He-Pin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pjfanning pjfanning merged commit 8e6501b into apache:main Apr 16, 2026
9 checks passed
@pjfanning pjfanning deleted the indexOf branch April 16, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants