GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use `PyObject_CallMethodOneArg` in datetime `call_tzinfo_method` · Issue #151059 · python/cpython

Use PyObject_CallMethodOneArg in datetime call_tzinfo_method #151059

New issue
Copy link
New issue
Copy link
Closed
Closed
Use PyObject_CallMethodOneArg in datetime call_tzinfo_method#151059
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirperformancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

Description

@eendebakpt
eendebakpt
opened on Jun 7, 2026
Issue body actions

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 temporary argument tuple, and rebuilds the method-name str (PyObject_GetAttrString). Switching to PyObject_CallMethodOneArg with interned strings improves performance.

Benchmarks

Wall-clock, PGO+LTO build

operation before after speedup
astimezone() UTC→local 225.3 ns 124.3 ns 1.81×
timestamp() 461.5 ns 270.2 ns 1.71×
isoformat() (aware) 667.2 ns 542.1 ns 1.23×
utcoffset() (direct) 116.6 ns 48.4 ns 2.41×
now().isoformat() (control) 664.8 ns 681.8 ns 0.98×
tzname() (control) 48.8 ns 48.3 ns 1.01×
Benchmark script
"""Microbenchmark for datetime call_tzinfo_method (gh-150942)."""
import datetime as dt
import time
from datetime import timezone
from zoneinfo import ZoneInfo

ny = ZoneInfo("America/New_York")
utc_dt = dt.datetime(2024, 6, 7, 16, 30, tzinfo=timezone.utc)
ny_dt = dt.datetime(2024, 6, 7, 12, 30, tzinfo=ny)


def bench(fn, N=1_000_000, repeat=7):
    best = float("inf")
    for _ in range(repeat):
        start = time.perf_counter()
        for _ in range(N):
            fn()
        best = min(best, (time.perf_counter() - start) / N * 1e9)
    return best


CASES = {
    "astimezone() UTC->local": lambda: utc_dt.astimezone(ny),
    "timestamp()":             lambda: ny_dt.timestamp(),
    "isoformat()":             lambda: ny_dt.isoformat(),
    "utcoffset()":             lambda: ny_dt.utcoffset(),
    "now().isoformat()":       lambda: dt.datetime.now().isoformat(),  # naive: control
    "tzname()  [control]":     lambda: ny_dt.tzname(),
}

print(f"{'operation':<26}{'ns/op':>10}")
print("-" * 36)
for name, fn in CASES.items():
    print(f"{name:<26}{bench(fn):>9.1f}")

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-151059: Use PyObject_CallMethodOneArg in datetime call_tzinfo_method #151062

Activity

  1. eendebakpt
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jun 7, 2026
  2. eendebakpt
    added a commit that references this issue on Jun 7, 2026

    pythongh-151059: Use PyObject_CallMethodOneArg in datetime call_tzinf…

    26d5647
  3. eendebakpt
    added
    performancePerformance or resource usagePerformance or resource usage
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Jun 7, 2026
  4. StanFromIreland
    added this to Date and time issues 🕰️on Jun 8, 2026
  5. sobolevn
    added a commit that references this issue on Jun 8, 2026

    gh-151059: [perf] Use `PyObject_CallMethodOneArg` in datetime's `call…

    537702d
  6. StanFromIreland
    closed this as completedon Jun 8, 2026
  7. github-project-automation
    moved this to Done in Date and time issues 🕰️on Jun 8, 2026
  8. philthompson10
    added a commit that references this issue on Jun 17, 2026

    pythongh-151059: [perf] Use `PyObject_CallMethodOneArg` in datetime's…

    37f9353
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirperformancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Date and time issues 🕰️
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.