Skip to content

Improve islice_extended memory usage when start<0 and step>0#995

Merged
bbayles merged 2 commits intomore-itertools:masterfrom
ben42code:u/ben42code/islice_extended_memory_usage
May 30, 2025
Merged

Improve islice_extended memory usage when start<0 and step>0#995
bbayles merged 2 commits intomore-itertools:masterfrom
ben42code:u/ben42code/islice_extended_memory_usage

Conversation

@ben42code
Copy link
Copy Markdown
Contributor

Ensure we release iterated elements as soon as start<0 and step>0.
We're currently holding a reference on all the elements until we've completed a full iteration.

See #994 for a detailed example.

Issue reference

#994

Changes

In this specific case, improved the iteration over the cached data, by also releasing elements after having been yield.

Checks and tests

test_all (tests.test_more.IsliceExtendedTests.test_all) ... ok
test_invalid_slice (tests.test_more.IsliceExtendedTests.test_invalid_slice) ... ok
test_slicing (tests.test_more.IsliceExtendedTests.test_slicing) ... ok
test_slicing_extensive (tests.test_more.IsliceExtendedTests.test_slicing_extensive) ... ok
test_zero_step (tests.test_more.IsliceExtendedTests.test_zero_step) ... ok

Ensure we release iterated elements asap when start<0 and step>0.
We're currently holding a reference on all the elements until we've
completed a full iteration.

See more-itertools#994 for
details.
- previous was partial.
cached_item variable was keeping a reference on the current item a bit
too long.
@JamesParrott
Copy link
Copy Markdown
Contributor

I'm not sure this is how Bo wants to tackle this issue, but that's just what I had in mind Ben. Great stuff - thanks.

@bbayles
Copy link
Copy Markdown
Collaborator

bbayles commented May 30, 2025

This is fine for this case, thanks for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants