Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use efficient operations when copying bytes between Dart and Java #1019

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions pkgs/cronet_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.0-jni

* Use more efficient operations when copying bytes between Java and Dart.

## 0.3.0-jni

* Switch to using `package:jnigen` for bindings to Cronet
Expand Down
29 changes: 11 additions & 18 deletions pkgs/cronet_http/lib/src/cronet_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:jni/jni.dart';
import 'jni/jni_bindings.dart' as jb;

final _digitRegex = RegExp(r'^\d+$');
const _bufferSize = 10 * 1024; // The size of the Cronet read buffer.

/// The type of caching to use when making HTTP requests.
enum CacheMode {
Expand Down Expand Up @@ -138,7 +139,7 @@ Map<String, String> _cronetToClientHeaders(
jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
BaseRequest request, Completer<StreamedResponse> responseCompleter) {
StreamController<List<int>>? responseStream;
jb.ByteBuffer? byteBuffer;
JByteBuffer? jByteBuffer;
var numRedirects = 0;

// The order of callbacks generated by Cronet is documented here:
Expand Down Expand Up @@ -175,8 +176,8 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
headers: responseHeaders,
));

byteBuffer = jb.ByteBuffer.allocateDirect(1024 * 1024);
urlRequest.read(byteBuffer!);
jByteBuffer = JByteBuffer.allocateDirect(_bufferSize);
urlRequest.read(jByteBuffer!.castTo(const jb.$ByteBufferType()));
},
onRedirectReceived: (urlRequest, responseInfo, newLocationUrl) {
if (!request.followRedirects) {
Expand Down Expand Up @@ -204,20 +205,15 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
},
onReadCompleted: (urlRequest, responseInfo, byteBuffer) {
byteBuffer.flip();
responseStream!
.add(jByteBuffer!.asUint8List().sublist(0, byteBuffer.remaining()));

final remaining = byteBuffer.remaining();
final data = Uint8List(remaining);
// TODO: Use a more efficient approach when
// https://github.com/dart-lang/jnigen/issues/387 is fixed.
for (var i = 0; i < remaining; ++i) {
data[i] = byteBuffer.get1(i);
}
responseStream!.add(data);
byteBuffer.clear();
urlRequest.read(byteBuffer);
},
onSucceeded: (urlRequest, responseInfo) {
responseStream!.sink.close();
jByteBuffer?.release();
},
onFailed: (urlRequest, responseInfo, cronetException) {
final error = ClientException(
Expand All @@ -228,6 +224,7 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
responseStream!.addError(error);
responseStream!.close();
}
jByteBuffer?.release();
},
));
}
Expand Down Expand Up @@ -333,14 +330,10 @@ class CronetClient extends BaseClient {
headers.forEach((k, v) => builder.addHeader(k.toJString(), v.toJString()));

if (body.isNotEmpty) {
// TODO: Use a more efficient approach when
// https://github.com/dart-lang/jnigen/issues/387 is fixed.
final bodyBytes = JArray(jbyte.type, body.length);
for (var i = 0; i < body.length; ++i) {
bodyBytes[i] = body[i];
}
final bodyBuffer =
body.toJByteBuffer().castTo(const jb.$ByteBufferType());
brianquinlan marked this conversation as resolved.
Show resolved Hide resolved
builder.setUploadDataProvider(
jb.UploadDataProviders.create4(bodyBytes), _executor);
jb.UploadDataProviders.create2(bodyBuffer), _executor);
}
builder.build().start();
return responseCompleter.future;
Expand Down
Loading
Loading