Files
sdk/runtime/vm/finalizable_data.h
T
Alexander Aprelev 4ccae238ea [vm/isolate] Add TransferableTypedData class that allows low-cost passing of Uint8List between isolates.
TransferableTypedData instances are one-use kind of thing: once receiver materializes it, it can't be used
again, once sender sends it out to an isolate, sender can't send it to different isolate.

Example of use:

sender isolate:

```
Future<TransferableTypedData> consolidateHttpClientResponseBytes(HttpClientResponse response) {
  final completer = Completer<TransferableTypedData>();
  final chunks = <Uint8List>[];
  response.listen((List<int> chunk) {
    chunks.add(chunk);
  }, onDone: () {
    completer.complete(TransferableTypedData.fromList(chunks));
  });
  return completer.future;
}
...
sendPort.send(await consolidateHttpClientResponseBytes(response));
```

receiver isolate:
```
    RawReceivePort port = RawReceivePort((TransferableTypedData transferable) {
      Uint8List content = transferable.materialize().asUint8List();
      ...
    });
```

31959[tr] and 31960[tr] tests were inspired by dartbug.com/31959, dartbug.com/31960 that this CL attempts to address:
```
╰─➤  out/ReleaseX64/dart 31960.dart
sending...
163ms for round-trip
sending...
81ms for round-trip
sending...
20ms for round-trip
sending...
14ms for round-trip
sending...
20ms for round-trip
sending...
14ms for round-trip
```

(notice no "since last checking" pauses") vs

```
╰─➤  out/ReleaseX64/dart 31960.dart
sending...
154ms since last checkin
174ms for round-trip
sending...
68ms since last checkin
9ms since last checkin
171ms for round-trip
sending...
13ms since last checkin
108ms for round-trip
sending...
14ms since last checkin
108ms for round-trip
sending...
14ms since last checkin
107ms for round-trip
```

Change-Id: I0fcb5ce285394f498c3f1db4414204531f98199d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99623
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-06-06 19:49:07 +00:00

76 lines
2.2 KiB
C++

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_FINALIZABLE_DATA_H_
#define RUNTIME_VM_FINALIZABLE_DATA_H_
#include "include/dart_api.h"
#include "platform/growable_array.h"
#include "vm/globals.h"
namespace dart {
struct FinalizableData {
void* data;
void* peer;
Dart_WeakPersistentHandleFinalizer callback;
Dart_WeakPersistentHandleFinalizer successful_write_callback;
};
class MessageFinalizableData {
public:
MessageFinalizableData() : records_(0), position_(0), external_size_(0) {}
~MessageFinalizableData() {
for (intptr_t i = position_; i < records_.length(); i++) {
records_[i].callback(nullptr, nullptr, records_[i].peer);
}
}
/// If [successful_write_callback] is provided, it's invoked when message
/// was serialized successfully.
/// [callback] is invoked when serialization failed.
void Put(
intptr_t external_size,
void* data,
void* peer,
Dart_WeakPersistentHandleFinalizer callback,
Dart_WeakPersistentHandleFinalizer successful_write_callback = nullptr) {
FinalizableData finalizable_data;
finalizable_data.data = data;
finalizable_data.peer = peer;
finalizable_data.callback = callback;
finalizable_data.successful_write_callback = successful_write_callback;
records_.Add(finalizable_data);
external_size_ += external_size;
}
FinalizableData Take() {
ASSERT(position_ < records_.length());
return records_[position_++];
}
void SerializationSucceeded() {
for (intptr_t i = position_; i < records_.length(); i++) {
if (records_[i].successful_write_callback != nullptr) {
records_[i].successful_write_callback(nullptr, nullptr,
records_[i].peer);
}
}
}
intptr_t external_size() const { return external_size_; }
private:
MallocGrowableArray<FinalizableData> records_;
intptr_t position_;
intptr_t external_size_;
DISALLOW_COPY_AND_ASSIGN(MessageFinalizableData);
};
} // namespace dart
#endif // RUNTIME_VM_FINALIZABLE_DATA_H_