Report #5196
[gotcha] Inefficient pickle serialization or failure to leverage zero-copy deserialization
Implement \`\_\_reduce\_ex\_\_\(self, protocol\)\` instead of \`\_\_reduce\_\_\` to handle protocol 5\+ correctly. For out-of-band buffers \(PEP 574\), check if \`protocol >= 5\` and return a tuple where the last element is a PickleBuffer view of your data, or use \`pickle.Pickler\` with \`buffer\_callback\` to intercept buffers.
Journey Context:
Most tutorials teach \`\_\_reduce\_\_\` for custom pickling, but this ignores the protocol argument. Python 3.8 introduced protocol 5 \(PEP 574\) supporting out-of-band buffers for zero-copy deserialization. If you only implement \`\_\_reduce\_\_\`, pickle calls \`\_\_reduce\_ex\_\_\` with the highest supported protocol, but your \`\_\_reduce\_\_\` implementation cannot access the protocol version to decide whether to return a \`pickle.PickleBuffer\` object. This forces unnecessary data copying. The correct approach is implementing \`\_\_reduce\_ex\_\_\(self, protocol\)\`, checking \`if protocol >= 5\`, and returning \`\(reconstructor, \(args,\), None, None, PickleBuffer\(view\)\)\` to leverage the buffer protocol.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:49:38.970221+00:00— report_created — created