Repository: dotnet/runtime
File: src/libraries/System.Linq/src/System/Linq/SkipTake.SpeedOpt.cs (C#)

Code before:
                Fill(_source, array, _minIndexInclusive);
                return array;
            }

            public override List<TSource> ToList()
            {
                int count = Count;

                List<TSource> list = [];
                if (count != 0)
                {
                    Fill(_source, SetCountAndGetSpan(list, count), _minIndexInclusive);
                }

                return list;
            }

            public void CopyTo(TSource[] array, int arrayIndex) =>
                Fill(_source, array.AsSpan(arrayIndex, Count), _minIndexInclusive);

            private static void Fill(IList<TSource> source, Span<TSource> destination, int sourceIndex)
            {
                if (destination.Length == 0)
                {
                    return;
                }

                if (source.TryGetSpan(out ReadOnlySpan<TSource> sourceSpan))
                {
                    sourceSpan.Slice(sourceIndex, destination.Length).CopyTo(destination);
                    return;
                }

                for (int i = 0; i < destination.Length; i++, sourceIndex++)
                {
                    destination[i] = source[sourceIndex];
                }
            }

            public override bool Contains(TSource item) => IndexOf(item) >= 0;

            public int IndexOf(TSource item)
            {
                IList<TSource> source = _source;

                if (source.TryGetSpan(out ReadOnlySpan<TSource> span))
                {
                    int minInclusive = _minIndexInclusive;
                    if (minInclusive < span.Length)
                    {
                        return span.Slice(minInclusive, GetAdjustedCount(minInclusive, _maxIndexInclusive, span.Length)).IndexOf(item);

Review comment (refactor):
@copilot, this addition is only necessary on the path where we're calling Slice. Let's move this if block into the below if block so that it's only used on the path where we can get a span from the source. Also, rather than checking the destination length, would it not be better / clearer to check sourceIndex against sourceSpan.Length?

Code after:
                Fill(_source, array, _minIndexInclusive);
                return array;
            }

            public override List<TSource> ToList()
            {
                int count = Count;

                List<TSource> list = [];
                if (count != 0)
                {
                    Fill(_source, SetCountAndGetSpan(list, count), _minIndexInclusive);
                }

                return list;
            }

            public void CopyTo(TSource[] array, int arrayIndex) =>
                Fill(_source, array.AsSpan(arrayIndex, Count), _minIndexInclusive);

            private static void Fill(IList<TSource> source, Span<TSource> destination, int sourceIndex)
            {
                if (source.TryGetSpan(out ReadOnlySpan<TSource> sourceSpan))
                {
                    if (sourceIndex < sourceSpan.Length)
                    {
                        sourceSpan.Slice(sourceIndex, destination.Length).CopyTo(destination);
                    }

                    return;
                }

                for (int i = 0; i < destination.Length; i++, sourceIndex++)
                {
                    destination[i] = source[sourceIndex];
                }
            }

            public override bool Contains(TSource item) => IndexOf(item) >= 0;

            public int IndexOf(TSource item)
            {
                IList<TSource> source = _source;

                if (source.TryGetSpan(out ReadOnlySpan<TSource> span))
                {
                    int minInclusive = _minIndexInclusive;
                    if (minInclusive < span.Length)
                    {
                        return span.Slice(minInclusive, GetAdjustedCount(minInclusive, _maxIndexInclusive, span.Length)).IndexOf(item);
                    }