Title: Use a principled, and consistent, implementation of freelists. · Issue #100240 · python/cpython · GitHub
Open Graph Title: Use a principled, and consistent, implementation of freelists. · Issue #100240 · python/cpython
X Title: Use a principled, and consistent, implementation of freelists. · Issue #100240 · python/cpython
Description: We currently implement freelists for the following object and internal structs str float tuple (about 20 freelists, which is really wasteful) list async generator contexts small dicts small dict keys slice (a freelist of one) All of thes...
Open Graph Description: We currently implement freelists for the following object and internal structs str float tuple (about 20 freelists, which is really wasteful) list async generator contexts small dicts small dict ke...
X Description: We currently implement freelists for the following object and internal structs str float tuple (about 20 freelists, which is really wasteful) list async generator contexts small dicts small dict ke...
Opengraph URL: https://github.com/python/cpython/issues/100240
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use a principled, and consistent, implementation of freelists.","articleBody":"We currently implement freelists for the following object and internal structs\r\n\r\n* str\r\n* float \r\n* tuple (about 20 freelists, which is really wasteful)\r\n* list\r\n* async generator\r\n* contexts\r\n* small dicts\r\n* small dict keys\r\n* slice (a freelist of one)\r\n\r\nAll of these are implemented independently and rather inefficiently.\r\nThey take up 3672 bytes of space, instead of the ~200 bytes they should take.\r\nThis is not a lot in terms of memory, but it is a lot in terms of L1 cache.\r\n\r\nA freelist should look like this:\r\n```C\r\ntypedef struct {\r\n void *head;\r\n uint32_t space;\r\n uint16_t current_capacity;\r\n uint16_t limit_capacity;\r\n} _PyFreelist;\r\n```\r\n\r\nOnly one test is needed for allocation and deallocation (on the fast path).\r\nAllocation needs to test `freelist.head != NULL`. Deallocation needs to test `freelist.space != 0`.\r\n\r\nThe actual list is threaded through the objects on the list, terminated by `NULL`.\r\n\r\nCache locality is good. The `head` and `space` are adjacent, and 4 freelists fit in a single cache line.\r\nWhen freeing, the object is hot (and thus in cache).\r\nWhen allocating, the object is about to be used, so needs to be moved to cache anyway.\r\n\r\nThe `capacity` fields are there to allow the capacity of a freelist to be temporarily set to 0, ensuring that all allocations go through the main allocator, for use cases like `tracemalloc`. Currently `tracemalloc` doesn't see a lot of allocations, due to freelists.\r\n\r\nUnifying the code for freelists reduces code duplication, and simplifies things for further improvements.\r\n\r\n### Original discussion\r\n\r\nhttps://github.com/faster-cpython/ideas/discussions/132\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-101453\n* gh-121934\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/markshannon","@type":"Person","name":"markshannon"},"datePublished":"2022-12-14T12:29:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/100240/cpython/issues/100240"}
| 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:029ab0c5-1ef0-84f4-866e-d4a43970ee47 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB5A:10503:9BF2C6:C8E200:696AF5AD |
| html-safe-nonce | 4cb682ea8947906751fd3ba697df90d53428ff9c607cc90af74d388dbb5dc67f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjVBOjEwNTAzOjlCRjJDNjpDOEUyMDA6Njk2QUY1QUQiLCJ2aXNpdG9yX2lkIjoiMzc5NTkxOTMxNTE4MTE3MjE0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 94d08bbd8b2221945c868b0e58e2dde0dc884d2ce56eb86d9348b7a7feca540f |
| hovercard-subject-tag | issue:1496536176 |
| 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/100240/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b1e81101e4694afa8157b4f8a61037c1d73abd5c69df4118a81bd398935ec409/python/cpython/issues/100240 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b1e81101e4694afa8157b4f8a61037c1d73abd5c69df4118a81bd398935ec409/python/cpython/issues/100240 |
| og:image:alt | We currently implement freelists for the following object and internal structs str float tuple (about 20 freelists, which is really wasteful) list async generator contexts small dicts small dict ke... |
| 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 | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width