Skip to content
This repository has been archived by the owner. It is now read-only.

Reuse StopIteration exceptions (loop optimization) #881

Merged
merged 9 commits into from Aug 2, 2018

Conversation

@patiences
Copy link
Contributor

@patiences 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 patiences changed the title [WIP] Loop optimizations Reuse StopIteration exceptions (loop optimization) Aug 1, 2018
Copy link
Member

@freakboy3742 freakboy3742 left a comment

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.

java.Init('org/python/exceptions/StopIteration'),
JavaOpcodes.GETSTATIC('org/python/exceptions/StopIteration', 'STOPITERATION', 'Lorg/python/exceptions/StopIteration;'),
#java.New('org/python/exceptions/StopIteration'),
#java.Init('org/python/exceptions/StopIteration'),
Copy link
Member

@freakboy3742 freakboy3742 Aug 2, 2018

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

Copy link
Contributor Author

@patiences patiences Aug 2, 2018

Done!

Copy link
Member

@freakboy3742 freakboy3742 left a comment

👍 Nicely done!

@freakboy3742 freakboy3742 merged commit d1f3b64 into beeware:master Aug 2, 2018
5 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants