Can we have some heap segment allocation API in the future?
Per-Ake Minborg
per-ake.minborg at oracle.com
Wed Mar 27 17:50:08 UTC 2024
Hi and thank you for the encouragement!
I am a bit uncertain as to why you would like to generally work with heap segments if you do not simultaneously have access to the corresponding backing arrays?
1 and 2) What you could do is to implement your own HeapArena that allocates a larger segment up front and perhaps also support recycling of
memory. The latter feature will avoid zeroing out recycled memory portions but will come with additional security caveats as old content now can leak.
3) At the very end of Jorn's article, you can read more on VarHandles and invoke exact behavior. In short, this will not affect your performance in most cases as the C2 compiler will be able to see through the code. You can also experiment with setting the system property parameter "java.lang.invoke.VarHandle.VAR_HANDLE_SEGMENT_FORCE_EXACT" to "true" if you want to see what actually happens under your specific workloads.
Best, Per Minborg
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of 刘希晨 <benrush0705 at gmail.com>
Sent: Wednesday, March 27, 2024 5:47 PM
To: panama-dev at openjdk.org <panama-dev at openjdk.org>
Subject: Can we have some heap segment allocation API in the future?
Hi there, I am really happy that Panama-FFI finally released, congratulation!
I have been using Panama's features since JDK19, and when I am migrating to JDK22, I found that Heap segment could be passed to native functions if the call is really short, which is really helpful in lots of ways.
However, allocating heap segments are not so convenient as allocating native segments, it seems that we could only use MemorySegment.ofArray() to transform an array into a memorysegment.
Hence I got several question:
1. Could there be a HeapArena, which targets at allocating heap segments with given byteSize and byteAlignment, just like native segments? That would be pretty helpful in a lot of scenarios.
2. Could there be an API, for developers, to allocate a heap segment, with no zero-bytes filled, so that heap allocation would be much faster?
3. I noticed that, when calling MemorySegment.get() or MemorySegment.set() methods, it will use the underlying Varhandle to do the job, I read about Varhandle in https://jornvernee.github.io/methodhandles/2024/01/19/methodhandle-primer.html, and when I run the following code:
System.out.println(ValueLayout.JAVA_INT.varHandle().hasInvokeExactBehavior());
the result is false, so there is no InvokeExactBehaviour, which is not recommended. I thought this could be a mistake, or it's intended to handle the auto-boxing conversion, please take a look at this.
Best regards!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240327/b2352d2f/attachment.htm>
More information about the panama-dev
mailing list