Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8274592: Performance regression in upcalls
Reviewed-by: jvernee
  • Loading branch information
mcimadamore committed Sep 30, 2021
1 parent 4511190 commit 9cd966f
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 17 deletions.
Expand Up @@ -261,7 +261,7 @@ public void close() {
* Create a binding context from given native scope.
*/
public static Context ofBoundedAllocator(long size) {
ResourceScope scope = ResourceScope.newConfinedScope();
ResourceScope scope = ResourceScope.newConfinedScope(null);
return new Context(SegmentAllocator.arenaBounded(size, scope), scope);
}

Expand All @@ -283,7 +283,7 @@ public ResourceScope scope() {
* the context's allocator is accessed.
*/
public static Context ofScope() {
ResourceScope scope = ResourceScope.newConfinedScope();
ResourceScope scope = ResourceScope.newConfinedScope(null);
return new Context(null, scope) {
@Override
public SegmentAllocator allocator() { throw new UnsupportedOperationException(); }
Expand Down
Expand Up @@ -262,7 +262,7 @@ private MethodHandle specialize(MethodHandle leafHandle) {
*/
Object invokeMoves(long addr, Object[] args, Binding.VMStore[] argBindings, Binding.VMLoad[] returnBindings) {
MemorySegment stackArgsSeg = null;
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
try (ResourceScope scope = ResourceScope.newConfinedScope(null)) {
MemorySegment argBuffer = MemorySegment.allocateNative(layout.size, 64, scope);
if (stackArgsBytes > 0) {
stackArgsSeg = MemorySegment.allocateNative(stackArgsBytes, 8, scope);
Expand Down
Expand Up @@ -233,7 +233,7 @@ private Object read(Class<?> carrier, MemoryLayout layout, SegmentAllocator allo
}
case POINTER, INTEGER, FLOAT -> {
VarHandle reader = layout.varHandle();
try (ResourceScope localScope = ResourceScope.newConfinedScope()) {
try (ResourceScope localScope = ResourceScope.newConfinedScope(null)) {
MemorySegment slice = MemorySegment.ofAddressNative(stackPtr(), layout.byteSize(), localScope);
Object res = reader.get(slice);
postAlignStack(layout);
Expand Down
Expand Up @@ -128,7 +128,7 @@ public long mismatch_large_segment() {
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public long mismatch_large_segment_acquire() {
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
try (ResourceScope scope = ResourceScope.newConfinedScope(null)) {
scope.keepAlive(mismatchSegmentLarge1.scope());
return mismatchSegmentLarge1.mismatch(mismatchSegmentLarge2);
}
Expand Down
10 changes: 6 additions & 4 deletions test/micro/org/openjdk/bench/jdk/incubator/foreign/QSort.java
Expand Up @@ -56,13 +56,15 @@ public class QSort extends CLayouts {

static final CLinker abi = CLinker.systemCLinker();
static final MethodHandle clib_qsort;
static final MemoryAddress native_compar;
static final Addressable native_compar;
static final Addressable panama_upcall_compar;
static final long jni_upcall_compar;

static final int[] INPUT = { 5, 3, 2, 7, 8, 12, 1, 7 };
static final MemorySegment INPUT_SEGMENT;

static Addressable qsort_addr = abi.lookup("qsort").get();

static {
INPUT_SEGMENT = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(INPUT.length, JAVA_INT), ResourceScope.globalScope());
INPUT_SEGMENT.copyFrom(MemorySegment.ofArray(INPUT));
Expand All @@ -72,7 +74,7 @@ public class QSort extends CLayouts {

try {
clib_qsort = abi.downcallHandle(
abi.lookup("qsort").orElseThrow(),
qsort_addr,
FunctionDescriptor.ofVoid(C_POINTER, C_LONG_LONG, C_LONG_LONG, C_POINTER)
);
System.loadLibrary("QSort");
Expand Down Expand Up @@ -101,7 +103,7 @@ interface JNIComparator {

@Benchmark
public void native_qsort() throws Throwable {
clib_qsort.invokeExact(INPUT_SEGMENT.address(), (long) INPUT.length, JAVA_INT.byteSize(), native_compar);
clib_qsort.invokeExact((Addressable)INPUT_SEGMENT, (long) INPUT.length, JAVA_INT.byteSize(), native_compar);
}

@Benchmark
Expand All @@ -116,7 +118,7 @@ public void jni_upcall_qsort_naive() {

@Benchmark
public void panama_upcall_qsort() throws Throwable {
clib_qsort.invokeExact(INPUT_SEGMENT.address(), (long) INPUT.length, JAVA_INT.byteSize(), panama_upcall_compar);
clib_qsort.invokeExact((Addressable)INPUT_SEGMENT, (long) INPUT.length, JAVA_INT.byteSize(), panama_upcall_compar);
}

private static int getIntAbsolute(MemoryAddress addr) {
Expand Down
Expand Up @@ -104,29 +104,40 @@ public void tearDown() throws Throwable {

@Benchmark
public MemorySegment confined_close() {
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
try (ResourceScope scope = ResourceScope.newConfinedScope(null)) {
return MemorySegment.allocateNative(ALLOC_SIZE, 4, scope);
}
}

@Benchmark
public MemorySegment shared_close() {
try (ResourceScope scope = ResourceScope.newSharedScope()) {
try (ResourceScope scope = ResourceScope.newSharedScope(null)) {
return MemorySegment.allocateNative(ALLOC_SIZE, 4, scope);
}
}

@Benchmark
public MemorySegment implicit_close() {
public MemorySegment confined_implicit() {
return MemorySegment.allocateNative(ALLOC_SIZE, 4, ResourceScope.newConfinedScope());
}

@Benchmark
public MemorySegment implicit_close_systemgc() {
public MemorySegment shared_implicit() {
return MemorySegment.allocateNative(ALLOC_SIZE, 4, ResourceScope.newSharedScope());
}

@Benchmark
public MemorySegment confined_implicit_systemgc() {
if (gcCount++ == 0) System.gc(); // GC when we overflow
return MemorySegment.allocateNative(ALLOC_SIZE, 4, ResourceScope.newConfinedScope());
}

@Benchmark
public MemorySegment shared_implicit_systemgc() {
if (gcCount++ == 0) System.gc(); // GC when we overflow
return MemorySegment.allocateNative(ALLOC_SIZE, 4, ResourceScope.newSharedScope());
}

// keep
static byte gcCount = 0;
}
Expand Up @@ -72,7 +72,7 @@ public void ellipsis() throws Throwable {

@Benchmark
public void vaList() throws Throwable {
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
try (ResourceScope scope = ResourceScope.newConfinedScope(null)) {
jdk.incubator.foreign.VaList vaList = jdk.incubator.foreign.VaList.make(b ->
b.addVarg(C_INT, 1)
.addVarg(C_DOUBLE, 2D)
Expand Down
Expand Up @@ -66,7 +66,7 @@ public class PanamaPoint extends CLayouts implements AutoCloseable {
private final MemorySegment segment;

public PanamaPoint(int x, int y) {
this(MemorySegment.allocateNative(LAYOUT, ResourceScope.newConfinedScope()), x, y);
this(MemorySegment.allocateNative(LAYOUT, ResourceScope.newConfinedScope(null)), x, y);
}

public PanamaPoint(MemorySegment segment, int x, int y) {
Expand Down
Expand Up @@ -239,7 +239,7 @@ public void segmentImplicitScalar() {

@Benchmark
public void bufferSegmentConfined() {
try (final var scope = ResourceScope.newConfinedScope()) {
try (final var scope = ResourceScope.newConfinedScope(null)) {
final var srcBufferSegmentConfined = MemorySegment.ofAddressNative(srcAddress, size, scope).asByteBuffer();
final var dstBufferSegmentConfined = MemorySegment.ofAddressNative(dstAddress, size, scope).asByteBuffer();

Expand Down
Expand Up @@ -209,7 +209,7 @@ public void bufferSegmentImplicit() {

@Benchmark
public void bufferSegmentConfined() {
try (final var scope = ResourceScope.newConfinedScope()) {
try (final var scope = ResourceScope.newConfinedScope(null)) {
final var srcBufferSegmentConfined = MemorySegment.ofAddressNative(srcAddress, size, scope).asByteBuffer();
final var dstBufferSegmentConfined = MemorySegment.ofAddressNative(dstAddress, size, scope).asByteBuffer();

Expand Down

0 comments on commit 9cd966f

Please sign in to comment.