Skip to content

Make AdaptiveByteBuf.setBytes faster (#15736)#16048

Merged
normanmaurer merged 1 commit into
5.0from
adapt5
Dec 16, 2025
Merged

Make AdaptiveByteBuf.setBytes faster (#15736)#16048
normanmaurer merged 1 commit into
5.0from
adapt5

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

Motivation:
The setBytes method was getting a sliced nioBuffer of its source, which typically causes allocation. On Java 16 onwards, we can instead copy using an absolutely offsetted put method, and forego allocating a duplicate ByteBuffer instance that is otherwise needed for isolating the position field.

Modification:

  • Make use of the absolutely offsetted put method in setBytes, when its available.
  • Use the underlying ByteBuffer of the shared chunk where possible to avoid multiple bounds checks.
  • Add a benchmark targeting the setBytes method that takes a ByteBuf source.
  • Change a few benchmarks to use the default allocator when pooling is enabled.

Result:
Faster setBytes in certain cases.

Motivation:
The setBytes method was getting a sliced nioBuffer of its source, which
typically causes allocation. On Java 16 onwards, we can instead copy
using an absolutely offsetted `put` method, and forego allocating a
duplicate ByteBuffer instance that is otherwise needed for isolating the
position field.

Modification:
- Make use of the absolutely offsetted put method in setBytes, when its
available.
- Use the underlying ByteBuffer of the shared chunk where possible to
avoid multiple bounds checks.
- Add a benchmark targeting the setBytes method that takes a ByteBuf
source.
- Change a few benchmarks to use the default allocator when pooling is
enabled.

Result:
Faster setBytes in certain cases.
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Dec 16, 2025
@normanmaurer normanmaurer merged commit 5a8d7ce into 5.0 Dec 16, 2025
13 checks passed
@normanmaurer normanmaurer deleted the adapt5 branch December 16, 2025 10:59
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