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/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:1b26a74b-d7bd-a6f6-f4f2-dae3440fa205 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9CF8:66035:C26BB3:10B2C4B:6969F37A |
| html-safe-nonce | 6ed68dae706f9fa5527dabecda45e84deed25c7fcd99f48e2b233642de0032de |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5Q0Y4OjY2MDM1OkMyNkJCMzoxMEIyQzRCOjY5NjlGMzdBIiwidmlzaXRvcl9pZCI6IjI0OTA4NDkyODAzMDExNDUwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 223837da2da55f65526b1a90ff71a48851b5df2fb965210974e44e225b9d1eaf |
| hovercard-subject-tag | pull_request:1215913819 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/files |
| 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 |
| diff-view | unified |
| 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 |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width