-
Notifications
You must be signed in to change notification settings - Fork 6k
Image.asset will block UI thread when load large image #58572 #18808
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,30 +19,40 @@ namespace { | |
| // Avoid copying the contents of messages beyond a certain size. | ||
| const int kMessageCopyThreshold = 1000; | ||
|
|
||
| class DataWrapper { | ||
| public: | ||
| DataWrapper(void* data) { data_ = data; } | ||
|
|
||
| ~DataWrapper() { free(data_); } | ||
|
|
||
| private: | ||
| void* data_; | ||
| }; | ||
|
|
||
| void MessageDataFinalizer(void* isolate_callback_data, | ||
| Dart_WeakPersistentHandle handle, | ||
| void* peer) { | ||
| std::vector<uint8_t>* data = reinterpret_cast<std::vector<uint8_t>*>(peer); | ||
| DataWrapper* data = reinterpret_cast<DataWrapper*>(peer); | ||
| delete data; | ||
| } | ||
|
|
||
| Dart_Handle WrapByteData(std::vector<uint8_t> data) { | ||
| if (data.size() < kMessageCopyThreshold) { | ||
| Dart_Handle WrapByteData(std::unique_ptr<fml::Mapping> mapping) { | ||
| size_t size = mapping->GetSize(); | ||
| if (size < kMessageCopyThreshold) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| std::vector<uint8_t> data(size); | ||
| memcpy(data.data(), mapping->GetMapping(), size); | ||
| return ToByteData(data); | ||
| } else { | ||
| std::vector<uint8_t>* heap_data = new std::vector<uint8_t>(std::move(data)); | ||
| return Dart_NewExternalTypedDataWithFinalizer( | ||
| Dart_TypedData_kByteData, heap_data->data(), heap_data->size(), | ||
| heap_data, heap_data->size(), MessageDataFinalizer); | ||
| void* data = malloc(size); | ||
| memset(data, 0, size); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
PlatformMessageResponseDart only post a ui task, and this ui task will consume time when execute
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, just according to the logical of std::vector |
||
| memcpy(data, mapping->GetMapping(), size); | ||
| DataWrapper* wrapper = new DataWrapper(data); | ||
| return Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_kByteData, | ||
| data, size, wrapper, size, | ||
| MessageDataFinalizer); | ||
| } | ||
| } | ||
|
|
||
| Dart_Handle WrapByteData(std::unique_ptr<fml::Mapping> mapping) { | ||
| std::vector<uint8_t> data(mapping->GetSize()); | ||
| memcpy(data.data(), mapping->GetMapping(), mapping->GetSize()); | ||
| return WrapByteData(std::move(data)); | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| PlatformMessageResponseDart::PlatformMessageResponseDart( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this
DataWrapperwould be unnecessary if we just useToByteData's implementation insideWrapByteData:Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intend to optimze according to orignal code. The origial code if if (data.size() < kMessageCopyThreshold) , it will use dart vm heap to allocate data, else it use application heap to alloate data, dart vm only use DataWrapper pointer.
If use ToByteData will consume more dart vm heap.