Skip to content
This repository has been archived by the owner on Feb 25, 2025. It is now read-only.

Commit

Permalink
Eliminates an extra copy when returning messages from the host platfo…
Browse files Browse the repository at this point in the history
…rm to dart.
  • Loading branch information
gaaclarke committed Jun 14, 2022
1 parent 10ff302 commit a236ed5
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/ui/window/platform_message_response_dart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

namespace flutter {

namespace {
void MappingFinalizer(void* isolate_callback_data, void* peer) {
delete static_cast<fml::Mapping*>(peer);
}
} // anonymous namespace

PlatformMessageResponseDart::PlatformMessageResponseDart(
tonic::DartPersistentValue callback,
fml::RefPtr<fml::TaskRunner> ui_task_runner)
Expand Down Expand Up @@ -42,8 +48,15 @@ void PlatformMessageResponseDart::Complete(std::unique_ptr<fml::Mapping> data) {
}
tonic::DartState::Scope scope(dart_state);

Dart_Handle byte_buffer =
tonic::DartByteData::Create(data->GetMapping(), data->GetSize());
void* mapping = const_cast<uint8_t*>(data->GetMapping());
size_t data_size = data->GetSize();
Dart_Handle byte_buffer = Dart_NewExternalTypedDataWithFinalizer(
/*type=*/Dart_TypedData_kByteData,
/*data=*/mapping,
/*length=*/data_size,
/*peer=*/data.release(),
/*external_allocation_size=*/data_size,
/*callback=*/MappingFinalizer);
tonic::DartInvoke(callback.Release(), {byte_buffer});
}));
}
Expand Down

0 comments on commit a236ed5

Please sign in to comment.