New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse StopIteration exceptions (loop optimization) #881

merged 9 commits into from Aug 2, 2018


None yet
2 participants

patiences commented Jul 31, 2018

Nested loops are expensive in part because of StopIteration exceptions that have to be created. Since most of these exceptions are created and thrown away, it might be a good idea to recycle them.

Results on benchmarking test:

Without optimization

Running test_loops
  Elapsed time:  33.62263886607252  sec
  CPU process time:  0.000150000000000039  sec
Running test_loops
  Elapsed time:  34.80267596896738  sec
  CPU process time:  0.00011499999999997623  sec
Running test_loops
  Elapsed time:  33.84538966906257  sec
  CPU process time:  0.00011900000000003574  sec

With optimization

Running test_loops
  Elapsed time:  24.959855893044733  sec
  CPU process time:  9.500000000001174e-05  sec
Running test_loops
  Elapsed time:  25.487824370036833  sec
  CPU process time:  0.00013099999999999223  sec
Running test_loops
  Elapsed time:  25.309327021008357  sec
  CPU process time:  0.0011470000000000091  sec

~25% improvement!

patiences added some commits Jul 29, 2018

@patiences patiences changed the title from [WIP] Loop optimizations to Reuse StopIteration exceptions (loop optimization) Aug 1, 2018


This looks good; however, there's an edge case where the introduction of a singleton will cause equality tests to behave differently.

This needs to be flagged in documentation as a reference that implementors need to be aware of.

JavaOpcodes.GETSTATIC('org/python/exceptions/StopIteration', 'STOPITERATION', 'Lorg/python/exceptions/StopIteration;'),

This comment has been minimized.


freakboy3742 Aug 2, 2018


No need to keep these as commented code; just delete them.

This comment has been minimized.


patiences Aug 2, 2018



patiences added some commits Aug 2, 2018


👍 Nicely done!

@freakboy3742 freakboy3742 merged commit d1f3b64 into pybee:master Aug 2, 2018

5 checks passed

beekeeper:0/beefore:javacheckstyle Java lint checks passed.
beekeeper:0/beefore:pycodestyle Python lint checks passed.
beekeeper:1/smoke-test Smoke build (Python 3.4) passed.
beekeeper:2/full-test:py3.5 Python 3.5 tests passed.
beekeeper:2/full-test:py3.6 Python 3.6 tests passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment