RFR: 8277924: Small tweaks to foreign function and memory API
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Mon Nov 29 18:22:27 UTC 2021
On Mon, 29 Nov 2021 11:22:45 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> Following integration of the second incubator of the foreign function and memory API [1], we detected few divergences between the contents of the jdk repo and the panama repo:
>
> * the name of some of the `FunctionDescriptor` wither methods is different (e.g. `withAppendedLayoutArguments` vs. `appendLayoutArguments`), as it has been simplified and improved following a change that was not incorporated in [1].
>
> * TestUpcall does not execute all the test combinations, because of an issue in the jtreg header (also fixed in the panama repo)
>
> * Addressing some feedback, we would like to bring back alignment to JAVA_INT layout constants (and related constants).
>
> Javadoc: http://cr.openjdk.java.net/~mcimadamore/8277924/v1/javadoc/jdk/incubator/foreign/package-summary.html
> Specdiff: http://cr.openjdk.java.net/~mcimadamore/8277924/v1/spec_diff/overview-summary.html
>
> [1] - #5907
src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java line 1337:
> 1335: */
> 1336: @ForceInline
> 1337: default char getAtIndex(ValueLayout.OfChar layout, long index) {
We need to thread lightly here. The alignment check is not free, performance-wise, because of [1]. So, if we can detect that the var handle will always be accessed at offsets that are multiple of the alignment, we can use the more optimized "aligned" var handle, which will skip the alignment check. Eventually this special casing will go away when [1] is fixed.
[1] - https://bugs.openjdk.java.net/browse/JDK-8277850
src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/MemoryAddressImpl.java line 273:
> 271: public char getAtIndex(ValueLayout.OfChar layout, long index) {
> 272: Reflection.ensureNativeAccess(Reflection.getCallerClass());
> 273: if (layout.byteAlignment() <= layout.byteSize()) {
Here we also have to workaround [1]. Since we don't want to slice the everything segment (as that would create an additional instance), the only option is to use the var handle directly, but to use the "aligned" version if we detect that the offset is always aligned. One caveat is that we need to manually check the base address for alignment, since this check will be omitted otherwise. Again, this logic will go away when [1] is fixed.
[1] - https://bugs.openjdk.java.net/browse/JDK-8277850
-------------
PR: https://git.openjdk.java.net/jdk/pull/6589
More information about the core-libs-dev
mailing list