GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve Direct Buffer accesses via VarHandle by franz1981 · Pull Request #15504 · netty/netty

Conversation

@franz1981

@franz1981 franz1981 commented Jul 18, 2025

Copy link
Copy Markdown
Contributor

Motivation:

With Unsafe gone the existing Direct's ByteBuf have too much overhead compared to the unsafe versions

Modifications:

Removed redundant bound checks and byte swapping conversions by using VarHandle

Result:

Faster safe Direct ByteBuffers (Fixes #15503)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@franz1981

franz1981 commented Jul 18, 2025

Copy link
Copy Markdown
Contributor Author

This is blocked by #15486

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 18, 2025

Copy link
Copy Markdown
Contributor Author

@normanmaurer @chrisvest I see that adaptive is using mostly unpooled direct ByteBuf, whilst the "old" allocator PooledDirectByteBuf - which means that I should "fix" that one too - let me know wdyt?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

The improvements, so far (at b2824591d322770f81f633fff11eb65142de4628) are:

                                                     BEFORE         |       AFTER
Benchmark                                      Score   Error  Units | Score   Error  Units 
ByteBufAccessBenchmark.getBytes                7.020 ± 0.136  ns/op | 4.573 ± 0.135  ns/op
ByteBufAccessBenchmark.getBytesConstantOffset  5.856 ± 0.296  ns/op | 4.853 ± 0.029  ns/op
ByteBufAccessBenchmark.readBytes               6.708 ± 0.030  ns/op | 5.694 ± 0.097  ns/op
ByteBufAccessBenchmark.setBytes                9.082 ± 0.097  ns/op | 6.149 ± 0.076  ns/op
ByteBufAccessBenchmark.setBytesConstantOffset  7.143 ± 0.069  ns/op | 5.195 ± 0.340  ns/op

whilst the batchy numbers are still far behind Unsafe (reported in the related issue) because we now have an unrolling of 4 instead of 8.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

github-advanced-security[bot]
github-advanced-security AI found potential problems Jul 18, 2025
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java Outdated

Check failure

Code scanning / CodeQL

User-controlled data in numeric cast

This cast to a narrower type depends on a [user-provided value](1), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](2), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](3), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](4), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](5), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](6), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](7), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](8), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](9), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](10), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](11), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](12), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](13), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](14), potentially causing truncation. This cast to a narrower type depends on a [user-provided value](15), potentially causing truncation.
@franz1981
franz1981 requested review from chrisvest and normanmaurer July 18, 2025 13:00
@franz1981

franz1981 commented Jul 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@chrisvest
The funny thing is: if we have cleaners which wraps MemorySegment - and under the hood NIO Direct buffers uses such - maybe we could just do the same and re-use the one in the clearner, slicing it and using it instead of the NIO buffer.

I have to perform few benchmarks vs MemorySegment but I can see already that some of the benchmarks I did requires reading buffer -> segment + limit + capacity

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981 franz1981 mentioned this pull request Jul 18, 2025
Introduce VarHandle 9 stub #15486
Merged
@franz1981

franz1981 commented Jul 18, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@normanmaurer @chrisvest for this, do I need to care about the jemalloc pooled buffers as well?

Fyi I have tried before/after just with JDK 24 but it could be worthy to try with 11/17 as well - which meant doesn't have that good varHandle optimizations.
Right now this improve the adaptive case only and in theory I can push this beyond what it does by having a segment based buffer too

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Jul 18, 2025

Copy link
Copy Markdown
Member

do I need to care about the jemalloc pooled buffers as well?

I think it's fine to at least do them one at a time, in separate PRs. The pooled allocator will be used for cases where the adaptive allocator turns out to be a poor fit, and as an intermediate step in upgrading systems from 4.1 to 4.2.

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Jul 18, 2025

Copy link
Copy Markdown
Member

@franz1981 please rebase

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981
Improve Direct Buffer accesses via VarHandle
533cd3b
Motivation:

With Unsafe gone the existing Direct's ByteBuf have too much overhead compared to the unsafe versions

Modifications:

Removed redundant bound checks and byte swapping conversions by using VarHandle

Result:

Faster safe Direct ByteBuffers (Fixes netty#15503)
@franz1981
franz1981 force-pushed the 4.2_varhandle_improving_buffers branch from b282459 to 533cd3b Compare July 21, 2025 00:53
@franz1981
franz1981 marked this pull request as ready for review July 21, 2025 00:53
@franz1981

franz1981 commented Jul 21, 2025

Copy link
Copy Markdown
Contributor Author

Done ptal @normanmaurer

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

If this look good @chrisvest we can go for it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest
chrisvest approved these changes Aug 7, 2025
View reviewed changes
@chrisvest
chrisvest merged commit 15b8bbf into netty:4.2 Aug 7, 2025
@chrisvest

chrisvest commented Aug 7, 2025

Copy link
Copy Markdown
Member

Thanks, Franz!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yawkat yawkat mentioned this pull request Sep 13, 2025
After upgrading Netty to 4.2.4+, application is throwing java.lang.VerifyError on Android platform 8/9/10/11/12 #15654
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chrisvest chrisvest chrisvest approved these changes
@normanmaurer normanmaurer Awaiting requested review from normanmaurer

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Safe Direct Buffer accesses are not as fast as Unsafe ones

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@franz1981 @chrisvest @normanmaurer @github-advanced-security
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.