Title: gh-97933: add opcode for more efficient comprehension execution by carljm · Pull Request #101310 · python/cpython · GitHub
Open Graph Title: gh-97933: add opcode for more efficient comprehension execution by carljm · Pull Request #101310 · python/cpython
X Title: gh-97933: add opcode for more efficient comprehension execution by carljm · Pull Request #101310 · python/cpython
Description: This avoids allocating a throwaway single-use function object every time we run a comprehension. Otherwise it shouldn't have any user-visible impact; the comprehension is still a separate code object and runs in its own frame, just as before. Tracebacks look the same, etc. We just have a new COMPREHENSION opcode that builds a frame directly from code object and optional closure and inline-calls it, without creating a function object. In a micro-benchmark of comprehension execution time, this looks like it saves about 25%: On inlinecomp (e4a68550426dbea79bd9fc6ff0a75395891d35b7): ➜ ./python -m pyperf timeit -s 'l = [1, 2, 3, 4, 5]' '[x for x in l]' ..................... Mean +- std dev: 243 ns +- 14 ns ➜ ./python -m pyperf timeit -s 'l = [1, 2, 3, 4, 5]' '[x for x in l]' ..................... Mean +- std dev: 240 ns +- 11 ns On main (f02fa64bf2d03ef7a28650c164e17a5fb5d8543d): ➜ ./python -m pyperf timeit -s 'l = [1, 2, 3, 4, 5]' '[x for x in l]' ..................... Mean +- std dev: 324 ns +- 11 ns ➜ ./python -m pyperf timeit -s 'l = [1, 2, 3, 4, 5]' '[x for x in l]' ..................... Mean +- std dev: 329 ns +- 18 ns Currently this doesn't handle async comprehensions or generator expressions; those still create a function object and CALL it. In principle I think they could be handled as well, but to keep the PR smaller I'll defer that to a second PR if this one is merged. Closes #97933. Issue: gh-97933
Open Graph Description: This avoids allocating a throwaway single-use function object every time we run a comprehension. Otherwise it shouldn't have any user-visible impact; the comprehension is still a separate code ...
X Description: This avoids allocating a throwaway single-use function object every time we run a comprehension. Otherwise it shouldn't have any user-visible impact; the comprehension is still a separate c...
Opengraph URL: https://github.com/python/cpython/pull/101310
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:82975046-70a3-71f7-df33-e60c6fe53e17 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | 8582:14C034:FFA5B6:15F972B:6969F7C5 |
| html-safe-nonce | 8eacaab9033f78d5804c317a3a775e87c1aa7ff0dc14608aa5da33471019dc87 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTgyOjE0QzAzNDpGRkE1QjY6MTVGOTcyQjo2OTY5RjdDNSIsInZpc2l0b3JfaWQiOiI3NzU5Mzc4MTEzMDM1MzY4Mzg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5a5c635c27c203cb82b9143c0ff042c4d518d35a68cdf80097d4edcae21a2eab |
| hovercard-subject-tag | pull_request:1215913819 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/101310/checks |
| twitter:image | https://avatars.githubusercontent.com/u/61586?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/61586?s=400&v=4 |
| og:image:alt | This avoids allocating a throwaway single-use function object every time we run a comprehension. Otherwise it shouldn't have any user-visible impact; the comprehension is still a separate code ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width