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
Domain: github.com
{"@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-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:fbb330b6-1f19-54ca-bcd7-276fc57cdc7b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB8E:172B92:35ADF6D:47818EF:6B0207DB |
| html-safe-nonce | 573fb7243f88c26ed20ba3a35dbed6817f70e091ee8be66947897a02200a9ecd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjhFOjE3MkI5MjozNUFERjZEOjQ3ODE4RUY6NkIwMjA3REIiLCJ2aXNpdG9yX2lkIjoiNjY2MTQxMTQ3NTIxNjc5NTYxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eb813715b4c914653eac80e05c17f1128421b2aacc343c7ad3ed88246dce714e |
| hovercard-subject-tag | issue:4622423212 |
| 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/151157/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3928ea1fc207ec96d676ad5141e0911f0d136491f297a46417ac801f1b518a77/python/cpython/issues/151157 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3928ea1fc207ec96d676ad5141e0911f0d136491f297a46417ac801f1b518a77/python/cpython/issues/151157 |
| og:image:alt | Feature or enhancement Proposal: PyObject_CallMethod() (and siblings _PyObject_CallMethod(), PyEval_CallMethod(), _PyObject_CallMethodId() and _PyObject_CallMethod_SizeT()) look the method up with ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eendebakpt |
| 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