RFR: 8338967: Improve performance for MemorySegment::fill [v10]

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Aug 30 14:14:21 UTC 2024


On Fri, 30 Aug 2024 10:51:59 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> The performance of the `MemorySegment::fil` can be improved by replacing the `checkAccess()` method call with calling `checkReadOnly()` instead (as the bounds of the segment itself do not need to be checked).
>> 
>> Also, smaller segments can be handled directly by Java code rather than transitioning to native code.
>> 
>> Here is how the `MemorySegment::fill` performance is improved by this PR:
>> 
>> ![image](https://github.com/user-attachments/assets/ee29fdf0-a7cf-4d5b-bb6b-278b01d97e3c)
>> 
>> Operations involving 8 or more bytes are delegated to native code whereas smaller segments are handled via a switch rake.
>> 
>> It should be noted that `Arena::allocate` is using `MemorySegment::fil`. Hence, this PR will also have a positive effect on memory allocation performance.
>
> Per Minborg has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Revert copyright year
>  - Move logic back to AMSI

src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java line 200:

> 198:         checkReadOnly(false);
> 199:         // 0...0X...XXXX implies: 0 <= length < FILL_NATIVE_LIMIT
> 200:         if ((length & -FILL_NATIVE_THRESHOLD) == 0) {

Looks great - few notes:
* adding `@ForceInline` here might help
* I'd suggest to move the `length == 0` outside in its own `if` branch (and drop the `return` from there). E.g.


if (length == 0) { ... }
else if (length & -FILL...) { ... }
else { ... }
return this;

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

PR Review Comment: https://git.openjdk.org/jdk/pull/20712#discussion_r1738755601


More information about the core-libs-dev mailing list