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

[engine] move asset mapping copy to background thread #39918

Merged
merged 9 commits into from
Mar 8, 2023

Conversation

jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Feb 27, 2023

Fixes flutter/flutter#121525
Fixes flutter/flutter#121351

Move asset open/copy to a background thread

See timeline trace:

image

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.


auto size = data->GetSize();
const void* bytes = static_cast<const void*>(data->GetMapping());
auto sk_data = MakeSkDataWithCopy(bytes, size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was scratching my head about what was being decompressed and where. Is this copy doing the decompression via faults?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I don't know why I called this decompression.

We need to open the file and then copy it into an SkData (due to potential lack of thread-safeness of asset mappings_.

@jonahwilliams jonahwilliams changed the title [engine] move asset decompression to IO thread [engine] move asset mapping copy to IO thread Feb 28, 2023
Comment on lines +86 to +87
// -1 is used as a sentinel that the file could not be opened.
tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just use Dart_Null() to avoid a sentinel and the tonic conversion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the receiving callback would have to be nullable and its still a sentinel value.

tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(buffer_size)});
});

dart_state->GetConcurrentTaskRunner()->PostTask(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this isn't the IO thread either, this is the worker pool.

There should really be some kind of test for this. https://github.com/flutter/engine/pull/29016/files was a similar attempt that had a test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will take a look at this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there perhaps a better way to do this without mocking out so much functionality? I don't actually care that its done on a background worker either, just that its non blocking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some more time looking at this and I'm still scratching my head a bit. I can't mock/override things on the engine to implement this easily since immutable buffer itself is mostly using UIDartState.

@dnfield any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you could basically use the test from the PR I linked.

It doesn't assert the copy is happening on the worker thread, but it does assert that assets are opened on the worker thread.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not, that PR predates us moving all of this logic to immutable_buffer

@jonahwilliams jonahwilliams changed the title [engine] move asset mapping copy to IO thread [engine] move asset mapping copy to background thread Mar 1, 2023
@chinmaygarde
Copy link
Member

From Triage: Only the AAssetManager backed asset managers have thread safety issues. We should just ask the managers about their thread safety characteristics so that we can elide the copy where we can. I suspect everything except Android can.

@jonahwilliams
Copy link
Member Author

tested!

@jonahwilliams
Copy link
Member Author

tests worked locally but I was not building unopt, will double check again

EXPECT_EQ(leaked_resolver->mapping_requests[0], "DoesNotExist");

PlatformViewNotifyDestroyed(shell.get());
DestroyShell(std::move(shell));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want DestroyShell(std::move(shell), task_runners) - the unopt crash is suggesting the shell is getting destroyed on the wrong thread.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for saving me hours of debugging

zanderso added a commit that referenced this pull request Mar 8, 2023
auto-submit bot pushed a commit that referenced this pull request Mar 8, 2023
…" (#40147)

Revert "[engine] move asset mapping copy to background thread"
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 8, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scrolling is janky in gridView with local images [regression] Janky gridView on android
3 participants