Title: Top-of-stack caching in the JIT · Issue #135379 · python/cpython · GitHub
Open Graph Title: Top-of-stack caching in the JIT · Issue #135379 · python/cpython
X Title: Top-of-stack caching in the JIT · Issue #135379 · python/cpython
Description: Unlike #131498 which was a wash for performance, TOS caching in the JIT promises substantial performance improvements. This is because we can create several stencils for each uop, tailored for the number of registers and dynamically vary...
Open Graph Description: Unlike #131498 which was a wash for performance, TOS caching in the JIT promises substantial performance improvements. This is because we can create several stencils for each uop, tailored for the ...
X Description: Unlike #131498 which was a wash for performance, TOS caching in the JIT promises substantial performance improvements. This is because we can create several stencils for each uop, tailored for the ...
Opengraph URL: https://github.com/python/cpython/issues/135379
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Top-of-stack caching in the JIT","articleBody":"Unlike https://github.com/python/cpython/issues/131498 which was a wash for performance, TOS caching in the JIT promises substantial performance improvements. This is because we can create several stencils for each uop, tailored for the number of registers and dynamically vary the number of values cached.\nFor example, in this code:\n```\n LOAD_FAST_BORROW\n LOAD_FAST_BORROW\n BINARY_OP_ADD_INT\n STORE_FAST\n```\nwe can tailor each version to the number of registers cached:\n```\n LOAD_FAST_BORROW_0_1 ( 0 -> 1 registers)\n LOAD_FAST_BORROW_1_2 ( 1 -> 2 registers)\n BINARY_OP_ADD_INT_2_1 ( 2 -> 1 registers)\n STORE_FAST_1_0 ( 1 -> 0 registers)\n```\nthus avoiding any memory traffic to and from the stack at all.\n\nThe exact number of variants per uop will need to be determined empirically.\nHaving more stencils allows more freedom when generating code, but excessive numbers of stencils would cause bloat both at runtime, and in any repository containing the stencils. \n\n#### Spilling and reloading\n\nThere will be an upper bound to the number of values cached and some uops may need a minimum number of values in the cache.\nTo handle those we will need to insert spill and reload uops. Spills will reduce the number of cached values, saving them to the in-memory stack and reloads will do the opposite moving values from the in-memory stack to the cache.\n\nE.g.\n```\n LOAD_FAST_BORROW\n BINARY_OP_ADD_INT\n```\n`BINARY_OP_ADD_INT` expects two inputs, but we only have one cached (from the `LOAD_FAST_BORROW`) so we need to insert a `RELOAD`:\n```\n LOAD_FAST_BORROW ( 0 -> 1 registers)\n RELOAD_1_2 ( 1 -> 2 registers)\n BINARY_OP_ADD_INT ( 2 -> 1 registers)\n```\n\n`SPILL` and `RELOAD` are semantic no-ops, and will be generated automatically.\n\n#### Deferred references\n\nFor this to work the code generator must spill any cached values to the in-memory stack when GC could occur. Fortunately, the code generator already does this (as part of the work for https://github.com/python/cpython/issues/131498).\n\nSee https://github.com/faster-cpython/ideas/issues/711\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-135384\n* gh-135465\n* gh-135563\n* gh-135668\n* gh-135706\n* gh-135707\n* gh-135714\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/markshannon","@type":"Person","name":"markshannon"},"datePublished":"2025-06-11T09:03:10Z","url":"https://github.com/python/cpython/issues/135379"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:525d5d5b-2a55-b42f-fb3e-106e67864521 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E61E:2A42F4:650FFB:8EC5BD:6AEC9A02 |
| html-safe-nonce | 6018c6ab3a54cbf7d207e2339c25f0e178bb248a5431f206ac400364df19e2e4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNjFFOjJBNDJGNDo2NTBGRkI6OEVDNUJEOjZBRUM5QTAyIiwidmlzaXRvcl9pZCI6Ijc1NTQyNjAxOTc3NzQ4NTMxNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 566d3ebe69a21c6ce755297d9d212ac1d72608eb3e912129ab0e30eee0da79eb |
| hovercard-subject-tag | issue:3136054313 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/python/cpython/135379/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b2886e9d33b8a31b2aa14a6af1871283e440789eaa1794f9c1781ff9c652936c/python/cpython/issues/135379 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b2886e9d33b8a31b2aa14a6af1871283e440789eaa1794f9c1781ff9c652936c/python/cpython/issues/135379 |
| og:image:alt | Unlike #131498 which was a wash for performance, TOS caching in the JIT promises substantial performance improvements. This is because we can create several stencils for each uop, tailored for the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markshannon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 778ee6e397a70c4818ce35b395ffb1fbafa9257e9c82757aeed54a7bc3f8eeb3 |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b259703a082fb4640d324e18ab765d50c336c41c |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width