Title: Pure-Python pickle._Pickler can be significantly optimized · Issue #148706 · python/cpython · GitHub
Open Graph Title: Pure-Python pickle._Pickler can be significantly optimized · Issue #148706 · python/cpython
X Title: Pure-Python pickle._Pickler can be significantly optimized · Issue #148706 · python/cpython
Description: Feature or enhancement Proposal: Inspired by some of the optimization patterns used in #148700 , I took a look at the pure Python pickler and found some low-hanging fruit. Some of these changes do increase the complexity of maintaining t...
Open Graph Description: Feature or enhancement Proposal: Inspired by some of the optimization patterns used in #148700 , I took a look at the pure Python pickler and found some low-hanging fruit. Some of these changes do ...
X Description: Feature or enhancement Proposal: Inspired by some of the optimization patterns used in #148700 , I took a look at the pure Python pickler and found some low-hanging fruit. Some of these changes do ...
Opengraph URL: https://github.com/python/cpython/issues/148706
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pure-Python pickle._Pickler can be significantly optimized","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nInspired by some of the optimization patterns used in #148700 , I took a look at the pure Python pickler and found some low-hanging fruit. Some of these changes do increase the complexity of maintaining the code, but the double-digit performance gains might be worth it given `dill` and `cloudpickle` usage patterns.\n\nFYI, my testing showed 19-37% speed increase for `dill` so far.\n\nBelow is Opus's summary of the things *kept* in the PR I will be submitting. The PR includes microbenchmark experiments (`Misc/pickle-perf-diary.md`) for a much broader set of changes that did not survive.\n\n```\n The pure-Python pickle._Pickler (Lib/pickle.py) is used as the fallback when\n the _pickle C accelerator is unavailable and by code that explicitly\n subclasses pickle.Pickler in Python. Its save() hot path currently does\n more work than necessary relative to Modules/_pickle.c::save():\n\n - the memo.get(id(obj)) and reducer_override probes run before the atomic-\n type dispatch, so int / None / bool / float values pay for checks the\n C reference implementation skips for these types;\n - _batch_appends and _batch_setitems use itertools.batched() + enumerate()\n even for exact list / dict instances, where the C accelerator's\n batch_list_exact / batch_dict_exact skip that machinery;\n - memoize() dispatches through put() to write a single MEMOIZE byte for\n protocol >= 4 (the common case);\n - save_long rebuilds the 2-byte BININT1 opcode via struct.pack on every\n small-int save;\n - framer.commit_frame() is invoked as a full method call per save() even\n though the hot check is a single length compare;\n - bytes values aren't in the save() dispatch fast path alongside str.\n\n Attached patch addresses each of these with measured wins of -20% to -49%\n on pure-Python _Pickler dump across a representative workload set\n (list_of_ints, list_of_strs, dict of str->int, deep list, nested list of\n dicts, list of short bytes, dict of bytes->int). Full CPython test suite,\n dill 0.4.1, and cloudpickle 3.1.2 all pass / match baseline. Detailed\n experiment ledger with raw bench data committed under\n Misc/pickle-perf-diary.md and Misc/pickle-perf-data/ on the proposed\n branch.\n```\n\nThere is notable user-facing change that can be addressed but I'd defer to reviewers on how important it is: atomic types no longer invoke reducer_override, wich actually aligns this with _pickle.c::save().\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148707\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/mjbommar","@type":"Person","name":"mjbommar"},"datePublished":"2026-04-18T00:26:19Z","url":"https://github.com/python/cpython/issues/148706"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:42b4dbcc-e984-956a-af54-ff36676db736 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC68:2E124E:3DD9B1A:4EF2E61:6B021F3D |
| html-safe-nonce | 3423e64b973820e067cb8b3d296f4c93f53594c0c7594e786891c6c336d1fddb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzY4OjJFMTI0RTozREQ5QjFBOjRFRjJFNjE6NkIwMjFGM0QiLCJ2aXNpdG9yX2lkIjoiNTgzMDU2MDM5ODM4MTgxNzY2MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5ef04add531da96d3e2c1a6d47feb6e9d35c4789ad4c2870ab4000197642aed6 |
| hovercard-subject-tag | issue:4286054080 |
| 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/148706/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70e673a36e67b182290ce550f6feaf369a0519387525d299da0eff1a55c91822/python/cpython/issues/148706 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70e673a36e67b182290ce550f6feaf369a0519387525d299da0eff1a55c91822/python/cpython/issues/148706 |
| og:image:alt | Feature or enhancement Proposal: Inspired by some of the optimization patterns used in #148700 , I took a look at the pure Python pickler and found some low-hanging fruit. Some of these changes do ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mjbommar |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width