Can we have some heap segment allocation API in the future?

刘希晨 benrush0705 at gmail.com
Thu Mar 28 01:55:19 UTC 2024


Thanks for your answer!

I have several examples for using heap segments to replace native segments
usage:

1. For each web client thread (Could be platform thread or virtual thread),
calling connect() function would use a `sockaddr` struct, and that's all,
it's a quite small memory allocation, and non-blocking connect will be
returned immediately, in scenarios like this, I think it's better to not
use an Arena, but to use heap allocation, which is much easier and should
be taken well care of by the GC.

2. When we are sending some messages over the network, the message size are
not known before, so will filling the buffer, some array-expansion
mechanism like ArrayList would be needed, instead of calling realloc(), I
think heap allocation should be better, since GC is really good at doing
that.

3. Sometimes we have to aggregate many chunks of small memorysegment into
the final one for native function call, these small memory chunks doesn't
need to be native memory, and they are really short-lifecycled.

All the senarios requires no memset() at all, because developers do know
about which part of memory should be overwritten if we are using
MemorySegment for native functions call, and I have tested the performance
drop at zeroing out the memory when allocated, it's nearly half of the
total allocation time.

I have written a HeapArena class for Heap memory allocation and it works
well (by using new long[], which requires some waste of memory, I think
that's totally fine), it's just the zeroing issue that makes heap segment
allocation not that fast as predicted, and I currently can't do anything
about it.

Reusing heap segment would solve this problem, but as I listed, the use
case are usually arbitrary small allocations, where I definitely don't want
something like ThreadLocal cache to get involved.

So in my opinion, it's better if JDK could provide a mechanism, to allocate
a heap MemorySegment, with given byteSize and byteAlignment, without
zeroing, for these use cases.

Best regards!

Per-Ake Minborg <per-ake.minborg at oracle.com> 于2024年3月28日周四 01:50写道:

> 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/20240328/f7eebe81/attachment-0001.htm>


More information about the panama-dev mailing list