Skip to content

gh-162350: Add AC vectorcall for pickle.PickleBuffer - #162448

Open
tanloong wants to merge 1 commit into
python:mainfrom
tanloong:picklebuffer_vectorcall
Open

tanloong wants to merge 1 commit into
python:mainfrom
tanloong:picklebuffer_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Replace hand-written picklebuf_new with Argument Clinic + vectorcall.

Extra positional arguments now raise TypeError: PickleBuffer expected 1 argument, got N instead of TypeError: PickleBuffer() takes at most 1 argument (N given). Keyword arguments now raise TypeError: PickleBuffer() takes no keyword arguments instead of TypeError: PickleBuffer() takes exactly 1 positional argument (0 given).

Benchmark:

+--------------------------------+--------+------------------------+
| Benchmark                      | main   | patch                  |
+================================+========+========================+
| PickleBuffer(b)                | 187 ns | 112 ns: 1.67x faster   |
| pickle.PickleBuffer(b)         | 205 ns | 124 ns: 1.66x faster   |
+--------------------------------+--------+------------------------+
| Geometric mean                 | (ref)  | 1.66x faster           |
+--------------------------------+--------+------------------------+
Details
import pyperf

SETUP = """
import pickle
b = b'abc'
PickleBuffer = pickle.PickleBuffer
"""

CASES = [
    ('PickleBuffer(b)', 'PickleBuffer(b)'),
    ('pickle.PickleBuffer(b)', 'pickle.PickleBuffer(b)')
]

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

Comment thread Objects/picklebufobject.c
.tp_basicsize = sizeof(PyPickleBufferObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_new = picklebuf_new,
.tp_vectorcall = picklebuf_vectorcall,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add vector all? Is the PickleBuffer used in performance critical code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is listed as speedup candidate in #162350. Benchmark above shows good 1.6x enhancement. This enhancement is free, the only cost to get the benefit is a bit refactor of code to add vectorcall. I think we can do it.

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