Skip to content

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

Merged
cmaloney merged 1 commit into
python:mainfrom
tanloong:picklebuffer_vectorcall
Nov 25, 2026
Merged

cmaloney merged 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.

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.

Makes a measurable difference in pickle benchmarks

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.

It is not entirely free (it adds code to the binary), but if there are pickle benchmarks that improve it is fine.
Code changes look good to me

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.

In local prototyping I found overall pickles got measurably faster for small cases. Will re-run with this PR and validate

@cmaloney

cmaloney commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This saves ~38 ns per object on my x64 linux whose __reduce_ex__ returns a PickleBuffer (it's only used in protocol v5 and out of band buffers).

Benchmark main PR
pickle.dumps(objs, protocol=5, buffer_callback=list().append) 22.3 us 18.5 us: 1.21x faster
pickle.loads(data, buffers=bufs) 9.35 us 9.48 us: 1.01x slower

loads doesn't call the constructor, so it stays flat.

Benchmark
import pyperf

runner = pyperf.Runner()
setup = """
import pickle
class Blob:
    def __init__(self, b):
        self.b = b
    def __reduce_ex__(self, protocol):
        return bytearray, (pickle.PickleBuffer(self.b),)
objs = [Blob(bytearray(1000)) for _ in range(100)]
bufs = []
data = pickle.dumps(objs, protocol=5, buffer_callback=bufs.append)
"""
runner.timeit("pickle.dumps(objs, protocol=5, buffer_callback=list().append)",
              "pickle.dumps(objs, protocol=5, buffer_callback=list().append)", setup)
runner.timeit("pickle.loads(data, buffers=bufs)",
              "pickle.loads(data, buffers=bufs)", setup)

@cmaloney
cmaloney merged commit cc0e59f into python:main Nov 25, 2026
62 checks passed
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.

3 participants