Title: Use `PyObject_CallMethodOneArg` in datetime `call_tzinfo_method` · Issue #151059 · python/cpython · GitHub
Open Graph Title: Use `PyObject_CallMethodOneArg` in datetime `call_tzinfo_method` · Issue #151059 · python/cpython
X Title: Use `PyObject_CallMethodOneArg` in datetime `call_tzinfo_method` · Issue #151059 · python/cpython
Description: Feature or enhancement Proposal: The call_tzinfo_method() (invoked from tzinfo.utcoffset() and tzinfo.dst()) use: offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); On every call this parses the "O" format string, builds a tempo...
Open Graph Description: Feature or enhancement Proposal: The call_tzinfo_method() (invoked from tzinfo.utcoffset() and tzinfo.dst()) use: offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); On every call this pars...
X Description: Feature or enhancement Proposal: The call_tzinfo_method() (invoked from tzinfo.utcoffset() and tzinfo.dst()) use: offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); On every call...
Opengraph URL: https://github.com/python/cpython/issues/151059
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use `PyObject_CallMethodOneArg` in datetime `call_tzinfo_method`","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nThe `call_tzinfo_method()` (invoked from `tzinfo.utcoffset()` and `tzinfo.dst()`) use:\n\n```c\noffset = PyObject_CallMethod(tzinfo, name, \"O\", tzinfoarg);\n```\n\nOn every call this parses the `\"O\"` format string, builds a temporary argument tuple, and rebuilds the method-name `str` (`PyObject_GetAttrString`). Switching to `PyObject_CallMethodOneArg` with interned strings improves performance.\n\n### Benchmarks\n\nWall-clock, PGO+LTO build\n\n| operation | before | after | speedup |\n|---|---|---|---|\n| `astimezone()` UTC→local | 225.3 ns | 124.3 ns | **1.81×** |\n| `timestamp()` | 461.5 ns | 270.2 ns | **1.71×** |\n| `isoformat()` (aware) | 667.2 ns | 542.1 ns | **1.23×** |\n| `utcoffset()` (direct) | 116.6 ns | 48.4 ns | **2.41×** |\n| `now().isoformat()` (control) | 664.8 ns | 681.8 ns | 0.98× |\n| `tzname()` (control) | 48.8 ns | 48.3 ns | 1.01× |\n\n\u003cdetails>\n\u003csummary>Benchmark script\u003c/summary>\n\n```python\n\"\"\"Microbenchmark for datetime call_tzinfo_method (gh-150942).\"\"\"\nimport datetime as dt\nimport time\nfrom datetime import timezone\nfrom zoneinfo import ZoneInfo\n\nny = ZoneInfo(\"America/New_York\")\nutc_dt = dt.datetime(2024, 6, 7, 16, 30, tzinfo=timezone.utc)\nny_dt = dt.datetime(2024, 6, 7, 12, 30, tzinfo=ny)\n\n\ndef bench(fn, N=1_000_000, repeat=7):\n best = float(\"inf\")\n for _ in range(repeat):\n start = time.perf_counter()\n for _ in range(N):\n fn()\n best = min(best, (time.perf_counter() - start) / N * 1e9)\n return best\n\n\nCASES = {\n \"astimezone() UTC->local\": lambda: utc_dt.astimezone(ny),\n \"timestamp()\": lambda: ny_dt.timestamp(),\n \"isoformat()\": lambda: ny_dt.isoformat(),\n \"utcoffset()\": lambda: ny_dt.utcoffset(),\n \"now().isoformat()\": lambda: dt.datetime.now().isoformat(), # naive: control\n \"tzname() [control]\": lambda: ny_dt.tzname(),\n}\n\nprint(f\"{'operation':\u003c26}{'ns/op':>10}\")\nprint(\"-\" * 36)\nfor name, fn in CASES.items():\n print(f\"{name:\u003c26}{bench(fn):>9.1f}\")\n```\n\u003c/details>\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-151062\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/eendebakpt","@type":"Person","name":"eendebakpt"},"datePublished":"2026-06-07T20:16:45Z","url":"https://github.com/python/cpython/issues/151059"}
| 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:2e444501-b3e3-c6b4-c73f-9fcc0027fe22 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B044:CE438:1326893:198D5EA:6B0203A6 |
| html-safe-nonce | 0813c9edb1a14e3e9beeab365c337e551c70816af3ed7e2a204f62ade5ca44fc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDQ0OkNFNDM4OjEzMjY4OTM6MTk4RDVFQTo2QjAyMDNBNiIsInZpc2l0b3JfaWQiOiI3Njc0ODI2NjIyMzY4ODc1NDMxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | bdd7ea011a76d7c4b2c0d5160921bac4998fc37a1eb8a4ee9b25cb3892f4feb2 |
| hovercard-subject-tag | issue:4608592634 |
| 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/151059/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0bffb5bd0ddb07a3317c6eded31b64be2b8faae0a9720f739f3eee2a5b0d15a9/python/cpython/issues/151059 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0bffb5bd0ddb07a3317c6eded31b64be2b8faae0a9720f739f3eee2a5b0d15a9/python/cpython/issues/151059 |
| og:image:alt | Feature or enhancement Proposal: The call_tzinfo_method() (invoked from tzinfo.utcoffset() and tzinfo.dst()) use: offset = PyObject_CallMethod(tzinfo, name, "O", tzinfoarg); On every call this pars... |
| 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