roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

Suggested fix for RCS1077 runs slower

Open kiwiant opened this issue 3 years ago • 5 comments

Great work guys!

Product and Version Used: 3.2.2

Running on ASP.NET 4.8, RCS1077 triggers for this snippet:

_items.Where(x => x.Id == findId).SingleOrDefault();

Suggested fix is as follows but runs 250% slower (20,000 iterations):

_items.SingleOrDefault(x => x.Id == findId);

kiwiant avatar Oct 21 '21 00:10 kiwiant

Thanks for you appreciation and sorry for the later answer.

I think thank it's possible that the code with the fix can run slower if the collection is List or array but generally this analyzer simplifies code and improves readability.

If it's on the hot path then I think the best solution is to suppress the warning.

josefpihrt avatar Nov 13 '21 11:11 josefpihrt

I would classify this as an issue with .NET Framework. Though as .NET Framework isn't actively developed I don't think it will be resolved there. Someone (if I find time I could, but not soon) should validate if the same issue persists in .NET 6.

philipborg avatar Dec 13 '21 14:12 philipborg

It does. Simple test in LinqPad shows SingleOrDefault is 2x slower. If you have a List<T> then Find(x => x.Id == findId) is 6x faster than SingleOrDefault(x => x.Id == findId)

PhilPJL avatar Jan 23 '22 13:01 PhilPJL

Yeah. As long as it's an issue in .NET, it should not be suggested.

Meligy avatar Mar 14 '22 05:03 Meligy

Several fixes are part of RCS1077, maybe each should have an eslint config to turn off/on?

Meligy avatar Mar 14 '22 05:03 Meligy