[foreign-preview] RFR: 8282070: Drop workaround from memory segment implementation
Maurizio Cimadamore
mcimadamore at openjdk.java.net
Thu Feb 17 19:18:36 UTC 2022
On Thu, 17 Feb 2022 19:07:12 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
> Following some preliminary experiments (see [1]) it is now finally time to drop the various workarounds we have in the memory segment implementation. This patch removes two workarounds:
>
> * special treatment for "small" segments (e.g. segments whose size fits in an int) - see `MemorySegment::isSmall`; these segments were special cased so that bound checks would only involve `int` comparisons. This has now been fixed in JDK-8276116, and special treatment is no longer required.
>
> * special shortcuts to avoid extra alignment checks. Since when generating var handles from a layout we check most of the alignment constraints (except those involving the base address), in the past we have added a fastpath for these var handles, as C2 had trouble hoisting alignment checks outside of the loops. This has now been fixed in JDK-8277850 and is no longer required.
>
> I have run benchmarks before and after and got identical results. Some benchmarkks in `ParallelSum` got a boost, but those always have noise, due to multi-threading.
src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java line 392:
> 390: @ForceInline
> 391: void checkBounds(long offset, long length) {
> 392: if (length > 0) {
This is the best I could do, given that `Objects.checkIndex` implements a <= semantics on the upper bound. That is, we need to protect the code from overflow, which can happen if `length == 0` (which can happen in slicing). When that happens, the upper bound in the second check `this.length - length + 1` ends up being `this.length + 1`, which is problematic. I've also tested to see if we could just use the old check bounds routine, but it seems that using `Objects.checkIndex` is required to properly apply BCE (I could see differences in the benchmark results).
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/644
More information about the panama-dev
mailing list