Skip to content

gh-162350: Add AC vectorcall for _csv.Dialect - #162499

Open
tanloong wants to merge 3 commits into
python:mainfrom
tanloong:csv_vectorcall
Open

tanloong wants to merge 3 commits into
python:mainfrom
tanloong:csv_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Replace the hand-written dialect_new with an Argument Clinic + vectorcall. Move _csv_Dialect___replace___impl() from calling dialect_new() to dialect_new_helper() to avoid temporary tuple.

Error message for too many arguments is now TypeError: Dialect() takes at most 9 arguments (N given) instead of TypeError: function takes at most 9 arguments (N given).

Didn't change _csv.reader / _csv.writer whose **fmtparams is not supported by AC vectorcall before #162465 is merged.

Benchmark:

+-------------------------------------------------+---------+-----------------------+
| Benchmark                                       | main    | patch                 |
+=================================================+=========+=======================+
| _csv.Dialect()                                  | 247 ns  | 193 ns: 1.28x faster  |
+-------------------------------------------------+---------+-----------------------+
| _csv.Dialect().__replace__(**{'delimiter':','}) | 1.82 us | 1.52 us: 1.20x faster |
+-------------------------------------------------+---------+-----------------------+
| Geometric mean                                  | (ref)   | 1.24x faster          |
+-------------------------------------------------+---------+-----------------------+
Details
import pyperf

SETUP = """
import _csv
"""

CASES = [
    ("_csv.Dialect()", "_csv.Dialect()"),
    ("_csv.Dialect().__replace__(**{'delimiter':','})", "_csv.Dialect().__replace__(**{'delimiter':','})"),
]

runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

@tanloong
tanloong marked this pull request as draft November 24, 2026 07:24
@tanloong
tanloong marked this pull request as ready for review November 24, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants