Skip to content

Improve islice_extended memory usage when stop<0 and step>0#999

Merged
bbayles merged 2 commits intomore-itertools:masterfrom
ben42code:u/ben42code/islice_extended_memory_usage2
Jun 27, 2025
Merged

Improve islice_extended memory usage when stop<0 and step>0#999
bbayles merged 2 commits intomore-itertools:masterfrom
ben42code:u/ben42code/islice_extended_memory_usage2

Conversation

@ben42code
Copy link
Copy Markdown
Contributor

@ben42code ben42code commented Jun 7, 2025

Issue reference

#1011
Follow up on issue #994

Scope

Improve islice_extended memory usage when stop<0 and step>0

This pull request cover the case when stop<0 and step>0 where we're currently holding a reference on that returned element one iteration step too long, since we store the yielded element in a temporary variable.

start<0 and step>0 was covered with #995 (improvement) and #996 (unit test)

Changes

  • Release the iterated element as soon as it's been returned, for islice_extended when stop<0 and step>0.
  • Update unit test to reflect/confirm lifecycle management improvement.

image

Checks and tests

On python 3.13

ruff format .
12 files left unchanged
python -m unittest -v 'tests.test_more.IsliceExtendedTests'
test_all (tests.test_more.IsliceExtendedTests.test_all) ... ok
test_elements_lifecycle (tests.test_more.IsliceExtendedTests.test_elements_lifecycle) ... 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
coverage report --show-missing --fail-under=99
Name                         Stmts   Miss  Cover   Missing
----------------------------------------------------------
more_itertools\__init__.py       3      0   100%
more_itertools\more.py        1612      1    99%   3411
more_itertools\recipes.py      354     16    95%   95-96, 104-105, 344-345, 994-1012, 1066
----------------------------------------------------------
TOTAL                         1969     17    99%

Ensure we release iterated elements asap when stop<0 and step>0.
We're currently holding a reference on that last returned element one
iteration step too long, since we use a variable to hold the yielded element.

- Improved _islice_helper for start>0,stop<0,step>0.
- Updated unit test test_elements_lifecycle with improvement.
@ben42code ben42code force-pushed the u/ben42code/islice_extended_memory_usage2 branch from eab2b81 to addb37f Compare June 7, 2025 20:15
- improvement has been introduced in previous commit.
- removing comments in a separate commit just for diff readability.
@ben42code
Copy link
Copy Markdown
Contributor Author

@ben42code ben42code changed the title U/ben42code/islice extended memory usage2 Improve islice_extended memory usage when stop<0 and step>0 Jun 21, 2025
@bbayles bbayles merged commit cea037c into more-itertools:master Jun 27, 2025
6 checks passed
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.

2 participants