Skip to content
This repository has been archived by the owner. It is now read-only.

8279930: Synthetic cast causes generation of store barriers when using heap segments #97

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -266,7 +266,7 @@ public sealed interface MemorySegment extends Addressable permits AbstractMemory
* <p>
* The returned spliterator splits this segment according to the specified element layout; that is,
* if the supplied layout has size N, then calling {@link Spliterator#trySplit()} will result in a spliterator serving
* approximately {@code S/N/2} elements (depending on whether N is even or not), where {@code S} is the size of
* approximately {@code S/N} elements (depending on whether N is even or not), where {@code S} is the size of
* this segment. As such, splitting is possible as long as {@code S/N >= 2}. The spliterator returns segments that
* are associated with the same scope as this segment.
* <p>
@@ -36,13 +36,17 @@
import java.util.Objects;

/**
* Implementation for heap memory segments. An heap memory segment is composed by an offset and
* Implementation for heap memory segments. A heap memory segment is composed by an offset and
* a base object (typically an array). To enhance performances, the access to the base object needs to feature
* sharp type information, as well as sharp null-check information. For this reason, many concrete subclasses
* of {@link HeapMemorySegmentImpl} are defined (e.g. {@link OfFloat}, so that each subclass can override the
* {@link HeapMemorySegmentImpl#base()} method so that it returns an array of the correct (sharp) type.
* {@link HeapMemorySegmentImpl#base()} method so that it returns an array of the correct (sharp) type. Note that
* the field type storing the 'base' coordinate is just Object; similarly, all the constructor in the subclasses
* accept an Object 'base' parameter instead of a sharper type (e.g. {@code byte[]}). This is deliberate, as
* using sharper types would require use of type-conversions, which in turn would inhibit some C2 optimizations,
* such as the elimination of store barriers in methods like {@link HeapMemorySegmentImpl#dup(long, long, int, ResourceScopeImpl)}.
*/
public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl {
public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl {

private static final Unsafe UNSAFE = Unsafe.getUnsafe();
private static final int BYTE_ARR_BASE = UNSAFE.arrayBaseOffset(byte[].class);
@@ -53,25 +57,25 @@
private static final long MAX_ALIGN_8 = 8;

final long offset;
final H base;
final Object base;

@ForceInline
HeapMemorySegmentImpl(long offset, H base, long length, int mask) {
HeapMemorySegmentImpl(long offset, Object base, long length, int mask) {
super(length, mask, ResourceScopeImpl.GLOBAL);
this.offset = offset;
this.base = base;
}

@Override
abstract H base();
abstract Object base();

@Override
long min() {
return offset;
}

@Override
abstract HeapMemorySegmentImpl<H> dup(long offset, long size, int mask, ResourceScopeImpl scope);
abstract HeapMemorySegmentImpl dup(long offset, long size, int mask, ResourceScopeImpl scope);

@Override
ByteBuffer makeByteBuffer() {
@@ -84,9 +88,9 @@ ByteBuffer makeByteBuffer() {

// factories

public static class OfByte extends HeapMemorySegmentImpl<byte[]> {
public static class OfByte extends HeapMemorySegmentImpl {

OfByte(long offset, byte[] base, long length, int mask) {
OfByte(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -97,7 +101,7 @@ OfByte dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
byte[] base() {
return Objects.requireNonNull(base);
return (byte[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(byte[] arr) {
@@ -112,9 +116,9 @@ public long maxAlignMask() {
}
}

public static class OfChar extends HeapMemorySegmentImpl<char[]> {
public static class OfChar extends HeapMemorySegmentImpl {

OfChar(long offset, char[] base, long length, int mask) {
OfChar(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -125,7 +129,7 @@ OfChar dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
char[] base() {
return Objects.requireNonNull(base);
return (char[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(char[] arr) {
@@ -140,9 +144,9 @@ public long maxAlignMask() {
}
}

public static class OfShort extends HeapMemorySegmentImpl<short[]> {
public static class OfShort extends HeapMemorySegmentImpl {

OfShort(long offset, short[] base, long length, int mask) {
OfShort(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -153,7 +157,7 @@ OfShort dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
short[] base() {
return Objects.requireNonNull(base);
return (short[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(short[] arr) {
@@ -168,9 +172,9 @@ public long maxAlignMask() {
}
}

public static class OfInt extends HeapMemorySegmentImpl<int[]> {
public static class OfInt extends HeapMemorySegmentImpl {

OfInt(long offset, int[] base, long length, int mask) {
OfInt(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -181,7 +185,7 @@ OfInt dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
int[] base() {
return Objects.requireNonNull(base);
return (int[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(int[] arr) {
@@ -196,9 +200,9 @@ public long maxAlignMask() {
}
}

public static class OfLong extends HeapMemorySegmentImpl<long[]> {
public static class OfLong extends HeapMemorySegmentImpl {

OfLong(long offset, long[] base, long length, int mask) {
OfLong(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -209,7 +213,7 @@ OfLong dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
long[] base() {
return Objects.requireNonNull(base);
return (long[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(long[] arr) {
@@ -224,9 +228,9 @@ public long maxAlignMask() {
}
}

public static class OfFloat extends HeapMemorySegmentImpl<float[]> {
public static class OfFloat extends HeapMemorySegmentImpl {

OfFloat(long offset, float[] base, long length, int mask) {
OfFloat(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -237,7 +241,7 @@ OfFloat dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
float[] base() {
return Objects.requireNonNull(base);
return (float[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(float[] arr) {
@@ -252,9 +256,9 @@ public long maxAlignMask() {
}
}

public static class OfDouble extends HeapMemorySegmentImpl<double[]> {
public static class OfDouble extends HeapMemorySegmentImpl {

OfDouble(long offset, double[] base, long length, int mask) {
OfDouble(long offset, Object base, long length, int mask) {
super(offset, base, length, mask);
}

@@ -265,7 +269,7 @@ OfDouble dup(long offset, long size, int mask, ResourceScopeImpl scope) {

@Override
double[] base() {
return Objects.requireNonNull(base);
return (double[])Objects.requireNonNull(base);
}

public static MemorySegment fromArray(double[] arr) {