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 — Faster setZero (Fixes #13692) by franz1981 · Pull Request #13693 · netty/netty

Conversation

@franz1981

@franz1981 franz1981 commented Nov 9, 2023

Copy link
Copy Markdown
Contributor

Motivation:

Unsafe's buffer setZero always piggyback to the JDK's Unsafe::setMemory,
which is not optimized for small writes, given that's not an intrinsic candidate
and requires handling thread-state transitions.

Modifications:

Handle small writes with a specialized code path which won't rely on Unsafe::setMemory

Result:

Faster setZero(...) and writeZero(...)

Sorry, something went wrong.

Uh oh!

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

All reactions
@franz1981 franz1981 added the improvement label Nov 9, 2023
@franz1981
franz1981 requested a review from normanmaurer November 9, 2023 16:16
@franz1981 franz1981 self-assigned this Nov 9, 2023
@franz1981

franz1981 commented Nov 9, 2023

Copy link
Copy Markdown
Contributor Author

@normanmaurer I'm adding 2 commits that @imangry could play and try with his own micro (fixed after my comments!)

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Nov 9, 2023

Copy link
Copy Markdown
Contributor Author

@normanmaurer I've changed my mind: let's see first if this PR already deliver what's promised and we can see if the check for when apply this optimization is still 64 or just 8; and eventually adding int/short handling OR just the int handling too

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981
franz1981 force-pushed the 4.1_setzero branch 2 times, most recently from 4a143fc to 523f6c7 Compare November 10, 2023 10:35
@franz1981
Faster setZero (Fixes netty#13692)
c09b4f2
Motivation:

Unsafe's buffer setZero always piggyback to the JDK's Unsafe::setMemory,
which is not optimized for small writes, given that's not an intrinsic candidate
and requires handling thread-state transitions.

Modifications:

Handle small writes with a specialized code path which won't rely on Unsafe::setMemory

Result:

Faster setZero(...) and writeZero(...)
@franz1981
franz1981 force-pushed the 4.1_setzero branch from 523f6c7 to c09b4f2 Compare November 10, 2023 12:05
@franz1981
franz1981 marked this pull request as ready for review November 10, 2023 12:07
@franz1981

franz1981 commented Nov 10, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

@normanmaurer and @imangry these are the results for the latest commit

SINGLE BYTES, DIRECT BUFFERS:

Benchmark                         (bytes)  (checkAccessible)  (checkBounds)  (direct)  (pooled)  (startOffset)  Mode  Cnt   Score   Error  Units
ByteBufZeroingBenchmark.setBytes        1              false          false      true      true              0  avgt   10   1.485 ± 0.002  ns/op
ByteBufZeroingBenchmark.setBytes        2              false          false      true      true              0  avgt   10   1.784 ± 0.008  ns/op
ByteBufZeroingBenchmark.setBytes        3              false          false      true      true              0  avgt   10   2.763 ± 0.070  ns/op
ByteBufZeroingBenchmark.setBytes        4              false          false      true      true              0  avgt   10   3.216 ± 0.007  ns/op
ByteBufZeroingBenchmark.setBytes        7              false          false      true      true              0  avgt   10   4.175 ± 0.045  ns/op
ByteBufZeroingBenchmark.setBytes        8              false          false      true      true              0  avgt   10   3.883 ± 0.041  ns/op
ByteBufZeroingBenchmark.setBytes       15              false          false      true      true              0  avgt   10   4.811 ± 0.095  ns/op
ByteBufZeroingBenchmark.setBytes       64              false          false      true      true              0  avgt   10   7.269 ± 0.021  ns/op
ByteBufZeroingBenchmark.setBytes       65              false          false      true      true              0  avgt   10   7.353 ± 0.135  ns/op
ByteBufZeroingBenchmark.setBytes     1024              false          false      true      true              0  avgt   10  28.634 ± 0.095  ns/op

BEFORE, DIRECT BUFFERS:

Benchmark                         (bytes)  (checkAccessible)  (checkBounds)  (direct)  (pooled)  (startOffset)  Mode  Cnt   Score   Error  Units
ByteBufZeroingBenchmark.setZero         1              false          false      true      true              0  avgt   10   8.023 ± 0.040  ns/op
ByteBufZeroingBenchmark.setZero         2              false          false      true      true              0  avgt   10   7.583 ± 0.007  ns/op
ByteBufZeroingBenchmark.setZero         3              false          false      true      true              0  avgt   10   8.027 ± 0.018  ns/op
ByteBufZeroingBenchmark.setZero         4              false          false      true      true              0  avgt   10   7.630 ± 0.078  ns/op
ByteBufZeroingBenchmark.setZero         7              false          false      true      true              0  avgt   10  10.780 ± 3.705  ns/op
ByteBufZeroingBenchmark.setZero         8              false          false      true      true              0  avgt   10   7.426 ± 0.056  ns/op
ByteBufZeroingBenchmark.setZero        15              false          false      true      true              0  avgt   10   8.292 ± 0.268  ns/op
ByteBufZeroingBenchmark.setZero        64              false          false      true      true              0  avgt   10   8.835 ± 0.605  ns/op
ByteBufZeroingBenchmark.setZero        65              false          false      true      true              0  avgt   10   8.869 ± 0.486  ns/op
ByteBufZeroingBenchmark.setZero      1024              false          false      true      true              0  avgt   10  22.767 ± 0.182  ns/op


AFTER, DIRECT BUFFERS:

Benchmark                        (bytes)  (checkAccessible)  (checkBounds)  (direct)  (pooled)  (startOffset)  Mode  Cnt   Score   Error  Units
ByteBufZeroingBenchmark.setZero        1              false          false      true      true              0  avgt   10   1.795 ± 0.023  ns/op
ByteBufZeroingBenchmark.setZero        2              false          false      true      true              0  avgt   10   2.013 ± 0.017  ns/op
ByteBufZeroingBenchmark.setZero        3              false          false      true      true              0  avgt   10   3.264 ± 0.047  ns/op
ByteBufZeroingBenchmark.setZero        4              false          false      true      true              0  avgt   10   3.345 ± 0.007  ns/op
ByteBufZeroingBenchmark.setZero        7              false          false      true      true              0  avgt   10   3.643 ± 0.027  ns/op
ByteBufZeroingBenchmark.setZero        8              false          false      true      true              0  avgt   10   1.821 ± 0.016  ns/op
ByteBufZeroingBenchmark.setZero       15              false          false      true      true              0  avgt   10   4.907 ± 0.007  ns/op
ByteBufZeroingBenchmark.setZero       64              false          false      true      true              0  avgt   10   3.755 ± 0.025  ns/op
ByteBufZeroingBenchmark.setZero       65              false          false      true      true              0  avgt   10   9.375 ± 1.166  ns/op
ByteBufZeroingBenchmark.setZero     1024              false          false      true      true              0  avgt   10  22.742 ± 0.014  ns/op

The improvement "seems" dramatic (but need to take in account that the accessed buffer's memory is always in L1 cache, and hot, which is not what always happen in real world!), for smaller write size as expected, which seems to be due to the presence of the Unsafe::setMemory AND memset calls for 1 byte writes too, which basically become a challenge between 2 not-inlined calls and a branch + put byte one, which explain why there's such a performance difference.
Reason for that is that Unsafe::setMemory isn't an intrinsic nor OpenJDK users are directly exposed on it (it's used on the DirectBuffer's constructor, which is not an hot spot!).

Last but not least; this is a microbenchmark trying to measure a very tiny effect: according to some of the reported numbers (eg ~1.4 ns, which is too near to the L1 cache access latency of my Ryzen box) it's likely we're affected by the JMH infra as well (although less by using compiler's black holes), meaning that some of the differences are not as meaningful as they could be given that they both includes the JMH infra.

The test has run on # VM version: JDK 17.0.7, Java HotSpot(TM) 64-Bit Server VM, 17.0.7+8-LTS-224
in order to benefit from lighter compiler's assisted black-holes.

The perfasm analysis shown the presence of the mentioned calls and the original issue solved here reported already the relevant JDK src code paths involved.

I've decided for 64 bytes as a cutoff value to honor AVX512, but probably even higher values could benefit from this (in short, I've been conservative with the original code). It's interesting to note it by checking the difference between 64 and 65 bytes results.

I didn't reported heap/unpooled data because not relevant, given that is 1:1 in term of perf increase.

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Nov 10, 2023

Copy link
Copy Markdown
Member

@franz1981 nice work!

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer
normanmaurer requested a review from chrisvest November 10, 2023 12:52
normanmaurer
normanmaurer approved these changes Nov 10, 2023
View reviewed changes
@normanmaurer
normanmaurer merged commit 5a58f04 into netty:4.1 Nov 13, 2023
@normanmaurer

normanmaurer commented Nov 13, 2023

Copy link
Copy Markdown
Member

@franz1981 can you also do a PR for main ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer normanmaurer added this to the 4.1.102.Final milestone Nov 13, 2023
@franz1981 franz1981 mentioned this pull request Nov 21, 2023
Draft: Improve Unsafe::setMemory for small writes openjdk/jdk#16760
Closed
3 tasks
@normanmaurer normanmaurer modified the milestones: 4.1.102.Final, 4.1.103.Final Dec 13, 2023
@franz1981 franz1981 mentioned this pull request Aug 30, 2024
8338967: Improve performance for MemorySegment::fill openjdk/jdk#20712
Closed
3 tasks done
franz1981 added a commit to franz1981/netty that referenced this pull request Oct 10, 2025
@franz1981
Fix aligned off-heap zeroing
dd208e1
Motivation:

netty#13693 introduced a buggy alignment rounding while zeroing off-heap memory on platforms which requires aligned accesses

Modifications:
Use a safer branchless alignment rounding to save down-casting and handle correctly the negative cases while fixing the original bug.

Result:
Faster alignment and no more assertion failures on aligned platforms
@franz1981 franz1981 mentioned this pull request Oct 10, 2025
Fix aligned off-heap zeroing #15740
Merged
chrisvest pushed a commit that referenced this pull request Oct 10, 2025
@franz1981
Fix aligned off-heap zeroing (#15740)
38a9b8d
Motivation:

#13693 introduced a buggy alignment rounding while zeroing off-heap
memory on platforms which requires aligned accesses

Modifications:
Use a safer branchless alignment rounding to save down-casting and
handle correctly the negative cases while fixing the original bug.

Result:
Faster alignment and no more assertion failures on aligned platforms
normanmaurer pushed a commit that referenced this pull request Oct 11, 2025
@franz1981 @normanmaurer
Fix aligned off-heap zeroing (#15740)
0899b6b
Motivation:

#13693 introduced a buggy alignment rounding while zeroing off-heap
memory on platforms which requires aligned accesses

Modifications:
Use a safer branchless alignment rounding to save down-casting and
handle correctly the negative cases while fixing the original bug.

Result:
Faster alignment and no more assertion failures on aligned platforms
@normanmaurer normanmaurer mentioned this pull request Oct 11, 2025
Fix aligned off-heap zeroing (#15740) #15744
Merged
normanmaurer added a commit that referenced this pull request Oct 11, 2025
@normanmaurer @franz1981
Fix aligned off-heap zeroing (#15740) (#15744)
206fffa
Motivation:

#13693 introduced a buggy alignment rounding while zeroing off-heap
memory on platforms which requires aligned accesses

Modifications:
Use a safer branchless alignment rounding to save down-casting and
handle correctly the negative cases while fixing the original bug.

Result:
Faster alignment and no more assertion failures on aligned platforms

Co-authored-by: Francesco Nigro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

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

Uh oh!

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

Assignees

@franz1981 franz1981

Labels

improvement

Projects

None yet

Milestone

4.1.103.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

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