Skip to content

Commit 7a418fc

Browse files
committedSep 3, 2024
8338967: Improve performance for MemorySegment::fill
Reviewed-by: mcimadamore, psandoz
1 parent 633fad8 commit 7a418fc

File tree

3 files changed

+277
-3
lines changed

3 files changed

+277
-3
lines changed
 

‎src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import jdk.internal.misc.ScopedMemoryAccess;
5252
import jdk.internal.reflect.CallerSensitive;
5353
import jdk.internal.reflect.Reflection;
54+
import jdk.internal.util.Architecture;
5455
import jdk.internal.util.ArraysSupport;
5556
import jdk.internal.util.Preconditions;
5657
import jdk.internal.vm.annotation.ForceInline;
@@ -188,10 +189,52 @@ public Stream<MemorySegment> elements(MemoryLayout elementLayout) {
188189
return StreamSupport.stream(spliterator(elementLayout), false);
189190
}
190191

192+
// FILL_NATIVE_THRESHOLD must be a power of two and should be greater than 2^3
193+
// Update the value for Aarch64 once 8338975 is fixed.
194+
private static final long FILL_NATIVE_THRESHOLD = 1L << (Architecture.isAARCH64() ? 10 : 5);
195+
191196
@Override
192-
public final MemorySegment fill(byte value){
193-
checkAccess(0, length, false);
194-
SCOPED_MEMORY_ACCESS.setMemory(sessionImpl(), unsafeGetBase(), unsafeGetOffset(), length, value);
197+
@ForceInline
198+
public final MemorySegment fill(byte value) {
199+
checkReadOnly(false);
200+
if (length == 0) {
201+
// Implicit state check
202+
checkValidState();
203+
} else if (length < FILL_NATIVE_THRESHOLD) {
204+
// 0 <= length < FILL_NATIVE_LIMIT : 0...0X...XXXX
205+
206+
// Handle smaller segments directly without transitioning to native code
207+
final long u = Byte.toUnsignedLong(value);
208+
final long longValue = u << 56 | u << 48 | u << 40 | u << 32 | u << 24 | u << 16 | u << 8 | u;
209+
210+
int offset = 0;
211+
// 0...0X...X000
212+
final int limit = (int) (length & (FILL_NATIVE_THRESHOLD - 8));
213+
for (; offset < limit; offset += 8) {
214+
SCOPED_MEMORY_ACCESS.putLong(sessionImpl(), unsafeGetBase(), unsafeGetOffset() + offset, longValue);
215+
}
216+
int remaining = (int) length - limit;
217+
// 0...0X00
218+
if (remaining >= 4) {
219+
SCOPED_MEMORY_ACCESS.putInt(sessionImpl(), unsafeGetBase(), unsafeGetOffset() + offset, (int) longValue);
220+
offset += 4;
221+
remaining -= 4;
222+
}
223+
// 0...00X0
224+
if (remaining >= 2) {
225+
SCOPED_MEMORY_ACCESS.putShort(sessionImpl(), unsafeGetBase(), unsafeGetOffset() + offset, (short) longValue);
226+
offset += 2;
227+
remaining -= 2;
228+
}
229+
// 0...000X
230+
if (remaining == 1) {
231+
SCOPED_MEMORY_ACCESS.putByte(sessionImpl(), unsafeGetBase(), unsafeGetOffset() + offset, value);
232+
}
233+
// We have now fully handled 0...0X...XXXX
234+
} else {
235+
// Handle larger segments via native calls
236+
SCOPED_MEMORY_ACCESS.setMemory(sessionImpl(), unsafeGetBase(), unsafeGetOffset(), length, value);
237+
}
195238
return this;
196239
}
197240

‎test/jdk/java/foreign/TestFill.java

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2024, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @summary Test MemorySegment::fill
27+
* @run junit TestFill
28+
*/
29+
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.Arguments;
32+
import org.junit.jupiter.params.provider.MethodSource;
33+
34+
import java.lang.foreign.Arena;
35+
import java.lang.foreign.ValueLayout;
36+
import java.util.Arrays;
37+
import java.util.concurrent.CompletableFuture;
38+
import java.util.concurrent.atomic.AtomicReference;
39+
import java.util.stream.IntStream;
40+
import java.util.stream.Stream;
41+
42+
import static org.junit.jupiter.api.Assertions.*;
43+
44+
final class TestFill {
45+
46+
// Make sure negative values are treated as expected
47+
private static final byte VALUE = -71;
48+
49+
@ParameterizedTest
50+
@MethodSource("sizes")
51+
void testFill(int len) {
52+
int offset = 16;
53+
int expandedLen = offset + MAX_SIZE + offset;
54+
55+
// Make sure fill only affects the intended region XXXXXX
56+
//
57+
// ................XXXXXX................
58+
// | offset | len | offset |
59+
60+
try (var arena = Arena.ofConfined()) {
61+
var segment = arena.allocate(expandedLen);
62+
var slice = segment.asSlice(offset, len);
63+
slice.fill(VALUE);
64+
65+
var expected = new byte[expandedLen];
66+
Arrays.fill(expected, offset, offset + len, VALUE);
67+
68+
// This checks the actual fill region as well as potential under and overflows
69+
assertArrayEquals(expected, segment.toArray(ValueLayout.JAVA_BYTE));
70+
}
71+
}
72+
73+
@ParameterizedTest
74+
@MethodSource("values")
75+
void testValues(int value) {
76+
int size = 0b1111;
77+
try (var arena = Arena.ofConfined()) {
78+
var segment = arena.allocate(size);
79+
segment.fill((byte) value);
80+
assertTrue(segment.elements(ValueLayout.JAVA_BYTE)
81+
.map(s -> s.get(ValueLayout.JAVA_BYTE, 0))
82+
.allMatch(v -> v == value), "Failed to fill with value " + value);
83+
}
84+
}
85+
86+
@ParameterizedTest
87+
@MethodSource("sizes")
88+
void testReadOnly(int len) {
89+
try (var arena = Arena.ofConfined()) {
90+
var segment = arena.allocate(10).asReadOnly();
91+
assertThrows(IllegalArgumentException.class, () -> segment.fill(VALUE));
92+
}
93+
}
94+
95+
@ParameterizedTest
96+
@MethodSource("sizes")
97+
void testConfinement(int len) {
98+
try (var arena = Arena.ofConfined()) {
99+
var segment = arena.allocate(10);
100+
AtomicReference<RuntimeException> ex = new AtomicReference<>();
101+
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
102+
try {
103+
segment.fill(VALUE);
104+
} catch (RuntimeException e) {
105+
ex.set(e);
106+
}
107+
});
108+
future.join();
109+
assertInstanceOf(WrongThreadException.class, ex.get());
110+
}
111+
}
112+
113+
@ParameterizedTest
114+
@MethodSource("sizes")
115+
void testScope(int len) {
116+
var arena = Arena.ofConfined();
117+
var segment = arena.allocate(len);
118+
arena.close();
119+
assertThrows(IllegalStateException.class, () -> segment.fill(VALUE));
120+
}
121+
122+
private static final int MAX_SIZE = 1 << 10;
123+
124+
private static Stream<Arguments> sizes() {
125+
return IntStream.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 23, 32, 63, 128, 256, 511, MAX_SIZE)
126+
.boxed()
127+
.map(Arguments::of);
128+
}
129+
130+
private static Stream<Arguments> values() {
131+
return IntStream.rangeClosed(Byte.MIN_VALUE, Byte.MAX_VALUE)
132+
.boxed()
133+
.map(Arguments::of);
134+
}
135+
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2024, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
package org.openjdk.bench.java.lang.foreign;
26+
27+
import org.openjdk.jmh.annotations.Benchmark;
28+
import org.openjdk.jmh.annotations.BenchmarkMode;
29+
import org.openjdk.jmh.annotations.Fork;
30+
import org.openjdk.jmh.annotations.Measurement;
31+
import org.openjdk.jmh.annotations.Mode;
32+
import org.openjdk.jmh.annotations.OutputTimeUnit;
33+
import org.openjdk.jmh.annotations.Param;
34+
import org.openjdk.jmh.annotations.Scope;
35+
import org.openjdk.jmh.annotations.Setup;
36+
import org.openjdk.jmh.annotations.State;
37+
import org.openjdk.jmh.annotations.Warmup;
38+
39+
import java.lang.foreign.Arena;
40+
import java.lang.foreign.MemorySegment;
41+
import java.nio.ByteBuffer;
42+
import java.util.Arrays;
43+
import java.util.concurrent.TimeUnit;
44+
45+
@BenchmarkMode(Mode.AverageTime)
46+
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
47+
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
48+
@State(Scope.Thread)
49+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
50+
@Fork(value = 3)
51+
public class TestFill {
52+
53+
@Param({"0", "1", "2", "3", "4", "5", "6", "7",
54+
"8", "9", "10", "11", "12", "13", "14", "15",
55+
"16", "17", "18", "19", "20", "21", "22", "23",
56+
"24", "25", "26", "27", "28", "29", "30", "31",
57+
"32", "128", "256", "384", "511", "512"})
58+
public int ELEM_SIZE;
59+
60+
byte[] array;
61+
MemorySegment heapSegment;
62+
MemorySegment nativeSegment;
63+
MemorySegment unalignedSegment;
64+
ByteBuffer buffer;
65+
66+
@Setup
67+
public void setup() {
68+
array = new byte[ELEM_SIZE];
69+
heapSegment = MemorySegment.ofArray(array);
70+
nativeSegment = Arena.ofAuto().allocate(ELEM_SIZE, 8);
71+
unalignedSegment = Arena.ofAuto().allocate(ELEM_SIZE + 1, 8).asSlice(1);
72+
buffer = ByteBuffer.wrap(array);
73+
}
74+
75+
@Benchmark
76+
public void arrays_fill() {
77+
Arrays.fill(array, (byte) 0);
78+
}
79+
80+
@Benchmark
81+
public void heap_segment_fill() {
82+
heapSegment.fill((byte) 0);
83+
}
84+
85+
@Benchmark
86+
public void native_segment_fill() {
87+
nativeSegment.fill((byte) 0);
88+
}
89+
90+
@Benchmark
91+
public void unaligned_segment_fill() {
92+
unalignedSegment.fill((byte) 0);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)
Please sign in to comment.