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 — adapt mem5 by normanmaurer · Pull Request #17851 · netty/netty

Conversation

@normanmaurer

@normanmaurer normanmaurer commented Oct 16, 2026

Copy link
Copy Markdown
Member
  • Port 5.0: Keep reading a closed QUIC stream until the end of its input (Keep reading a closed QUIC stream until the end of its input #17755)
  • Make AdaptiveByteBuf.memoryAddress() faster (Make AdaptiveByteBuf.memoryAddress() faster #17767)

Sorry, something went wrong.

Uh oh!

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

All reactions
@lhotari @normanmaurer
Make AdaptiveByteBuf.memoryAddress() faster (#17767)
0679ea3
Motivation:

An `AdaptiveByteBuf` is a segment of a chunk's root buffer.
`AdaptiveByteBuf._memoryAddress()` computes its address by calling the
root buffer's `_memoryAddress()` and adding the segment's offset. On
every call, the root buffer's `_memoryAddress()`:

1. checks `isAccessible()`,
2. calls `hasMemoryAddress()`,
3. calls `memoryAddress()`, which checks the reference count again in
`ensureAccessible()`.

These checks on the root buffer are redundant. The root buffer stays
accessible while the `AdaptiveByteBuf` holds a segment of its chunk, and
the root buffer's address doesn't change.

A gathering write in the native transports gets the memory address of
each buffer that it writes (`IovArray.add()`). In a profile of an Apache
Pulsar broker writing 8 KB messages, the checks on the root buffer were
3.3 % of the I/O threads' CPU.

Modification:

Existing behavior that this relies on:

- The address of an `AdaptiveByteBuf` is the root buffer's address plus
the segment's offset in the chunk (`startIndex`).
- The allocator calls `AdaptiveByteBuf.init()` when it hands out a
buffer. When a buffer grows beyond its segment, the reallocation moves
it to a new segment and calls `init()` again.

Changes:

- `Chunk` computes the root buffer's memory address once, when the chunk
is created.
- `AdaptiveByteBuf.init()` stores the buffer's memory address, the root
buffer's address plus `startIndex`, in a new field. When the root buffer
has no memory address, it stores 0, as
`AbstractByteBuf._memoryAddress()` returns for buffers without one.
- `AdaptiveByteBuf._memoryAddress()` returns the field, instead of
calling the root buffer's `_memoryAddress()`. The public
`memoryAddress()` still checks that the `AdaptiveByteBuf` itself is
accessible, and, since #17820, throws `UnsupportedOperationException`
when it has no memory address.
- `deallocate()` resets the field, so `_memoryAddress()` still returns 0
after the buffer is deallocated.
- A new test in `AdaptiveByteBufAllocatorTest`,
`testDirectBufferMemoryAddressFollowsItsSegment`, allocates two direct
buffers, so that the second one is likely at an offset above zero in the
chunk. It compares each buffer's `memoryAddress()` with the address of
the NIO buffer of the same range, which is computed from the chunk's
buffer separately. It compares again after a buffer grows into another
segment, and checks that `_memoryAddress()` returns 0 after release.
`AbstractByteBufTest.testMemoryAddressUnsupportedWhenNoMemoryAddress`
(#17820) covers the heap buffers.
- A new benchmark, `AdaptiveByteBufMemoryAddressBenchmark`, gets the
memory addresses of 16 direct buffers, as a gathering write does.

Result:

Each `memoryAddress()` call checks a reference count once, the
`AdaptiveByteBuf`'s own, instead of three times. The root buffer's
address is computed once per chunk; allocating and releasing a buffer
each add one field write.

`AdaptiveByteBufMemoryAddressBenchmark` with 16 buffers of 8 KB, on an
Intel i9-9980HK at a fixed 2.4 GHz, with Unsafe enabled
(`-Dio.netty.noUnsafe=false`), 2 alternating runs per side. Before is
4.2 at 60d9baf with only the benchmark added:

| | Before | After |
|---|---:|---:|
| JDK 21, run 1, ops/s | 18,497,697 ± 74,560 | 30,904,677 ± 69,876 |
| JDK 21, run 2, ops/s | 18,514,434 ± 31,468 | 30,915,307 ± 74,873 |
| JDK 25, run 1, ops/s | 18,532,696 ± 50,826 | 30,851,633 ± 296,462 |
| JDK 25, run 2, ops/s | 18,558,233 ± 23,243 | 30,877,691 ± 173,624 |

That's 3.38 ns per buffer before and 2.02 ns after on both JDKs: 67 %
more throughput, with warm caches and a monomorphic call site.

<details>
<summary>JMH output</summary>

```
Benchmark                                            (buffers)  (size)   Mode  Cnt         Score       Error  Units

JDK 21 before:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  18497696.574 ± 74560.210  ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  18514434.341 ± 31467.599  ops/s

JDK 21 after:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  30904677.380 ± 69875.648  ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  30915306.886 ± 74873.233  ops/s

JDK 25 before:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  18532696.343 ± 50826.312  ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  18558232.849 ± 23243.203  ops/s

JDK 25 after:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  30851632.966 ± 296461.862  ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress         16    8192  thrpt   20  30877691.180 ± 173623.904  ops/s
```

</details>
@normanmaurer
normanmaurer force-pushed the adapt_mem5 branch from 7e4391c to 0679ea3 Compare October 16, 2026 06:06
@normanmaurer normanmaurer closed this Oct 16, 2026
@normanmaurer
normanmaurer deleted the adapt_mem5 branch October 16, 2026 06:06
@normanmaurer
normanmaurer restored the adapt_mem5 branch October 16, 2026 06:07
@normanmaurer
normanmaurer deleted the adapt_mem5 branch October 16, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

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.

Uh oh!

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

2 participants

@normanmaurer @lhotari
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.