René's URL Explorer Experiment


Title: gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only) by eendebakpt · Pull Request #146397 · python/cpython · GitHub

Open Graph Title: gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only) by eendebakpt · Pull Request #146397 · python/cpython

X Title: gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only) by eendebakpt · Pull Request #146397 · python/cpython

Description: We optimize float divisions for the case where one of the operands is a unique reference. This is similar to #146307, but with a guard for division by zero. We do not add opcodes in tier 1 For tier 2 we can specialize for the case when one of the operations is a unique reference and for the case when there are no unique references. The case _BINARY_TRUEDIV_FLOAT where there are no unique references (or we miss information about the uniqueness) has no performance improvement in itself, but is to propagate types better. This opcode has guards, so that even with input from locals the type is propagated. In tier 2 we now return more results marked as unique. Micro-benchmarks (min of 3 runs, 2M iterations) update benchmark no longer valid (see a new one below) Pattern main (ns/iter) branch (ns/iter) Speedup Notes (a+b) * c 10.8 10.9 -- baseline (multiply, already optimized) (a+b) + (c+d) 18.0 18.1 -- baseline (add, already optimized) a / b 20.6 10.8 1.9x speculative guards + truediv specialization (a+b) / c 26.4 11.0 2.4x inplace LHS, guard inserted for c (2.0+x) / y 25.1 10.9 2.3x inplace LHS, guard inserted for y c / (a+b) 26.0 11.2 2.3x inplace RHS, guard inserted for c (a/b) / (c/d) 41.3 19.1 2.2x speculative guards enable inplace chain (a/b) + (c/d) 29.1 19.0 1.5x speculative guards enable inplace add All patterns are total += in a tight loop. Benchmark script """Benchmark for float true division tier 2 specialization. Usage: ./python bench_truediv.py """ import timeit N = 2_000_000 INNER = 1000 def bench(label, fn): iters = N // INNER times = [timeit.timeit(fn, number=iters) for _ in range(3)] t = min(times) print(f" {label}: {t/N*1e9:.1f} ns/iter") def f_chain_mul(n, a, b, c): t = 0.0 for i in range(n): t += (a + b) * c return t def f_div(n, a, b): t = 0.0 for i in range(n): t += a / b return t def f_chain_div(n, a, b, c): t = 0.0 for i in range(n): t += (a + b) / c return t def f_2px_div_y(n, x, y): t = 0.0 for i in range(n): t += (2.0 + x) / y return t def f_div_rhs(n, a, b, c): t = 0.0 for i in range(n): t += c / (a + b) return t def f_ab_div_cd(n, a, b, c, d): t = 0.0 for i in range(n): t += (a / b) / (c / d) return t def f_ab_add_cd(n, a, b, c, d): t = 0.0 for i in range(n): t += (a / b) + (c / d) return t def f_add_chain(n, a, b, c, d): t = 0.0 for i in range(n): t += (a + b) + (c + d) return t # Warmup f_chain_mul(10000, 2.0, 3.0, 4.0) f_div(10000, 10.0, 3.0) f_chain_div(10000, 2.0, 3.0, 4.0) f_2px_div_y(10000, 3.0, 4.0) f_div_rhs(10000, 2.0, 3.0, 4.0) f_ab_div_cd(10000, 10.0, 3.0, 4.0, 5.0) f_ab_add_cd(10000, 10.0, 3.0, 4.0, 5.0) f_add_chain(10000, 1.0, 2.0, 3.0, 4.0) print("Float truediv benchmark (min of 3 runs):") bench("(a+b) * c (baseline) ", lambda: f_chain_mul(INNER, 2.0, 3.0, 4.0)) bench("(a+b) + (c+d) (baseline) ", lambda: f_add_chain(INNER, 1.0, 2.0, 3.0, 4.0)) bench("a / b (spec div) ", lambda: f_div(INNER, 10.0, 3.0)) bench("(a+b) / c (inplace L)", lambda: f_chain_div(INNER, 2.0, 3.0, 4.0)) bench("(2.0+x) / y (inplace L)", lambda: f_2px_div_y(INNER, 3.0, 4.0)) bench("c / (a+b) (inplace R)", lambda: f_div_rhs(INNER, 2.0, 3.0, 4.0)) bench("(a/b) / (c/d) (spec div) ", lambda: f_ab_div_cd(INNER, 10.0, 3.0, 4.0, 5.0)) bench("(a/b) + (c/d) (spec div) ", lambda: f_ab_add_cd(INNER, 10.0, 3.0, 4.0, 5.0)) Analysis The inplace truediv kicks in when at least one operand is a uniquely-referenced float (e.g. the result of a prior add/multiply). The optimizer emits _BINARY_OP_TRUEDIV_FLOAT_INPLACE or _INPLACE_RIGHT, saving one PyFloat_FromDouble allocation + deallocation per iteration. The optimization works well for several cases. For some (e.g. (a/b) + (c/d) ) the performance gain is not due to an inplace division, but by better type propagation allowing the + to be specialized inplace. The a / b is also faster because of better type propagation and a += in the test script. In typical code intermediate results are often stored in local variables. For these cases it is important pick up (speculative) type information as soon as possible. Issue: gh-146393

Open Graph Description: We optimize float divisions for the case where one of the operands is a unique reference. This is similar to #146307, but with a guard for division by zero. We do not add opcodes in tier 1 For tie...

X Description: We optimize float divisions for the case where one of the operands is a unique reference. This is similar to #146307, but with a guard for division by zero. We do not add opcodes in tier 1 For tie...

Opengraph URL: https://github.com/python/cpython/pull/146397

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:30616343-bbac-efb9-d41f-fd431cac4ae7
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idECAC:69D3C:15AEFE1:1C9F9E7:6A51C122
html-safe-nonce91ea00906b0978de158792305d11244a2addf8bb195e33bf94085f9639686b7b
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQ0FDOjY5RDNDOjE1QUVGRTE6MUM5RjlFNzo2QTUxQzEyMiIsInZpc2l0b3JfaWQiOiI0OTIxODYzMDUyMTgwMTc3MTg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace2d22bd27bd930da90e6b772b76689482c830c10bb4e5822bc3605ca34dbb6e1
hovercard-subject-tagpull_request:3441935222
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/python/cpython/pull/146397/files
twitter:imagehttps://avatars.githubusercontent.com/u/883786?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/883786?s=400&v=4
og:image:altWe optimize float divisions for the case where one of the operands is a unique reference. This is similar to #146307, but with a guard for division by zero. We do not add opcodes in tier 1 For tie...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
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
release7aed05249554b889eb33d002851a973eebcc7e91
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/146397/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F146397%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F146397%2Ffiles
Sign up https://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=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/146397/files
Reloadhttps://github.com/python/cpython/pull/146397/files
Reloadhttps://github.com/python/cpython/pull/146397/files
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.7k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
Fidget-Spinnerhttps://github.com/Fidget-Spinner
python:mainhttps://github.com/python/cpython/tree/main
eendebakpt:jit_float_truedivhttps://github.com/eendebakpt/cpython/tree/jit_float_truediv
Conversation 18 https://github.com/python/cpython/pull/146397
Commits 25 https://github.com/python/cpython/pull/146397/commits
Checks 75 https://github.com/python/cpython/pull/146397/checks
Files changed https://github.com/python/cpython/pull/146397/files
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only) https://github.com/python/cpython/pull/146397/files#top
Show all changes 25 commits https://github.com/python/cpython/pull/146397/files
c846269 gh-146306: Specialize float/float true division in tier 2 optimizer eendebakpt Mar 24, 2026 https://github.com/python/cpython/pull/146397/commits/c846269e1038a4d3cb9a4b88754db9a1cfa0b711
97889f5 Mark results of float-producing _BINARY_OP as unique eendebakpt Mar 24, 2026 https://github.com/python/cpython/pull/146397/commits/97889f5f3deda26adb1a8eecbc62d3843dccc864
67869e4 Fix truediv type propagation for non-numeric types eendebakpt Mar 25, 2026 https://github.com/python/cpython/pull/146397/commits/67869e4622a78ae5849064ff2662b52307f3c382
5c4e3bf add guards eendebakpt Mar 25, 2026 https://github.com/python/cpython/pull/146397/commits/5c4e3bf6a0c6cede6f848c85618be4842e006754
228bfa9 news entry eendebakpt Mar 25, 2026 https://github.com/python/cpython/pull/146397/commits/228bfa9a16e1d45a5cef6d2bdaa5847e13bedcc0
8bf12bf Merge branch 'main' into jit_float_truediv eendebakpt Mar 25, 2026 https://github.com/python/cpython/pull/146397/commits/8bf12bf68666344f46c72323e3538d4a5940be56
46c241e Merge remote-tracking branch 'upstream/main' into pr/146397 Fidget-Spinner Mar 29, 2026 https://github.com/python/cpython/pull/146397/commits/46c241ea33079a4c06e631171d9c58ef00a362a2
53dd383 avoid speculative guards eendebakpt Mar 29, 2026 https://github.com/python/cpython/pull/146397/commits/53dd383c7da542915893d40b7d4f266e3e2b5039
44ee7f0 fix test eendebakpt Mar 29, 2026 https://github.com/python/cpython/pull/146397/commits/44ee7f07ae9c9c7bbc8569b1873a1e09f8aa0dff
e302112 Merge branch 'main' into jit_float_truediv eendebakpt Apr 3, 2026 https://github.com/python/cpython/pull/146397/commits/e302112619de9286151df35e9da802e59ec2bfdb
d2c03bc Regenerate pycore_uop_ids.h and pycore_uop_metadata.h with truediv fl… eendebakpt Apr 3, 2026 https://github.com/python/cpython/pull/146397/commits/d2c03bced625e3f837e96b3ccb1ead36801d9a50
dba0bb3 Merge branch 'main' into jit_float_truediv eendebakpt Apr 3, 2026 https://github.com/python/cpython/pull/146397/commits/dba0bb334a93fd2d55d6774151443731ca4c7b01
8615577 Merge branch 'main' into jit_float_truediv eendebakpt Apr 4, 2026 https://github.com/python/cpython/pull/146397/commits/8615577080c447199833dbedf4adb762928d30ac
e715dac review comments eendebakpt Apr 5, 2026 https://github.com/python/cpython/pull/146397/commits/e715dac12d6f5e8ec927f758f7d32696a8125b91
925841c Merge branch 'main' into jit_float_truediv eendebakpt Apr 5, 2026 https://github.com/python/cpython/pull/146397/commits/925841c61785ada2a0bfb47ed38b0bc9aef625de
43f4987 mark more returns as unique eendebakpt Apr 6, 2026 https://github.com/python/cpython/pull/146397/commits/43f4987c77e2e4e675e9034ee67475d6a169214f
a49018e Merge branch 'main' into jit_float_truediv eendebakpt Apr 6, 2026 https://github.com/python/cpython/pull/146397/commits/a49018ea282f84ec09ee450bbd2c5f596d9b6f46
9cab900 mark more returns as unique eendebakpt Apr 6, 2026 https://github.com/python/cpython/pull/146397/commits/9cab90075e698c6ad5cc1a854cbe506411703f75
b432e1a Merge branch 'main' into jit_float_truediv eendebakpt Apr 11, 2026 https://github.com/python/cpython/pull/146397/commits/b432e1a0509d8450c1e1c62d7a91fcc7b5383b83
0c89654 Merge branch 'main' into jit_float_truediv eendebakpt Apr 12, 2026 https://github.com/python/cpython/pull/146397/commits/0c8965401d41e2abdba348cb631da8f466afdc3f
fa97ab3 Merge branch 'main' into jit_float_truediv eendebakpt Apr 14, 2026 https://github.com/python/cpython/pull/146397/commits/fa97ab307c2ae2d97d41e4ef2d6517380f11adeb
3ba0c85 add back recording ops and guards eendebakpt Apr 14, 2026 https://github.com/python/cpython/pull/146397/commits/3ba0c85add2009c850f5f65a2b42d2b62ed4e56f
e7fdb04 simplify eendebakpt Apr 14, 2026 https://github.com/python/cpython/pull/146397/commits/e7fdb0458c9b56ef34d0d96147ef1b890d3c74e0
44666f7 Merge remote-tracking branch 'upstream/main' into pr/146397 Fidget-Spinner Apr 14, 2026 https://github.com/python/cpython/pull/146397/commits/44666f7b3f03936e0a248af9aad63f8e92e9d6f8
9a8c143 Merge branch 'jit_float_truediv' of github.com:eendebakpt/cpython int… Fidget-Spinner Apr 14, 2026 https://github.com/python/cpython/pull/146397/commits/9a8c1436cddef605df6ec397311acd27f805d75b
Clear filters https://github.com/python/cpython/pull/146397/files
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
pycore_opcode_metadata.h https://github.com/python/cpython/pull/146397/files#diff-72bc4c0445516cc3eabd536220b2459617491ffaabbdb8a7c5381f36c991e378
pycore_uop_ids.h https://github.com/python/cpython/pull/146397/files#diff-2a420e4d0d58009249c0f73f7c822167b053bd51cb213b20179bd2aebfc66ff9
pycore_uop_metadata.h https://github.com/python/cpython/pull/146397/files#diff-55e65a72fa31e9b746e03076d2c74b716c0c1171663f36960b2339fcbfae9bab
test_opt.py https://github.com/python/cpython/pull/146397/files#diff-67b5bd915e37f77df79907865982648172c27d3b96fbb072b30a73c6a2735439
2026-03-25-12-00-00.gh-issue-146306.B9f62e.rst https://github.com/python/cpython/pull/146397/files#diff-acb897482a80f293c5080bb50fa6d7c63740d2e9558fccb09699f28eaa07ec31
test_cases.c.h https://github.com/python/cpython/pull/146397/files#diff-2852a58ed53ab1473b74dbdc4165885a9b74865b6d71e48fa4813d6289fa1838
bytecodes.c https://github.com/python/cpython/pull/146397/files#diff-729a985b0cb8b431cb291f1edb561bbbfea22e3f8c262451cd83328a0936a342
ceval_macros.h https://github.com/python/cpython/pull/146397/files#diff-45baf725df91ed7826458cda8a17c2b4a2e5296504de1ed6a1c5a9ebe6390a47
executor_cases.c.h https://github.com/python/cpython/pull/146397/files#diff-a1c3430c42a66c16e6d2e154491a6da993b187b27c628545a12a02c7b2a06b81
generated_cases.c.h https://github.com/python/cpython/pull/146397/files#diff-4ef46fa654f95502e49a24f7dc8ee31a4cac9b3433fe9cd2b2d4dd78cfbad448
optimizer_bytecodes.c https://github.com/python/cpython/pull/146397/files#diff-e5bd2b14b0b10f0f47786e26306d689ed1361c3dc3b11dcc3ea52b8a2422ff64
optimizer_cases.c.h https://github.com/python/cpython/pull/146397/files#diff-ed4567b3e67709ec57bf4df91ef058cfcbe00d7da11cd2a75e38241fa1ca99d2
record_functions.c.h https://github.com/python/cpython/pull/146397/files#diff-ac2bf70082a97de53db16bbb949b29240f59f89c4b9ad93928b3926738cbb74c
Include/internal/pycore_opcode_metadata.hhttps://github.com/python/cpython/pull/146397/files#diff-72bc4c0445516cc3eabd536220b2459617491ffaabbdb8a7c5381f36c991e378
View file https://github.com/eendebakpt/cpython/blob/9a8c1436cddef605df6ec397311acd27f805d75b/Include/internal/pycore_opcode_metadata.h
Open in desktop https://desktop.github.com
how customized files appear on GitHubhttps://docs.github.com/github/administering-a-repository/customizing-how-changed-files-appear-on-github
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
Please reload this pagehttps://github.com/python/cpython/pull/146397/files
Please reload this pagehttps://github.com/python/cpython/pull/146397/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

Viewport: width=device-width


URLs of crawlers that visited me.