Skip to content

Commit

Permalink
use leaf calls to reduce memory copy
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Sep 9, 2024
1 parent d408fc6 commit 00dfca4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 61 deletions.
7 changes: 5 additions & 2 deletions flutter_ffi_plugin/lib/rinf.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// This module supports communication with Rust.
library;

import 'dart:ffi';
import 'dart:typed_data';
import 'src/exports.dart';

Expand Down Expand Up @@ -35,7 +36,9 @@ void sendDartSignal(
) async {
sendDartSignalReal(
messageId,
messageBytes,
binary,
messageBytes.address,
messageBytes.length,
binary.address,
binary.length,
);
}
88 changes: 31 additions & 57 deletions flutter_ffi_plugin/lib/src/interface_os.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:ffi';
import 'dart:typed_data';
import 'load_os.dart';
import 'package:ffi/ffi.dart';
import 'dart:async';
import 'dart:isolate';
import 'interface.dart';
Expand All @@ -18,14 +17,7 @@ Future<void> prepareInterfaceReal(
) async {
/// This should be called once at startup
/// to enable `allo_isolate` to send data from the Rust side.
final rustFunction = rustLibrary.lookupFunction<
Pointer Function(
Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>>,
),
Pointer Function(
Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>>,
)>('store_dart_post_cobject');
rustFunction(NativeApi.postCObject);
storeDartPostCObjectReal(NativeApi.postCObject);

// Prepare ports for communication over isolates.
final rustSignalPort = ReceivePort();
Expand Down Expand Up @@ -58,56 +50,38 @@ Future<void> prepareInterfaceReal(
prepareIsolateReal(rustSignalPort.sendPort.nativePort);
}

void startRustLogicReal() {
final rustFunction =
rustLibrary.lookupFunction<Void Function(), void Function()>(
'start_rust_logic_extern',
);
rustFunction();
}
@Native<Void Function()>(isLeaf: true, symbol: 'start_rust_logic_extern')
external void startRustLogicReal();

void stopRustLogicReal() {
final rustFunction =
rustLibrary.lookupFunction<Void Function(), void Function()>(
'stop_rust_logic_extern',
);
rustFunction();
}
@Native<Void Function()>(isLeaf: true, symbol: 'stop_rust_logic_extern')
external void stopRustLogicReal();

/// Sends bytes to Rust.
Future<void> sendDartSignalReal(
typedef SendDartSignalReal = Void Function(
Int32,
Pointer<Uint8>,
UintPtr,
Pointer<Uint8>,
UintPtr,
);
@Native<SendDartSignalReal>(isLeaf: true, symbol: 'send_dart_signal_extern')
external void sendDartSignalReal(
int messageId,
Uint8List messageBytes,
Uint8List binary,
) async {
final Pointer<Uint8> messageMemory = malloc.allocate(messageBytes.length);
messageMemory.asTypedList(messageBytes.length).setAll(0, messageBytes);

final Pointer<Uint8> binaryMemory = malloc.allocate(binary.length);
binaryMemory.asTypedList(binary.length).setAll(0, binary);
Pointer<Uint8> messageBytesAddress,
int messageBytesLength,
Pointer<Uint8> binaryAddress,
int binaryLength,
);

final rustFunction = rustLibrary.lookupFunction<
Void Function(Int32, Pointer<Uint8>, UintPtr, Pointer<Uint8>, UintPtr),
void Function(int, Pointer<Uint8>, int, Pointer<Uint8>, int)>(
'send_dart_signal_extern',
);
@Native<Void Function(Int64)>(isLeaf: true, symbol: 'prepare_isolate_extern')
external void prepareIsolateReal(
int port,
);

rustFunction(
messageId,
messageMemory,
messageBytes.length,
binaryMemory,
binary.length,
);

malloc.free(messageMemory);
malloc.free(binaryMemory);
}

void prepareIsolateReal(int port) {
final rustFunction =
rustLibrary.lookupFunction<Void Function(Int64), void Function(int)>(
'prepare_isolate_extern',
);
rustFunction(port);
}
typedef InnerFunction = Int8 Function(Int64, Pointer<Dart_CObject>);
@Native<Void Function(Pointer<NativeFunction<InnerFunction>>)>(
isLeaf: true,
symbol: 'store_dart_post_cobject',
)
external void storeDartPostCObjectReal(
Pointer<NativeFunction<InnerFunction>> postCObject,
);
4 changes: 2 additions & 2 deletions flutter_ffi_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version: 6.15.0
repository: https://github.com/cunarist/rinf

environment:
sdk: ">=3.0.5 <4.0.0"
flutter: ">=3.3.0"
sdk: ">=3.5.0 <4.0.0"
flutter: ">=3.22.0"

dependencies:
flutter:
Expand Down

0 comments on commit 00dfca4

Please sign in to comment.