Skip to content

ByteStrings: cross-boundary swar - #3430

Draft
pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:swar2
Draft

ByteStrings: cross-boundary swar#3430
pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:swar2

Conversation

@pjfanning

@pjfanning pjfanning commented Aug 13, 2026

Copy link
Copy Markdown
Member

The ByteString2 class (2-fragment ByteString) had cross-boundary read methods that used 8 individual byteAt calls for Long reads. Each byteAt call has a branch (check which fragment). The optimization:

  • Long reads (BE/LE): Build two sub-long values from byteAtUnchecked calls (which skip the bounds check branch) and combine them with bitwise OR. This reduces the branch overhead from 8 per Long to 8 virtual calls (one per byte, no extra branching per call) plus pure arithmetic assembly.
  • Short/Int reads: Kept using byteAt via SWARUtil helper methods. With only 2-4 bytes needed, the branch overhead is minimal and not worth adding complexity.

Why not direct array access: The first/second fields are typed as ByteString (trait), and .bytes/.startIndex are private[pekko] on the concrete subclasses but not accessible from the nested ByteString2 class context. byteAtUnchecked provides the same bounds-check-skipping benefit through virtual dispatch.

@pjfanning
pjfanning marked this pull request as draft August 13, 2026 10:45
i += 1
}
v << ((8 - split) << 3)
}

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.

use a private method with @inline annotation?

@pjfanning pjfanning Aug 15, 2026

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.

the code is very similar but the split logic is different - it will be hard to move this code to share it without affecting performance.

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