René's URL Explorer Experiment


Title: Speed up PyObject_CallMethod by avoiding the temporary bound method · Issue #151157 · python/cpython · GitHub

Open Graph Title: Speed up PyObject_CallMethod by avoiding the temporary bound method · Issue #151157 · python/cpython

X Title: Speed up PyObject_CallMethod by avoiding the temporary bound method · Issue #151157 · python/cpython

Description: Feature or enhancement Proposal: PyObject_CallMethod() (and siblings _PyObject_CallMethod(), PyEval_CallMethod(), _PyObject_CallMethodId() and _PyObject_CallMethod_SizeT()) look the method up with PyObject_GetAttr(), which materialises a...

Open Graph Description: Feature or enhancement Proposal: PyObject_CallMethod() (and siblings _PyObject_CallMethod(), PyEval_CallMethod(), _PyObject_CallMethodId() and _PyObject_CallMethod_SizeT()) look the method up with ...

X Description: Feature or enhancement Proposal: PyObject_CallMethod() (and siblings _PyObject_CallMethod(), PyEval_CallMethod(), _PyObject_CallMethodId() and _PyObject_CallMethod_SizeT()) look the method up with ...

Opengraph URL: https://github.com/python/cpython/issues/151157

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Speed up PyObject_CallMethod by avoiding the temporary bound method","articleBody":"# Feature or enhancement\n\n### Proposal:\n\n\n`PyObject_CallMethod()` (and siblings `_PyObject_CallMethod()`, `PyEval_CallMethod()`, `_PyObject_CallMethodId()` and `_PyObject_CallMethod_SizeT()`) look the method up with `PyObject_GetAttr()`, which materialises a temporary bound-method object (`PyMethodObject`) on every call, then call it and throw it away.\n\nThe interpreter itself avoids this for `obj.meth(...)` by using `_PyObject_GetMethod()` (the `LOAD_METHOD` lookup), which returns the unbound function plus a flag and calls it with `self` prepended. \n\nIn `PyObject_CallMethod()` we can resolve the method with `_PyObject_GetMethod()` and call it directly\n(`_PyObject_VectorcallPrepend` when unbound). The now-unused `callmethod()` and `_PyObject_CallMethodFormat()` helpers are removed.\n\n| benchmark | main | patch | speedup |\n| --- | ---: | ---: | ---: |\n| `CallMethod` / C method `tzinfo.utcoffset(dt)` | 116 ns | 102 ns | 1.14x |\n| `CallMethod` / C method `{}.get(k)` | 116 ns | 109 ns | 1.06x |\n| `CallMethod` / C method `\"..\".count(x)` | 131 ns | 122 ns | 1.07x |\n| `CallMethod` / py method `obj.m(x)` | 118 ns | 97 ns | 1.21x |\n| `CallMethod` / no args `(10**6).bit_length()` | 104 ns | 81 ns | 1.27x |\n| `_PyObject_CallMethod` (interned name) `tzinfo.utcoffset(dt)` | 43.7 ns | 39.4 ns | 1.11x |\n| `io.readline`  | 1.15 us | 1.05 us | 1.09x |\n| `bisect.insort` (list subclass) | 465 ns | 452 ns | 1.03x |\n| **Geometric mean** | | | **1.12x** |\n\nThe `CallMethod` rows are isolated per-call measurements via a small `_testinternalcapi` harness; `io.readline` and `bisect.insort` are pure-Python paths that reach the C-API indirectly. The performance can also impact C-extensions that use the `PyObject_CallMethod` interface.\n\n\u003cdetails>\n\u003csummary>Benchmark script (pyperf)\u003c/summary>\n\n```python\n\"\"\"Combined pyperf benchmark for the PyObject_CallMethod optimization.\n\nPyObject_CallMethod is a C-API entry point with no direct Python equivalent\n(`obj.m()` compiles to LOAD_METHOD/CALL), so the per-call cost is measured two\nways:\n\n* Isolated: via the _testinternalcapi.bench_callmethod harness, which loops the\n  raw C-API call.  Each measured op is exactly one CallMethod-family call.\n  (Harness is a local build artifact; these cases are skipped if absent.)\n* End-to-end: io's IOBase.readline() calls _PyObject_CallMethod(self, read, 1)\n  once per byte, so reading a line is a pure-Python path that hits it ~2000x.\n\nRun:\n    python bench_callmethod_pyperf.py -o new.json\n    python bench_callmethod_pyperf.py --compare-to old.json\n\"\"\"\nimport datetime\nimport io\nimport pyperf\n\ntry:\n    import _testinternalcapi\n    _bench = _testinternalcapi.bench_callmethod\nexcept (ImportError, AttributeError):\n    _bench = None\n\n# --- data for the isolated cases ---\ntz = datetime.timezone(datetime.timedelta(hours=5, minutes=30))\ndt = datetime.datetime(2024, 6, 1, 12, 0, tzinfo=tz)\n\nclass PyObj:\n    def m(self, x):\n        return x\n\n# (label, obj, name, arg, mode)   mode 0=PyObject_CallMethod \"O\",\n#                                 2=PyObject_CallMethod NULL, 3=_PyObject_CallMethod\nISOLATED = [\n    (\"CallMethod / C method  tzinfo.utcoffset(dt)\", tz, \"utcoffset\", dt, 0),\n    (\"CallMethod / C method  {}.get(k)\",            {}, \"get\", \"k\", 0),\n    (\"CallMethod / C method  '..'.count(x)\",        \"hello world\", \"count\", \"l\", 0),\n    (\"CallMethod / py method obj.m(x)\",             PyObj(), \"m\", 5, 0),\n    (\"CallMethod / no args   (10**6).bit_length()\", 10 ** 6, \"bit_length\", 0, 2),\n    (\"_PyObject_CallMethod   tzinfo.utcoffset(dt)\", tz, \"utcoffset\", dt, 3),\n]\n\ndef make_iso(obj, name, arg, mode):\n    def run(loops):\n        t0 = pyperf.perf_counter()\n        _bench(obj, name, arg, loops, mode)\n        return pyperf.perf_counter() - t0\n    return run\n\n# --- end-to-end io case (pure Python, no harness needed) ---\nLINE_LEN = 2000\n\nclass Raw(io.RawIOBase):\n    def __init__(self, data):\n        self.data = data\n        self.pos = 0\n    def readable(self):\n        return True\n    def readinto(self, b):\n        if self.pos >= len(self.data):\n            return 0\n        k = min(len(b), len(self.data) - self.pos)\n        b[:k] = self.data[self.pos:self.pos + k]\n        self.pos += k\n        return k\n\ndef bench_io(loops):\n    line = b\"a\" * LINE_LEN + b\"\\n\"\n    r = Raw(line)\n    t0 = pyperf.perf_counter()\n    for _ in range(loops):\n        r.pos = 0\n        r.readline()\n    return pyperf.perf_counter() - t0\n\n# A second, different pure-Python path: bisect.insort on a list *subclass* goes\n# through _PyObject_CallMethod(a, \"insert\", \"nO\", index, x) -- exact lists take a\n# PyList_Insert fast path that bypasses the call.  (Distinct \"nO\" / 2-arg path.)\nimport bisect\n\nclass SubList(list):\n    pass\n\ndef bench_insort(loops):\n    a = SubList(range(0, 32, 2))   # small even list, keeps the insert shift cheap\n    t0 = pyperf.perf_counter()\n    for _ in range(loops):\n        bisect.insort_right(a, 15)        # -> _PyObject_CallMethod(a, \"insert\", ...)\n        del a[bisect.bisect_left(a, 15)]  # remove it again to keep 'a' stable\n    return pyperf.perf_counter() - t0\n\nif __name__ == \"__main__\":\n    runner = pyperf.Runner()\n    if _bench is not None:\n        for label, obj, name, arg, mode in ISOLATED:\n            runner.bench_time_func(label, make_iso(obj, name, arg, mode))\n    runner.bench_time_func(\"io.readline (per _PyObject_CallMethod)\",\n                           bench_io, inner_loops=LINE_LEN)\n    runner.bench_time_func(\"bisect.insort (list subclass, _PyObject_CallMethod)\",\n                           bench_insort)\n```\n\n\u003c/details>\n\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-151158\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2026-06-09T13:31:45Z","url":"https://github.com/python/cpython/issues/151157"}

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/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:fbb330b6-1f19-54ca-bcd7-276fc57cdc7b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAB8E:172B92:35ADF6D:47818EF:6B0207DB
html-safe-nonce573fb7243f88c26ed20ba3a35dbed6817f70e091ee8be66947897a02200a9ecd
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjhFOjE3MkI5MjozNUFERjZEOjQ3ODE4RUY6NkIwMjA3REIiLCJ2aXNpdG9yX2lkIjoiNjY2MTQxMTQ3NTIxNjc5NTYxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmaceb813715b4c914653eac80e05c17f1128421b2aacc343c7ad3ed88246dce714e
hovercard-subject-tagissue:4622423212
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/151157/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3928ea1fc207ec96d676ad5141e0911f0d136491f297a46417ac801f1b518a77/python/cpython/issues/151157
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3928ea1fc207ec96d676ad5141e0911f0d136491f297a46417ac801f1b518a77/python/cpython/issues/151157
og:image:altFeature or enhancement Proposal: PyObject_CallMethod() (and siblings _PyObject_CallMethod(), PyEval_CallMethod(), _PyObject_CallMethodId() and _PyObject_CallMethod_SizeT()) look the method up with ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameeendebakpt
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
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
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/151157#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F151157
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%2Fpython%2Fcpython%2Fissues%2F151157
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/151157
Reloadhttps://github.com/python/cpython/issues/151157
Reloadhttps://github.com/python/cpython/issues/151157
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/151157
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/151157
Speed up PyObject_CallMethod by avoiding the temporary bound methodhttps://github.com/python/cpython/issues/151157#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/eendebakpt
eendebakpthttps://github.com/eendebakpt
on Jun 9, 2026https://github.com/python/cpython/issues/151157#issue-4622423212
gh-151157: Speed up PyObject_CallMethod via _PyObject_GetMethod #151158https://github.com/python/cpython/pull/151158
eendebakpthttps://github.com/eendebakpt
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
on Jun 9, 2026https://github.com/python/cpython/issues/151157#event-26525255363
serhiy-storchakahttps://github.com/serhiy-storchaka
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
on Jul 3, 2026https://github.com/python/cpython/issues/151157#event-27545876730
eendebakpthttps://github.com/eendebakpt
on Oct 5, 2026https://github.com/python/cpython/issues/151157#event-32536478283
gh-151157: Speed up PyObject_CallMethod via _PyObject_GetMethod (#151158https://github.com/python/cpython/commit/3f9118f1d17ae2ff0e25bd07fce9940ba13afdff
gh-151157https://github.com/python/cpython/issues/151157
#151158https://github.com/python/cpython/pull/151158
3f9118fhttps://github.com/python/cpython/commit/3f9118f1d17ae2ff0e25bd07fce9940ba13afdff
eendebakpthttps://github.com/eendebakpt
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Oct 5, 2026https://github.com/python/cpython/issues/151157#event-32548019585
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/151157
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/151157
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
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.