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

Code Quality: Reduced usage of Await inside foreach loops #15005

Merged
merged 45 commits into from Apr 14, 2024

Conversation

gumbarros
Copy link
Contributor

@gumbarros gumbarros commented Mar 20, 2024

Resolved / Related Issues

  • Were these changes approved in an issue or discussion with the project maintainers? In order to prevent extra work, feature requests and changes to the codebase must be approved before the pull request will be reviewed. This prevents extra work for the contributors and maintainers.
    Related to Code quality: Await is being used inside foreach loop #10268

Validation
How did you test these changes?

  • Did you build the app and test your changes?
  • Did you check for accessibility? You can use Accessibility Insights for this.
  • Did you remove any strings from the en-us resource file?
    • Did you search the solution to see if the string is still being used?
  • Did you implement any design changes to an existing feature?
    • Was this change approved?
  • Are there any other steps that were used to validate these changes?
    1. Open app ...
    2. Click settings button ...

Fixed every .ForEach async void, except two edge cases at ItemViewModel.cs,in the method UpdateDateDisplay and the property EnabledGitProperties.

@yaira2 yaira2 added the changes requested Changes are needed for this pull request label Mar 20, 2024
@gumbarros
Copy link
Contributor Author

gumbarros commented Mar 20, 2024

Good morning @yaira2! fixed the places that you asked, the other places can run concurrently?

@yaira2
Copy link
Member

yaira2 commented Mar 20, 2024

Good morning @yaira2! fixed the places that you asked, the other places can run concurrently?

The code for clearing the jumplist can, but I'm pretty sure the other ones need to run in a specific order. It might be best to revert those changes and just add a comment for future reference.

@gumbarros
Copy link
Contributor Author

gumbarros commented Mar 20, 2024

@yaira2 I think now it's ready, the only places that have Task.WhenAll now are for clearing the jumplist can, where the order does not matter. Just clarifying the other changes, places with .ForEach with async are changed to C# foreach so the Task results can be correctly handled.

@yaira2
Copy link
Member

yaira2 commented Mar 20, 2024

Thank you for staying on top of this! I'll try to finish my review in the next few days.

Just a quick tip regarding "conversations", instead of marking them as resolved, it's best to add a comment so the reviewer can see it and complete the conversation if they are happy with the solution.

@yaira2 yaira2 added needs-review and removed changes requested Changes are needed for this pull request labels Mar 20, 2024
Copy link
Member

@hishitetsu hishitetsu left a comment

Choose a reason for hiding this comment

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

Why are you changing LINQ ForEach to foreach loop? Since lambda expressions in ForEach are not awaited, awaiting them in foreach loop is not good for performance.

@gumbarros
Copy link
Contributor Author

gumbarros commented Apr 10, 2024

Why are you changing LINQ ForEach to foreach loop? Since lambda expressions in ForEach are not awaited, awaiting them in foreach loop is not good for performance.

https://stackoverflow.com//questions/59518243/what-is-the-proper-way-to-await-for-async-inside-of-a-foreach#answer-59518587

This answer explains well the difference.

Then we can migrate the changed .ForEach with async void to Task.WhenAll, as they were already out of order. Also async void inside .ForEach will swallow any exceptions.

@hishitetsu
Copy link
Member

Does this mean that this PR will not only improve performance, but will also include modifications to guarantee order where order is important, even at the expense of performance?

Then we can migrate the changed .ForEach with async void to Task.WhenAll

Awaiting Task.WhenAll also affects performance compared to .ForEach with async void. Is that also factored in?

@gumbarros
Copy link
Contributor Author

Does this mean that this PR will not only improve performance, but will also include modifications to guarantee order where order is important, even at the expense of performance?

Then we can migrate the changed .ForEach with async void to Task.WhenAll

Awaiting Task.WhenAll also affects performance compared to .ForEach with async void. Is that also factored in?

What about Parallel.ForEachAsync instead of ForEach with async void?

@hishitetsu
Copy link
Member

The issue is whether or not to await Task.WhenAll. If not awaiting, the performance is the same as .ForEach with async void.
If there is no performance problem with await, then that's fine.

src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs Outdated Show resolved Hide resolved
src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs Outdated Show resolved Hide resolved
src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs Outdated Show resolved Hide resolved
Comment on lines 165 to 166
// Await all removal tasks to complete concurrently
await Task.WhenAll(source.Select(x => jumpListService.RemoveFolderAsync(x.Path)));
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if we should await this. This is deleting items from the jump list, which is different from deleting the items itself, so it may be performed asynchronously.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree

src/Files.App/Views/Layouts/BaseLayoutPage.cs Outdated Show resolved Hide resolved
Copy link
Member

@hishitetsu hishitetsu left a comment

Choose a reason for hiding this comment

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

Probably my last comments.

src/Files.App/Views/Layouts/BaseLayoutPage.cs Outdated Show resolved Hide resolved
Copy link
Member

@hishitetsu hishitetsu left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@yaira2 yaira2 merged commit e9b5d5d into files-community:main Apr 14, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants