Use protocol=5 for dumping arrays with dtype=object - #1682
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1682 +/- ##
==========================================
- Coverage 95.57% 95.53% -0.04%
==========================================
Files 46 46
Lines 7865 7865
==========================================
- Hits 7517 7514 -3
- Misses 348 351 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
I think this is the reason. |
ogrisel
left a comment
There was a problem hiding this comment.
Protocol 5 was introduced in Python 3.8. Since we dropped support for earlier versions, it should be safe to this change.
LGTM, but we need a changelog entry.
|
For the record, here are benchmark results when using an object dtype instead of an unboxed fixed-size dtype: %load_ext memory_profiler
import pickle
import numpy as np
arr = np.array(['a'*1000 for _ in range(100_000)], dtype=object)
for protocol in [2, 4, 5]:
print(f"{protocol=}")
%timeit pickle.dumps(arr, protocol=protocol)
%memit pickle.dumps(arr, protocol=protocol)So the change of protocol does not seem to matter in this case. |
Not sure if there is a better way than hardcoding protocol=5 here.
Not quite sure why it was protocol=2, maybe because at the time compatibility between Python 2 and Python 3 was important? I did not find an explanation looking at #260.
Quick benchmark for an array with object dtype
protocol=5is twice as fast asprotocol=4which is twice as fast and use more than twice less memory thanprotocol=2.Output: