-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Use AsParallel() correctly #33769
Use AsParallel() correctly #33769
Comments
Here's an example of the kinds of things we'd like to catch: |
Estimates:
|
Would the fixer simply remove the call? I do not think it can add it earlier in the chain as it may cause race conditions depending on the implementation of the earlier linq chain, e.g. |
Yes
That's the safe fix, in that it doesn't change the semantics. It'd be fine to offer multiple fixes, though, with a second option being to move the call to the beginning of the chain, and if the developer chooses that, it's their choice. |
I have a working analyzer for the described scenario, as well as the safe fix as you described it. I would still have to integrate it with the analyzer structure in this project but otherwise I would love to work on this if this is available. |
@carlossanlop @buyaa-n Can you follow up with @Mrnikbobjeff on this? We would need to bring this in for API Review if we want to move it forward. |
This comment in dotnet/roslyn-analyzers#2812 gives a clear example of what to detect in a
As long as we keep those rules in mind, we think this is ready for review. After it gets approved, we will let @Mrnikbobjeff know so he can start working on it. Category: cc @buyaa-n |
Thank you for your interest in contributing to this analzyer @Mrnikbobjeff, until the proposal get approved please take a look at our definition of done list - the list of tasks/actions need to be completed/taken before, during, and after the analyzer implementation. You do not have to complete all tasks, but your PR needs to cover all requirements related to the analyzer and fixer implementations (first 2 bullets) at least. |
This makes sense. The only thing we have to check with Manish is whether they analyzer should report separate IDs due to how fixers are advertised in the IDE. |
This is surely something which could be done, but I do not think that would be advisable. Suppose I have a Select method which is not threadsafe, then we would introduce bugs with the fixer. This was not mentioned in the Video. I only have the remove fixer implemented as this is always valid. |
If someone added the AsParallel(), they intended for something to be run in parallel. And while we can't know for sure how much of the query that comes before it was intended, as an option I think it's reasonable to offer to fix it with our best guess for where it would go, which is at the beginning. The only 100% safe fix is to remove it, but both can be offered, and if we have any control which is the "default" / first option, it should be the removal. |
I see the reasoning behing not completely removing the AsParallel call, I just was curious as I can not recall a codefix provider which might introduce bugs for fixing compiler warnings. As fas as I know this would be a first for codefix providers. I will still work on adding the second codefix provider |
Hey @Mrnikbobjeff, would you still be interested in picking this up? |
Tagging subscribers to this area: @tarekgh, @dotnet/area-system-linq-parallel Issue DetailsUsing Category: Performance
|
Using
.AsParallel()
at the end of a LINQ query, e.g.foreach (var item in src.Select(...).Where(...).AsParallel(...))
, is a nop and should either be removed or theAsParallel()
moved earlier in the query. I've even seen developers writeforeach (var item in src.AsParallel())
thinking it parallelizes theforeach
loop, which it doesn't... it'd be good to warn about such misuse.Category: Performance
The text was updated successfully, but these errors were encountered: