[code-reflection] RFR: Use memory segment

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Feb 26 12:48:11 UTC 2025


On Wed, 26 Feb 2025 12:31:22 GMT, Adam Sotona <asotona at openjdk.org> wrote:

>> cr-examples/onnx/src/main/java/oracle/code/onnx/OnnxProtoBuilder.java line 320:
>> 
>>> 318:                 .buf.toByteArray();
>>> 319: //        OnnxProtoPrinter.printModel(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN));
>>> 320:         return Arena.ofAuto().allocate(bytes.length).copyFrom(MemorySegment.ofArray(bytes)).asReadOnly();
>> 
>> I suggest using `allocateFrom(JAVA_BYTE, bytes)` to avoid the zeroing
>
> allocateFrom creates on-heap memory segment

No - `allocateFrom` creates a new off-heap segment when invoked on an `Arena`. `Arena` is a segment allocator that always allocates off-heap. The `allocateFrom` functions are explicitly designed for use cases when you want to allocate and copy existing data on top of the new allocated segment -- in which case the FFM knows that zeroing the allocated segment is redundant. I think that should be used here.

-------------

PR Review Comment: https://git.openjdk.org/babylon/pull/328#discussion_r1971524785


More information about the babylon-dev mailing list