Skip to content

Commit

Permalink
eliminated the copies when sending data to and from objc (flutter#25988)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaaclarke authored May 14, 2021
1 parent e7290ec commit 1abeeec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions shell/platform/darwin/common/buffer_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace flutter {

fml::MallocMapping CopyNSDataToMapping(NSData* data);

NSData* CopyMappingToNSData(fml::MallocMapping buffer);
NSData* ConvertMappingToNSData(fml::MallocMapping buffer);

std::unique_ptr<fml::Mapping> CopyNSDataToMappingPtr(NSData* data);
std::unique_ptr<fml::Mapping> ConvertNSDataToMappingPtr(NSData* data);

NSData* CopyMappingPtrToNSData(std::unique_ptr<fml::Mapping> mapping);

Expand Down
28 changes: 23 additions & 5 deletions shell/platform/darwin/common/buffer_conversions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@

#import "flutter/shell/platform/darwin/common/buffer_conversions.h"

#include "flutter/fml/platform/darwin/scoped_nsobject.h"

namespace flutter {
namespace {
class NSDataMapping : public fml::Mapping {
public:
NSDataMapping(NSData* data) : data_([data retain]) {}

size_t GetSize() const override { return [data_.get() length]; }

const uint8_t* GetMapping() const override {
return static_cast<const uint8_t*>([data_.get() bytes]);
}

private:
fml::scoped_nsobject<NSData> data_;
FML_DISALLOW_COPY_AND_ASSIGN(NSDataMapping);
};
} // namespace

fml::MallocMapping CopyNSDataToMapping(NSData* data) {
const uint8_t* bytes = static_cast<const uint8_t*>(data.bytes);
return fml::MallocMapping::Copy(bytes, data.length);
}

NSData* CopyMappingToNSData(fml::MallocMapping buffer) {
return [NSData dataWithBytes:const_cast<uint8_t*>(buffer.GetMapping()) length:buffer.GetSize()];
NSData* ConvertMappingToNSData(fml::MallocMapping buffer) {
size_t size = buffer.GetSize();
return [NSData dataWithBytesNoCopy:buffer.Release() length:size];
}

std::unique_ptr<fml::Mapping> CopyNSDataToMappingPtr(NSData* data) {
auto mapping = CopyNSDataToMapping(data);
return std::make_unique<fml::MallocMapping>(std::move(mapping));
std::unique_ptr<fml::Mapping> ConvertNSDataToMappingPtr(NSData* data) {
return std::make_unique<NSDataMapping>(data);
}

NSData* CopyMappingPtrToNSData(std::unique_ptr<fml::Mapping> mapping) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
FlutterBinaryMessageHandler handler = it->second;
NSData* data = nil;
if (message->hasData()) {
data = CopyMappingToNSData(message->releaseData());
data = ConvertMappingToNSData(message->releaseData());
}
handler(data, ^(NSData* reply) {
if (completer) {
if (reply) {
completer->Complete(CopyNSDataToMappingPtr(reply));
completer->Complete(ConvertNSDataToMappingPtr(reply));
} else {
completer->CompleteEmpty();
}
Expand Down

0 comments on commit 1abeeec

Please sign in to comment.