Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use protocol=5 for dumping arrays with dtype=object - #1682

Merged
tomMoral merged 3 commits into
joblib:mainfrom
lesteve:protocol-5-for-dtype-object
Apr 29, 2025
Merged

Use protocol=5 for dumping arrays with dtype=object#1682
tomMoral merged 3 commits into
joblib:mainfrom
lesteve:protocol-5-for-dtype-object

Conversation

@lesteve

@lesteve lesteve commented Feb 27, 2025

Copy link
Copy Markdown
Member

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=5 is twice as fast as protocol=4 which is twice as fast and use more than twice less memory than protocol=2.

%load_ext memory_profiler
import pickle
import numpy as np

arr = np.array(['a'*1000 for _ in range(100_000)])

for protocol in [2, 4, 5]:
    print(f"{protocol=}")
    %timeit pickle.dumps(arr, protocol=protocol)
    %memit pickle.dumps(arr, protocol=protocol)

Output:

protocol=2
360 ms ± 58.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
peak memory: 1291.32 MiB, increment: 827.67 MiB
protocol=4
194 ms ± 9.19 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
peak memory: 845.16 MiB, increment: 381.47 MiB
protocol=5
93.5 ms ± 2.24 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
peak memory: 801.47 MiB, increment: 337.77 MiB

@lesteve lesteve changed the title Use protocol=5 for pickling arrays with dtype=object Use protocol=5 for dumping arrays with dtype=object Feb 27, 2025
@codecov

codecov Bot commented Feb 27, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.53%. Comparing base (863994d) to head (cd469f5).
⚠️ Report is 11 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ogrisel

ogrisel commented Mar 3, 2025

Copy link
Copy Markdown
Contributor

Not quite sure why it was protocol=2, maybe because at the time compatibility between Python 2 and Python 3 was important?

I think this is the reason.

@ogrisel ogrisel left a comment

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.

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.

@ogrisel

ogrisel commented Mar 3, 2025

Copy link
Copy Markdown
Contributor

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)
protocol=2
1.35 ms ± 65.3 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
peak memory: 89.78 MiB, increment: 0.00 MiB
protocol=4
1.88 ms ± 229 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
peak memory: 88.08 MiB, increment: 0.27 MiB
protocol=5
1.53 ms ± 64.7 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
peak memory: 88.23 MiB, increment: -0.80 MiB

So the change of protocol does not seem to matter in this case.

@tomMoral
tomMoral merged commit 8515638 into joblib:main Apr 29, 2025
@lesteve
lesteve deleted the protocol-5-for-dtype-object branch May 5, 2025 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants