direct buffer preference should not depend on unsafe presence#15381
Merged
direct buffer preference should not depend on unsafe presence#15381
Conversation
Contributor
Author
|
I had a small conversation about this with @chrisvest yesterday on discord: https://canary.discord.com/channels/936310574684438528/936310574684438532/1385254901654749244 |
franz1981
reviewed
Jun 20, 2025
| * applies for buffers allocated via {@link #allocateDirect(int)} and when using the {@code clean} method of the | ||
| * returned {@link CleanableDirectBuffer}. | ||
| */ | ||
| public static boolean canReliabilyFreeDirectBuffers() { |
Contributor
There was a problem hiding this comment.
it could be even boolean hasCleaner but not a strong preference
chrisvest
approved these changes
Jun 20, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation:
Currently, the preference if a direct buffer should be allocated by, for example,
ByteBufAllocator.buffer()orByteBufAllocator.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.