Skip to content

refactored slice to avoid twice iteration - #1011

Closed
leandromoh wants to merge 3 commits into
morelinq:masterfrom
leandromoh:refactor/slice
Closed

refactored slice to avoid twice iteration#1011
leandromoh wants to merge 3 commits into
morelinq:masterfrom
leandromoh:refactor/slice

Conversation

@leandromoh

Copy link
Copy Markdown
Collaborator

No description provided.

@leandromoh
leandromoh requested review from atifaziz and fsateler July 3, 2023 02:29
@viceroypenguin

viceroypenguin commented Jul 3, 2023

Copy link
Copy Markdown
Contributor

While there are two enumerable methods involved in the previous version, I don't see how it would have iterated the sequence twice - both Skip and Take are lazy, and neither will pull items unnecessarily. It would be good to update the tests for Slice to confirm this, though.

Furthermore, for non-list sequence, .Skip().Take() is already heavily optimized by the corelib. It turns into a single EnumerablePartition<TSource>() which handles both already. Take a look at the code here (Skip and Take are available in the same directory, and you can see that Skip creates the EnumerablePartition<> and Take calls EnumerablePartition<>.Take()). [note: since net6.0, Slice is actually an almost synonym for the bcl Take(IEnumerable<TSource> source, Range range) operator - Take(Range) is start..end and Slice(int, int) is start + count]

@codecov

codecov Bot commented Jul 3, 2023

Copy link
Copy Markdown

Codecov Report

Merging #1011 (d8fbc6b) into master (35fefdf) will decrease coverage by 0.01%.
The diff coverage is 91.66%.

❗ Current head d8fbc6b differs from pull request most recent head e221209. Consider uploading reports for the commit e221209 to get more accurate results

@@            Coverage Diff             @@
##           master    #1011      +/-   ##
==========================================
- Coverage   92.57%   92.57%   -0.01%     
==========================================
  Files         113      113              
  Lines        3422     3433      +11     
  Branches     1055     1060       +5     
==========================================
+ Hits         3168     3178      +10     
  Misses        191      191              
- Partials       63       64       +1     
Impacted Files Coverage Δ
MoreLinq/Slice.cs 92.00% <91.66%> (-0.86%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@atifaziz atifaziz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While there are two enumerable methods involved in the previous version, I don't see how it would have iterated the sequence twice - both Skip and Take are lazy, and neither will pull items unnecessarily.

@leandromoh I agree with @viceroypenguin here that there isn't any re-iteration going on here.

It would be good to update the tests for Slice to confirm this, though.

Yup, this should be done first and anyway. This was the idea behind PR #759. Either a commit against it or a separate one for just Slice would be welcome.

@atifaziz

Copy link
Copy Markdown
Member

Closing this assuming it's unnecessary and abandoned.

@atifaziz atifaziz closed this Oct 15, 2023
@leandromoh
leandromoh deleted the refactor/slice branch October 15, 2023 14:04
atifaziz added a commit that referenced this pull request Nov 25, 2025
This is a squashed commit of PR #1095 hat closes #1011

---------

Co-authored-by: Atif Aziz <code@raboof.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants