Bad implementation of Enumerable.Single?

后端 未结 7 598
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 03:19

I came across this implementation in Enumerable.cs by reflector.

public static TSource Single(this IEnumerable source, Func<         


        
7条回答
  •  长情又很酷
    2020-12-15 03:49

    When considering this implementation we must remember that this is the BCL: general code that is supposed to work good enough in all sorts of scenarios.

    First, take these scenarios:

    1. Iterate over 10 numbers, where the first and second elements are equal
    2. Iterate over 1.000.000 numbers, where the first and third elements are equal

    The original algorithm will work well enough for 10 items, but 1M will have a severe waste of cycles. So in these cases where we know that there are two or more early in the sequences, the proposed optimization would have a nice effect.

    Then, look at these scenarios:

    1. Iterate over 10 numbers, where the first and last elements are equal
    2. Iterate over 1.000.000 numbers, where the first and last elements are equal

    In these scenarios the algorithm is still required to inspect every item in the lists. There is no shortcut. The original algorithm will perform good enough, it fulfills the contract. Changing the algorithm, introducing an if on each iteration will actually decrease performance. For 10 items it will be negligible, but 1M it will be a big hit.

    IMO, the original implementation is the correct one, since it is good enough for most scenarios. Knowing the implementation of Single is good though, because it enables us to make smart decisions based on what we know about the sequences we use it on. If performance measurements in one particular scenario shows that Single is causing a bottleneck, well: then we can implement our own variant that works better in that particular scenario.

    Update: as CodeInChaos and Eamon have correctly pointed out, the if test introduced in the optimization is indeed not performed on each item, only within the predicate match block. I have in my example completely overlooked the fact that the proposed changes will not affect the overall performance of the implementation.

    I agree that introducing the optimization would probably benefit all scenarios. It is good to see though that eventually, the decision to implement the optimization is made on the basis of performance measurements.

提交回复
热议问题