Skip to content

Commit 37043b0

Browse files
committed
8257837: Performance regression in heap byte buffer views
Reviewed-by: chegar, roland
1 parent 0890620 commit 37043b0

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

src/hotspot/share/classfile/vmIntrinsics.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ class methodHandle;
495495
/* support for Unsafe */ \
496496
do_class(jdk_internal_misc_Unsafe, "jdk/internal/misc/Unsafe") \
497497
do_class(sun_misc_Unsafe, "sun/misc/Unsafe") \
498+
do_class(jdk_internal_misc_ScopedMemoryAccess, "jdk/internal/misc/ScopedMemoryAccess") \
498499
\
499500
do_intrinsic(_writeback0, jdk_internal_misc_Unsafe, writeback0_name, long_void_signature , F_RN) \
500501
do_name( writeback0_name, "writeback0") \

src/hotspot/share/oops/methodData.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,8 @@ bool MethodData::profile_unsafe(const methodHandle& m, int bci) {
15861586
Bytecode_invoke inv(m , bci);
15871587
if (inv.is_invokevirtual()) {
15881588
if (inv.klass() == vmSymbols::jdk_internal_misc_Unsafe() ||
1589-
inv.klass() == vmSymbols::sun_misc_Unsafe()) {
1589+
inv.klass() == vmSymbols::sun_misc_Unsafe() ||
1590+
inv.klass() == vmSymbols::jdk_internal_misc_ScopedMemoryAccess()) {
15901591
ResourceMark rm;
15911592
char* name = inv.name()->as_C_string();
15921593
if (!strncmp(name, "get", 3) || !strncmp(name, "put", 3)) {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package org.openjdk.bench.jdk.incubator.foreign;
26+
27+
28+
import org.openjdk.jmh.annotations.Benchmark;
29+
import org.openjdk.jmh.annotations.BenchmarkMode;
30+
import org.openjdk.jmh.annotations.Fork;
31+
import org.openjdk.jmh.annotations.Measurement;
32+
import org.openjdk.jmh.annotations.Mode;
33+
import org.openjdk.jmh.annotations.OutputTimeUnit;
34+
import org.openjdk.jmh.annotations.Setup;
35+
import org.openjdk.jmh.annotations.State;
36+
import org.openjdk.jmh.annotations.TearDown;
37+
import org.openjdk.jmh.annotations.Warmup;
38+
import sun.misc.Unsafe;
39+
40+
import java.nio.ByteBuffer;
41+
import java.nio.ByteOrder;
42+
import java.nio.FloatBuffer;
43+
import java.util.concurrent.TimeUnit;
44+
45+
import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
46+
47+
@BenchmarkMode(Mode.AverageTime)
48+
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
49+
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
50+
@State(org.openjdk.jmh.annotations.Scope.Thread)
51+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
52+
@Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
53+
public class LoopOverPollutedBuffer {
54+
55+
static final int ELEM_SIZE = 1_000_000;
56+
static final int CARRIER_SIZE = (int) JAVA_INT.byteSize();
57+
static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
58+
59+
static final Unsafe unsafe = Utils.unsafe;
60+
61+
ByteBuffer dbb = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
62+
byte[] arr = new byte[ALLOC_SIZE];
63+
ByteBuffer hbb = ByteBuffer.wrap(arr).order(ByteOrder.nativeOrder());
64+
FloatBuffer hfb = hbb.asFloatBuffer();
65+
66+
67+
@Setup
68+
public void setup() {
69+
for (int i = 0; i < ELEM_SIZE; i++) {
70+
dbb.putFloat(i * 4, i);
71+
hbb.putFloat(i * 4, i);
72+
}
73+
for (int i = 0; i < ELEM_SIZE; i++) {
74+
hfb.put(i, i);
75+
}
76+
}
77+
78+
@TearDown
79+
public void tearDown() {
80+
unsafe.invokeCleaner(dbb);
81+
arr = null;
82+
hbb = null;
83+
hfb = null;
84+
}
85+
86+
@Benchmark
87+
public int direct_byte_buffer_get_float() {
88+
int sum = 0;
89+
for (int k = 0; k < ELEM_SIZE; k++) {
90+
dbb.putFloat(k, (float)k + 1);
91+
float v = dbb.getFloat(k * 4);
92+
sum += (int)v;
93+
}
94+
return sum;
95+
}
96+
97+
@Benchmark
98+
public int heap_byte_buffer_get_int() {
99+
int sum = 0;
100+
for (int k = 0; k < ELEM_SIZE; k++) {
101+
hbb.putInt(k, k + 1);
102+
int v = hbb.getInt(k * 4);
103+
sum += v;
104+
}
105+
return sum;
106+
}
107+
108+
@Benchmark
109+
public int unsafe_get_float() {
110+
int sum = 0;
111+
for (int k = 0; k < ALLOC_SIZE; k += 4) {
112+
unsafe.putFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET, k + 1);
113+
float v = unsafe.getFloat(arr, k + Unsafe.ARRAY_BYTE_BASE_OFFSET);
114+
sum += (int)v;
115+
}
116+
return sum;
117+
}
118+
}

0 commit comments

Comments
 (0)