Skip to content

direct buffer preference should not depend on unsafe presence#15381

Merged
chrisvest merged 1 commit intonetty:4.2from
derklaro:no-unsafe-enforcement-for-direct-buffers
Jun 20, 2025
Merged

direct buffer preference should not depend on unsafe presence#15381
chrisvest merged 1 commit intonetty:4.2from
derklaro:no-unsafe-enforcement-for-direct-buffers

Conversation

@derklaro
Copy link
Copy Markdown
Contributor

Motivation:
Currently, the preference if a direct buffer should be allocated by, for example, ByteBufAllocator.buffer() or ByteBufAllocator.ioBuffer() depends on the fact if unsafe is available. It seems like this was originally introduced as only with unsafe it was possible to get the cleaner of a ByteBuffer to clean it. However, with the changes introduced in #15231, it doesn't make sense anymore to depend on this as unsafe could be unavailable but MemorySegments will still be freeable.

Modification:
Switch the check for unsafe to a check that validates if the current cleaner implementation is able to free the allocated buffers (each implementation can do this except the no-op one). This ensures that direct buffers are actually preferred when they're backed by something cleanable and still unpreferred when unsafe is unavailable and buffers cannot be freed in a controlled way.

Result:
Direct buffers will be preferred when they're cleanable in a controlled way unrelated to the fact if unsafe is available.

@derklaro
Copy link
Copy Markdown
Contributor Author

I had a small conversation about this with @chrisvest yesterday on discord: https://canary.discord.com/channels/936310574684438528/936310574684438532/1385254901654749244

* applies for buffers allocated via {@link #allocateDirect(int)} and when using the {@code clean} method of the
* returned {@link CleanableDirectBuffer}.
*/
public static boolean canReliabilyFreeDirectBuffers() {
Copy link
Copy Markdown
Contributor

@franz1981 franz1981 Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be even boolean hasCleaner but not a strong preference

@chrisvest chrisvest merged commit 23d30c7 into netty:4.2 Jun 20, 2025
17 checks passed
@chrisvest chrisvest added this to the 4.2.3.Final milestone Jun 20, 2025
Copy link
Copy Markdown
Member

@normanmaurer normanmaurer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM … Thanks a lot!

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.

4 participants