Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -18,7 +18,6 @@
import com.example.swift.MySwiftClass;
import com.example.swift.MySwiftLibrary;
import org.openjdk.jmh.annotations.*;
import org.swift.swiftkit.core.ClosableSwiftArena;

import java.util.concurrent.TimeUnit;

Expand All @@ -27,7 +26,7 @@
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Fork(value = 2, jvmArgsAppend = {"--enable-native-access=ALL-UNNAMED"})
@Fork(value = 2, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
public class StringPassingBenchmark {

@Param({
Expand Down Expand Up @@ -70,16 +69,23 @@ public long writeString_baseline() {
}

static String makeString(int size) {
var text =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut in augue ullamcorper, mattis lacus tincidunt, " +
"accumsan massa. Morbi gravida purus ut porttitor iaculis. Vestibulum lacinia, mi in tincidunt hendrerit," +
"lectus est placerat magna, vitae vestibulum nulla ligula at massa. Pellentesque nibh quam, pulvinar eu " +
"nunc congue, molestie molestie augue. Nam convallis consectetur velit, at dictum risus ullamcorper iaculis. " +
"Vestibulum lacinia nisi in elit consectetur vulputate. Praesent id odio tristique, tincidunt arcu et, convallis velit. " +
"Sed vitae pulvinar arcu. Curabitur euismod mattis dui in suscipit. Morbi aliquet facilisis vulputate. Phasellus " +
"non lectus dapibus, semper magna eu, aliquet magna. Suspendisse vel enim at augue luctus gravida. Suspendisse " +
"venenatis justo non accumsan sollicitudin. Suspendisse vitae ornare odio, id blandit nibh. Nulla facilisi. " +
"Nulla nulla orci, finibus nec luctus et, faucibus et ligula.";
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut in augue ullamcorper, mattis lacus tincidunt, "
+
"accumsan massa. Morbi gravida purus ut porttitor iaculis. Vestibulum lacinia, mi in tincidunt hendrerit,"
+
"lectus est placerat magna, vitae vestibulum nulla ligula at massa. Pellentesque nibh quam, pulvinar eu "
+
"nunc congue, molestie molestie augue. Nam convallis consectetur velit, at dictum risus ullamcorper iaculis. "
+
"Vestibulum lacinia nisi in elit consectetur vulputate. Praesent id odio tristique, tincidunt arcu et, convallis velit. "
+
"Sed vitae pulvinar arcu. Curabitur euismod mattis dui in suscipit. Morbi aliquet facilisis vulputate. Phasellus "
+
"non lectus dapibus, semper magna eu, aliquet magna. Suspendisse vel enim at augue luctus gravida. Suspendisse "
+
"venenatis justo non accumsan sollicitudin. Suspendisse vitae ornare odio, id blandit nibh. Nulla facilisi. "
+
"Nulla nulla orci, finibus nec luctus et, faucibus et ligula.";
return text.substring(0, size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -15,28 +15,28 @@
package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.*;
import org.swift.swiftkit.ffm.*;

import static org.junit.jupiter.api.Assertions.*;

import java.lang.foreign.ValueLayout;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;

public class FFMArraysTest {

@Test
void test_sumAllByteArrayElements_throughMemorySegment() {
byte[] bytes = new byte[124];
byte[] bytes = new byte[124];
Arrays.fill(bytes, (byte) 1);

try (var arena = AllocatingSwiftArena.ofConfined()) {
// NOTE: We cannot use MemorySegment.ofArray because that creates a HEAP backed segment and therefore cannot pass into native:
// java.lang.IllegalArgumentException: Heap segment not allowed: MemorySegment{ kind: heap, heapBase: [B@5b6ec132, address: 0x0, byteSize: 124 }
// MemorySegment bytesSegment = MemorySegment.ofArray(bytes); // NO COPY (!)
// MySwiftLibrary.sumAllByteArrayElements(bytesSegment, bytes.length);
// NOTE: We cannot use MemorySegment.ofArray because that creates a HEAP backed
// segment and therefore cannot pass into native:
// java.lang.IllegalArgumentException: Heap segment not allowed: MemorySegment{
// kind: heap, heapBase: [B@5b6ec132, address: 0x0, byteSize: 124 }
// MemorySegment bytesSegment = MemorySegment.ofArray(bytes); // NO COPY (!)
// MySwiftLibrary.sumAllByteArrayElements(bytesSegment, bytes.length);

var bytesCopy = arena.allocateFrom(ValueLayout.JAVA_BYTE, bytes);
var swiftSideSum = MySwiftLibrary.sumAllByteArrayElements(bytesCopy, bytes.length);
Expand All @@ -48,7 +48,7 @@ void test_sumAllByteArrayElements_throughMemorySegment() {

@Test
void test_sumAllByteArrayElements_arrayCopy() {
byte[] bytes = new byte[124];
byte[] bytes = new byte[124];
Arrays.fill(bytes, (byte) 1);

var swiftSideSum = MySwiftLibrary.sumAllByteArrayElements(bytes);
Expand All @@ -59,9 +59,8 @@ void test_sumAllByteArrayElements_arrayCopy() {

@Test
void test_getArray() {
AtomicLong bufferSize = new AtomicLong();
byte[] javaBytes = MySwiftLibrary.getArray(); // automatically converted [UInt8] to byte[]

assertArrayEquals(new byte[]{1, 2, 3}, javaBytes);
assertArrayEquals(new byte[] { 1, 2, 3 }, javaBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -15,17 +15,10 @@
package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.*;
import org.swift.swiftkit.ffm.*;

import java.lang.foreign.Arena;
import java.util.Optional;
import java.util.OptionalInt;

import static org.junit.jupiter.api.Assertions.*;

public class MultipleTypesFromSingleFileTest {

@Test
void bothTypesMustHaveBeenGenerated() {
try (var arena = AllocatingSwiftArena.ofConfined()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -14,14 +14,9 @@

package com.example.swift;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftLibraries;
import org.swift.swiftkit.ffm.AllocatingSwiftArena;

import java.io.File;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

public class SwiftTypeInSubDirectoryTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -15,16 +15,11 @@
package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.*;
import org.swift.swiftkit.ffm.*;
import org.swift.swiftkit.core.CallTraces;

import static org.junit.jupiter.api.Assertions.*;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;

public class WithBufferTest {

Expand Down
Loading