René's URL Explorer Experiment


Title: Cache decoded map keys in the C unpacker by sebastianbreguel · Pull Request #768 · msgpack/msgpack-python · GitHub

Open Graph Title: Cache decoded map keys in the C unpacker by sebastianbreguel · Pull Request #768 · msgpack/msgpack-python

X Title: Cache decoded map keys in the C unpacker by sebastianbreguel · Pull Request #768 · msgpack/msgpack-python

Description: This makes unpackb() about 25% faster on data where the same map keys repeat. The cost is about 80 lines of C and roughly 10 ns more per small message (+1.7% on a 9-key map). I know you chose plain interning over a memo dict in #373/#381. This keeps interning and only skips decoding keys it has already interned. If the trade-off is not worth it for msgpack, I'm fine closing this; I'd also take a pointer to a shape you would prefer. What The C unpacker now remembers the map keys it has already decoded. When it reads a key, it looks the raw bytes up in a small cache (256 slots) and reuses the interned str it built last time. On a miss, it decodes as before. The cache turns on after 32 keys in one unpackb() call or Unpacker, so small messages never touch it. Keys enter the cache in unpack_callback_map_item(), right after PyUnicode_InternInPlace(), so only keys of complete pairs get in. This matters on free-threaded builds, where interned strings are immortal. Only ASCII keys up to 64 bytes are cached, so a cached key's data is exactly its raw bytes. The result is the same object the current code returns: the interned key. Why I unpacked 300 pull requests from the GitHub API (4.8 MB of msgpack). That is 106,085 map keys, but only 144 distinct ones: "id", "url", "state" and friends, over and over. unpackb() decodes and interns every one of them. With raw=True (no decoding) it runs 39% faster, and the C unpacker was barely faster than json.loads() on the same data as JSON (8.85 ms vs 9.87 ms). Benchmark CPython 3.12, macOS arm64, best of 7, with the base and patched builds alternated 3 times: Workload Before After Change unpackb(), 300 PR objects (4.8 MB) 8.52 ms 6.29 ms -26% Unpacker, same objects fed in 64 KiB chunks 7.47 ms 5.67 ms -24% unpackb(), 10k maps with 9 keys 570 ns/msg 580 ns/msg +1.7% unpackb(), 10k maps with 1 key 71.1 ns/msg 71.7 ns/msg +0.8% Packing does not change. The small-map runs overlap, so I read them as about 10 ns per 9-key message: the cost of checking each key against the threshold. To reproduce, save any list of JSON objects, for example gh api "repos/numpy/numpy/pulls?state=all&per_page=100" > prs.json: import json, timeit, msgpack packed = msgpack.packb(json.load(open("prs.json"))) print(min(timeit.repeat(lambda: msgpack.unpackb(packed), number=3, repeat=7)) / 3) How to test make cython && python setup.py build_ext -i -f pytest test && MSGPACK_PUREPYTHON=1 pytest test The new tests in test/test_unpack.py also pass on current main, so they pin today's behavior: repeated keys (equal and interned), Unpacker across objects, raw=True, more distinct keys than cache slots, the empty key, and the error paths with the cache already warm (max_str_len, invalid UTF-8, reuse after an error). The last test checks that a key whose value never arrives is not interned. It caught a bug in my first version, which interned keys at decode time, when run on 3.13t. I ran the suite on 3.12 (extension and pure Python) and on 3.13t. A 3.13 tracemalloc loop over 2,000 rounds of unique keys grows exactly as much as on main. Risk The change is limited to the C extension's map-key path. The pure Python fallback does not change. Each unpack context gets a 2 KiB array, and the cache holds up to 256 key references until the unpackb() call returns or the Unpacker is reinitialized or freed. To revert, revert this commit.

Open Graph Description: This makes unpackb() about 25% faster on data where the same map keys repeat. The cost is about 80 lines of C and roughly 10 ns more per small message (+1.7% on a 9-key map). I know you chose plain...

X Description: This makes unpackb() about 25% faster on data where the same map keys repeat. The cost is about 80 lines of C and roughly 10 ns more per small message (+1.7% on a 9-key map). I know you chose plain...

Opengraph URL: https://github.com/msgpack/msgpack-python/pull/768

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.112.3

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:5ba72a9f-6d28-20f5-2222-d70118e911a2
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idCCDA:2046A0:2D2F47:3DE800:6B076232
html-safe-nonce2d7dab2888c36a830087341b7375128924e5519681a3bb8d24f9ca6567c10814
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0RBOjIwNDZBMDoyRDJGNDc6M0RFODAwOjZCMDc2MjMyIiwidmlzaXRvcl9pZCI6IjcxMDIyMzU5MTkwNjczNDEzNjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacde9dbf14b520380612dcc4c7396b6407905a6c0c3d885b1354684177e05ebf50
hovercard-subject-tagpull_request:5148453127
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/msgpack/msgpack-python/pull/768/files
twitter:imagehttps://avatars.githubusercontent.com/u/62109266?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/62109266?s=400&v=4
og:image:altThis makes unpackb() about 25% faster on data where the same map keys repeat. The cost is about 80 lines of C and roughly 10 ns more per small message (+1.7% on a 9-key map). I know you chose plain...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None7a557ecc960a3ae6e299fc66b0bc9132072f8860191648e6749fee5d3ff00d82
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/msgpack/msgpack-python git https://github.com/msgpack/msgpack-python.git
octolytics-dimension-user_id198264
octolytics-dimension-user_loginmsgpack
octolytics-dimension-repository_id2242705
octolytics-dimension-repository_nwomsgpack/msgpack-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id2242705
octolytics-dimension-repository_network_root_nwomsgpack/msgpack-python
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasebd845eb7947c1ed8c850f7ab415a51339d162de3
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/msgpack/msgpack-python/pull/768/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmsgpack%2Fmsgpack-python%2Fpull%2F768%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmsgpack%2Fmsgpack-python%2Fpull%2F768%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=msgpack%2Fmsgpack-python
Reloadhttps://github.com/msgpack/msgpack-python/pull/768/files
Reloadhttps://github.com/msgpack/msgpack-python/pull/768/files
Reloadhttps://github.com/msgpack/msgpack-python/pull/768/files
msgpackhttps://github.com/msgpack
msgpack-pythonhttps://github.com/msgpack/msgpack-python
Codehttps://github.com/msgpack/msgpack-python
Issues5 (5)https://github.com/msgpack/msgpack-python/issues
Pull requests21 (21)https://github.com/msgpack/msgpack-python/pulls
Discussionshttps://github.com/msgpack/msgpack-python/discussions
Actionshttps://github.com/msgpack/msgpack-python/actions
Security and quality2 (2)https://github.com/msgpack/msgpack-python/security
Insightshttps://github.com/msgpack/msgpack-python/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fmsgpack%2Fmsgpack-python%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fmsgpack%2Fmsgpack-python%2Fissues%2Fnew%2Fchoose
sebastianbreguelhttps://github.com/sebastianbreguel
msgpack:mainhttps://github.com/msgpack/msgpack-python/tree/main
sebastianbreguel:perf/map-key-cachehttps://github.com/sebastianbreguel/msgpack-python/tree/perf/map-key-cache
Conversation 0 https://github.com/msgpack/msgpack-python/pull/768
Commits 1 https://github.com/msgpack/msgpack-python/pull/768/commits
Checks 50 https://github.com/msgpack/msgpack-python/pull/768/checks
Files changed https://github.com/msgpack/msgpack-python/pull/768/files
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/768/files
Cache decoded map keys in the C unpacker https://github.com/msgpack/msgpack-python/pull/768/files#top
Show all changes 1 commit https://github.com/msgpack/msgpack-python/pull/768/files
8fa9e09 Cache decoded map keys in the C unpacker sebastianbreguel Nov 4, 2026 https://github.com/msgpack/msgpack-python/pull/768/commits/8fa9e09fd6be11a04f0a92b2f546c1fde592715d
Clear filters https://github.com/msgpack/msgpack-python/pull/768/files
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/768/files
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/768/files
_unpacker.pyx https://github.com/msgpack/msgpack-python/pull/768/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
unpack.h https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
unpack_template.h https://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
test_unpack.py https://github.com/msgpack/msgpack-python/pull/768/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
msgpack/_unpacker.pyxhttps://github.com/msgpack/msgpack-python/pull/768/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
View file https://github.com/sebastianbreguel/msgpack-python/blob/8fa9e09fd6be11a04f0a92b2f546c1fde592715d/msgpack/_unpacker.pyx
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/msgpack/msgpack-python/pull/768/{{ revealButtonHref }}
https://github.com/msgpack/msgpack-python/pull/768/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
https://github.com/msgpack/msgpack-python/pull/768/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
https://github.com/msgpack/msgpack-python/pull/768/files#diff-407b1aba76cd8909a978c41341f7fe756567d21c6dab7c7568ca6075614531b1
msgpack/unpack.hhttps://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
View file https://github.com/sebastianbreguel/msgpack-python/blob/8fa9e09fd6be11a04f0a92b2f546c1fde592715d/msgpack/unpack.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/msgpack/msgpack-python/pull/768/{{ revealButtonHref }}
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
https://github.com/msgpack/msgpack-python/pull/768/files#diff-167e9083fc109195a2c1c50f75abecb64b50c3b085af5292ceb76a3cd85f97d3
msgpack/unpack_template.hhttps://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
View file https://github.com/sebastianbreguel/msgpack-python/blob/8fa9e09fd6be11a04f0a92b2f546c1fde592715d/msgpack/unpack_template.h
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/msgpack/msgpack-python/pull/768/{{ revealButtonHref }}
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a35fa6234add31e8d66397bdedad27eebc3da5e2a98acb6b5c6ad02acb7a3ae4
test/test_unpack.pyhttps://github.com/msgpack/msgpack-python/pull/768/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
View file https://github.com/sebastianbreguel/msgpack-python/blob/8fa9e09fd6be11a04f0a92b2f546c1fde592715d/test/test_unpack.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/msgpack/msgpack-python/pull/768/{{ revealButtonHref }}
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
https://github.com/msgpack/msgpack-python/pull/768/files#diff-a38bda78674f20f297abfaafa9134ac6025345225b79c4c0012aa592740c2c7d
Please reload this pagehttps://github.com/msgpack/msgpack-python/pull/768/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlno-cache

Viewport: width=device-width


URLs of crawlers that visited me.