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
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

// ==== -----------------------------------------------------------------------
// MARK: [UInt8] pass-through

public func acceptBytes(_ bytes: [UInt8]) -> Int {
bytes.count
}

public func returnBytes(_ size: Int) -> [UInt8] {
[UInt8](repeating: 0xff, count: size)
}

public func echoBytes(_ bytes: [UInt8]) -> [UInt8] {
bytes
}

// ==== -----------------------------------------------------------------------
// MARK: Data

public func acceptData(_ data: Data) -> Int {
data.count
}

public func returnData(_ size: Int) -> Data {
Data(repeating: 0xff, count: size)
}

// ==== -----------------------------------------------------------------------
// MARK: large multi-parameter function

public func largeFunction(
a: Int32,
b: [UInt8],
c: [Int32],
d: [UInt8]
) -> [UInt8] {
let outSize = b.count + d.count + c.count * 4
return [UInt8](repeating: 0xff, count: outSize)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package org.swift.swiftkit.ffm;

import com.example.swift.Data;
import com.example.swift.MySwiftLibrary;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 2, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Fork(value = 1, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED", "-Xmx1g" })
public class FFMByteArrayBenchmark {

@Param({"ffm"})
public String mode;

@Param({"4096", "65536", "262144", "1048576", "8388608", "16777216"})
public int totalBytes;

byte[] flat;
Data data;
ClosableAllocatingSwiftArena arena;

// Fixtures for largeFunction(a:b:c:d:)
int large_a;
byte[] large_b;
int[] large_c;
byte[] large_d;

@Setup(Level.Trial)
public void beforeAll() {
arena = AllocatingSwiftArena.ofConfined();

flat = new byte[totalBytes];
for (int i = 0; i < totalBytes; i++) {
flat[i] = (byte) (i & 0xff);
}

data = Data.init(flat, arena);

large_a = 1000;
large_b = new byte[8192];
large_c = new int[8192];
for (int i = 0; i < 8192; i++) {
large_b[i] = (byte) (i & 0xff);
large_c[i] = i;
}
large_d = new byte[32];
}

@TearDown(Level.Trial)
public void afterAll() {
arena.close();
}

// ==== -----------------------------------------------------------------
// MARK: [UInt8]

@Benchmark
public long acceptBytes_ffm() {
return MySwiftLibrary.acceptBytes(flat);
}

@Benchmark
public byte[] returnBytes_ffm() {
return MySwiftLibrary.returnBytes(totalBytes);
}

@Benchmark
public byte[] echoBytes_ffm() {
return MySwiftLibrary.echoBytes(flat);
}

// ==== -----------------------------------------------------------------
// MARK: Data

@Benchmark
public long acceptData_ffm() {
return MySwiftLibrary.acceptData(data);
}

@Benchmark
public Data returnData_ffm(Blackhole bh) {
Data result = MySwiftLibrary.returnData(totalBytes, arena);
bh.consume(result.getCount());
return result;
}

@Benchmark
public Data echoData_ffm(Blackhole bh) {
Data echoed = MySwiftLibrary.echoData(data, arena);
bh.consume(echoed.getCount());
return echoed;
}

// ==== -----------------------------------------------------------------
// MARK: large multi-parameter function

@Benchmark
public byte[] wide_ffm() {
return MySwiftLibrary.largeFunction(large_a, large_b, large_c, large_d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import SwiftJava

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

// ==== -----------------------------------------------------------------------
// MARK: [UInt8] pass-through

public func acceptBytes(_ bytes: [UInt8]) -> Int {
bytes.count
}

public func returnBytes(_ size: Int) -> [UInt8] {
[UInt8](repeating: 0xff, count: size)
}

public func echoBytes(_ bytes: [UInt8]) -> [UInt8] {
bytes
}

// ==== -----------------------------------------------------------------------
// MARK: [[UInt8]] pass-through

public func acceptNestedBytes(_ arrays: [[UInt8]]) -> Int {
arrays.reduce(0) { $0 + $1.count }
}

public func returnNestedBytes(_ outer: Int, _ inner: Int) -> [[UInt8]] {
(0..<outer).map { _ in [UInt8](repeating: 0xff, count: inner) }
}

public func echoNestedBytes(_ arrays: [[UInt8]]) -> [[UInt8]] {
arrays
}

// ==== -----------------------------------------------------------------------
// MARK: UnsafeRawBufferPointer (JNI only)

public func acceptBuffer(_ buf: UnsafeRawBufferPointer) -> Int {
buf.count
}

public func acceptMutableBuffer(_ buf: UnsafeMutableRawBufferPointer) -> Int {
buf.count
}

// ==== -----------------------------------------------------------------------
// MARK: Data

public func acceptData(_ data: Data) -> Int {
data.count
}

public func returnData(_ size: Int) -> Data {
Data(repeating: 0xff, count: size)
}

// ==== -----------------------------------------------------------------------
// MARK: large multi-parameter function

public func largeFunction(
a: Int32,
b: [UInt8],
c: [Int32],
d: [UInt8]
) -> [UInt8] {
let outSize = b.count + d.count + c.count * 4
return [UInt8](repeating: 0xff, count: outSize)
}
Loading
Loading