[foreign-memaccess+abi] RFR: 8314292: Improve performance of Arena::allocateFrom [v3]

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Aug 16 13:53:19 UTC 2023


On Tue, 15 Aug 2023 12:09:00 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> This patch improves the performance of allocation of a standard confined/shared arenas in two steps:
>> 
>> * first, it special cases the allocation methods in SegmentAllocator to detect the case where the SegmentAllocator implementation is the internal Arena implementation. In such case, all the `allocateFrom` methods attempt an allocation request which does not perform memory zeroing (as the contents are going to be overwritten anyway).
>> * second, it minimizes the overhead associated with reserving/unreserving memory. More specifically, it only calls Bits::reserveMemory/unreserveMemory when allocating from an automatic arena.
>> 
>> Implementation-wise, this is done by having an internal arena implementation class (`ArenaImpl`) which is the implementation returned by the various arena factories. This class will have methods to allocate zeroed memory and non-zeroed memory. In order to avoid duplication of the various allocation routines, I instead re-routed the SegmentAllocator methods to a private allocation implementation which sees if we're `ArenaImpl` and if so calls the implementation that has better knowledge. Alternatively I could have overridden all `allocateFrom` methods from `SegmentAllocator` but that would have required some duplication.
>> 
>> One caveat: a custom arena does NOT inherit this special behavior. That is, it is the responsibility of the custom arena to define "shortcut" for the `allocateFrom` methods. The only way to avoid that would be to have zeroing as an explicit boolean parameter in the allocation methods, but that's not very safe, as it is now up to client to decide if they want zeroing or not. That said, this is only an issue for custom arenas, and we can assume that a client that wants a specialized arena behavior can handle overriding a bunch of methods via delegation (in case they care).
>
> Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Address review comments

I've updated the patch. I've realized that all the indirection when calling the SegmentAllocator::allocate methods were costing us big time. For instance, calling `allocate(JAVA_INT, array)` calls `allocate(JAVA_INT, array.length)` which calls `allocate(MemoryLayout.sequenceLayout(JAVA_INT, array.length))` which finally calls `allocate(long, long)`. This indirection (esp. the layout allocation, ends up being expensive on the faster allocators such as PoolArena (which is now 2x faster!).

Now all the SegmentAllocator methods jump straight into `SegmentAllocator::allocate(long, long)` (I needed to change the various implSpec clauses for this). This simplifies the patch a bit, as `ArenaImpl` only needs two different allocation methods.

I have also rearranged `NativeMemorySegmentImpl.makeNativeSegment` so that there's no `shouldInit` flag. Instead, the regular allocation method in `ArenaImpl` takes care of zeroing memory (by calling MS::fill on the allocated segment). This seems to be enough to take care of some bimodality across benchmark runs.

I've also marked `VM::pageAlignDirectMemory` as stable, as that field is set to the value of some JDK property and never touched again (I'm not too sure whether this value, which was meant for byte buffer, should also be honored by the memory segment API, but for now we can leave it as is).

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

PR Comment: https://git.openjdk.org/panama-foreign/pull/855#issuecomment-1680641269


More information about the panama-dev mailing list