You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need an efficient way to serialize Pandas Dataframes. As of #606 we can now customize this beyond just pickle. There are a number of different data regimes here including pure numeric data, very compressible numeric data like time series, text data with repeats, categoricals, long text data etc.. We care both about fast encoding and about fast compression for larger results. We want something that has minimal overhead on small dataframes (important for shuffling).
Several options come to mind:
Pickle + lz4: what we use now. Actually not that bad. Pickle deduplicates text data, handles numeric data without too much overhead, etc.. It's a shame that we can't compress per type.
NumPy serialization + blosc on blocks + pickle or msgpack + lz4 on text: This is similar to what we do in partd. A custom approach can get significant speed boosts in some cases.
Arrow: we could give arrow a shot. I suspect that it handles encoding intelligently. It would be nice if it were to provide different compressions per column but we could probably push a bit upstream if this went into use.
It would be good to make this decision with a benchmark in hand. It would be good both to get people's opinions on solutions we should consider as well as some benchmarks that are representative of data that they care about.
Reacted by David Cottrell, Matthieu Bizien, Denis Akhiyarov and Ricky Das
I've spent a lot of time lately working on DataFrame serialization with as little overhead as possible. I'm happy to assist as best I can with development to support this use case and benchmarking to support the decision making. I recently did some rudimentary benchmarking on uncompressed numeric data, see slide 24 in
Here, on the read path, this is all zero-copy up until converting to pandas.DataFrame, which introduces some overhead. This overhead could be reduced through the slightly messy process of deserializing directly into a 2D ndarray and then constructing pandas's internal BlockManager data structure. this will become much more favorable with pandas 2.0 (can be fully zero-copy if we use bitmaps for all null handling, see discussion in wesm/pandas2#46).
For compressible data you can obviously add buffer-level compression -- see https://issues.apache.org/jira/browse/ARROW-300. So in other words, if you have an array with a null bitmap buffer and a contiguous buffer, these buffers would be compressed independently on going into the IPC blob.
Dictionary encoding isn't yet implemented, but it's provided for in the IPC metadata.
Maybe this is pie in the sky, but at some point it may be in dask's interests to adopt a language independent serialization protocol so there is the option to run task worker code written in C++, Julia, or other languages.
Hi @wesm . Thanks for the read and write functions. These should be easy to benchmark and easy to incorporate as an optional dependency (hooray for Arrow being on conda-forge by the way). @jreback any interest in trying these out in the wild? You have some communication heavy computations.
Zero copy sounds nice. So too does compression on different components. It'll be nice to try some workloads in the wild though to see how much impact these have. That might help us to prioritize or deprioritize them. I have not yet run into workloads that are limited by single memory copies. Network bandwidth is more commonly a larger issue for me, at least on Amazon's hardware.
Maybe this is pie in the sky, but at some point it may be in dask's interests to adopt a language independent serialization protocol so there is the option to run task worker code written in C++, Julia, or other languages.
It's decently possible and there is some slow momentum towards that direction. The custom serialization system is a step in that direction. There are other users clamoring for destination-specific serialization (e.g. gpu-gpu transfers) that would also benefit from this. Presumably you would say "OK, this data of type T has to move from worker A to B, what is the best serialization that we have for this case?" If there were a use case today the serialization work could happen in about a week. I put the cost of making a decent Julia/R/whatever worker/client at "a couple of weeks" by someone familiar with the networking stack in that language. See #586
sure, i'll give this a go. IIRC you are going to have this settable via dask.set_options()?
(or someway completely hacky ok), just so I can go back and forth to try things out?
though since on win-64.... will wait for @wesm to kindly make packages available for pyarrow :>
Ah, forgot about the Windows thing. You should wait to try this until I give you the go ahead. I'm fairly comfortable putting Arrow into an optional plugin after #606 gets merged. There will be a few of these for other formats as well.
Initial timings comparing Pickle and Arrow. Corrections or counter-benchmarks welcome. Based on this very narrow benchmark custom serialization + blosc is winning out.
How did you install pyarrow? I believe you are using a debug build (the conda-forge artifact is a debug build, I can fix this to allow you to do a more apples-to-apples comparison)
Yes, you're on a debug build of pyarrow. I'll let you know when there's a new build available built with optimizations turned on (this build is gcc -O0)
PyArrow release builds are up. You ideally should to disable multithreading in blosc to make the benchmarks comparable -- we could add multithreaded writes to Arrow, too, and get similar speedups
tmp = df[['x']] # this is a copy -- should not be included in benchmark
import blosc
blosc.set_nthreads(1) # multithreading is not apples-to-apples
x = df.x.values
df_t = df[['t']] # this is a copy -- should not be included in benchmark
%timeit len(write_arrow(df))
%timeit len(pickle.dumps(df))
%timeit len(write_arrow(tmp))
%timeit len(pickle.dumps(tmp))
%timeit len(blosc.compress(x.data, typesize=x.dtype.itemsize, cname='lz4', clevel=5))
%time len(pickle.dumps(df_t))
%time len(write_arrow(df_t))
In [27]: %timeit len(write_arrow(df))
...:
10 loops, best of 3: 27.3 ms per loop
In [28]: %timeit len(pickle.dumps(df))
...:
10 loops, best of 3: 24.4 ms per loop
In [29]: %timeit len(pickle.dumps(tmp))
...:
The slowest run took 5.50 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 626 µs per loop
In [30]: %timeit len(write_arrow(tmp))
...:
The slowest run took 6.02 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 689 µs per loop
In [31]: %timeit len(pickle.dumps(tmp))
^[[41;1R
1000 loops, best of 3: 627 µs per loop
In [34]: %timeit len(blosc.compress(x.data, typesize=x.dtype.itemsize, cname='lz4', clevel=5))
...:
The slowest run took 5.77 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 683 µs per loop
In [36]: %timeit len(pickle.dumps(df_t))
10 loops, best of 3: 26.1 ms per loop
In [37]: %timeit len(write_arrow(df_t))
100 loops, best of 3: 17.7 ms per loop
if you change the characteristics of the string data, you get different results:
In [96]: paste
from pandas.util.testing import rands
string_choices = [rands(10) for i in range(1000)]
df = pd.DataFrame({'x': np.random.randint(0, 10000, size=n, dtype='i4'), 'y': np.random.random(n),
't': np.random.choice(string_choices, size=n)})
## -- End pasted text --
In [97]: %timeit len(write_arrow(df))
10 loops, best of 3: 47.1 ms per loop
In [98]: %timeit len(pickle.dumps(df))
10 loops, best of 3: 164 ms per loop
In [99]: df_t = df[['t']] # this is a copy -- should not be included in benchmark
In [100]: %timeit len(pickle.dumps(df_t))
10 loops, best of 3: 152 ms per loop
In [101]: %timeit len(write_arrow(df_t))
10 loops, best of 3: 40.1 ms per loop
there's some performance loss on the Arrow read path right now because we aren't constructing the precise pandas BlockManager. If we wanted to hyperoptimize for the pandas 1.x memory layout this would be a nice project for someone to tackle (multithreaded blockmanager read/write)
note you can also go through arrow's native IO subsystem and avoid extra pybytes interactions -- this lets you interact with memory in C/C++ with zero copy
OK, there is a trivial implementation using Arrow on the Dask side here: #643
Once questions there get resolved we should have enough in place to start doing more integrative benchmarks.
In my mind there are two approaches, Blosc-and-custom-code or Arrow. Each has pros and cons.
The Blosc solution can be implemented today and is likely to be near optimal in speed, at least for numeric data. The main cons here are that we need to write code around the block manager etc. and we need to maintain this going forward. We're likely to miss things like new dtypes, categorical index, etc.. The good news here is that we've already done a lot of this a couple times before and can probably steal ideas from previous implementations in systems like partd.
The Arrow solution has two main things going for it:
Other people are in charge of making sure the entire Pandas abstraction is well covered.
It keeps open the cross-language door
However in the short term it is missing some things like per-column or dtype-specific compression, comprehensive support for the Pandas abstraction (see issues in #643, I'd love to be wrong about this) etc..
I would like to write up and tune some performance benchmarks for dask.dataframe sometime in the next month. Serialization and communication costs do contribute significantly in shuffling so this is likely to end up on my critical path soon-ish.
It seems reasonable to have as many options available as possible (assuming the development burden is not too much), and as long as we have reproducible benchmarks to evaluate performance that can help drive performance engineering work. The Arrow<->Pandas conversion has a number of immaturities so it's not an turnkey solution at the moment -- it is an accessible codebase, though, so I invite others to get involved and I can help review patches and where help reach consensus w/ the other Arrow devs where design changes are needed (e.g. adding compression options).
@mrocklin
I think its worth trying a hybrid approach here.
wholesale pickling of the frame
In [10]: %time len(pickle.dumps(df))
CPU times: user 41.6 ms, sys: 8.45 ms, total: 50 ms
Wall time: 50 ms
Out[10]: 14003232
pickle in a dict-of-columns, about the same
In [7]: %time len(pickle.dumps({c: col for c, col in df.iteritems()}))
CPU times: user 41.9 ms, sys: 14.4 ms, total: 56.3 ms
Wall time: 56.3 ms
Out[7]: 14003220
but now can compress per-column
In [8]: %time len({c: blosc.compress(col.values, typesize=col.dtype.itemsize, cname='lz4', clevel=5) for c, col in df.iteritems()})
CPU times: user 14.4 ms, sys: 3.21 ms, total: 17.6 ms
Wall time: 6.92 ms
Out[8]: 3
In [11]: %time len(pickle.dumps({c: blosc.compress(col.values, typesize=col.dtype.itemsize, cname='lz4', clevel=5) for c, col in df.iteritems()}))
CPU times: user 18 ms, sys: 7.13 ms, total: 25.1 ms
Wall time: 13.7 ms
Out[11]: 12608516
points of note:
you have to unpickle, uncompress & reassemble (pd.concat, a single copy) on the receiving side, would require that you also send the column order, so a bit more complex
allows per-dtype compression
if you have lots of columns, this might degrade versus the block compression per-dtype (using blocks rather than columns), though I think its still going to be quite competetive as most compression is along the column and doing it on a bunch of same-dtype columns might not be optimal
I would like to avoid pickle if possible. It uses a couple of needless memory copies. Also if we don't compress (which happens sometimes) it's nice to be able to just pass along the memoryview directly. We could still do this when operating on columns (sliced memoryiews are just views).
Why would compressing columns individually be better than compressing several at once? I think that most of the fast compressors are purely local.
If I put up a minimal draft of this would you have time to review/suggest tests?
Why would compressing columns individually be better than compressing several at once? I think that most of the fast compressors are purely local.
This is a big assumption that only sometimes is True. IOW, you assume that 2 integer (or 2 float or whatever) are compressable just because they happen to be the same dtype. If they represent different data then they may or may not compress well as a 2-d array (not that actually know if blosc is smart about this, if it is, then what I am saying may be False!).
I think its safer/better to simply compress single columns (e.g. this is what a column-major storage system does, e.g. redshift).
I would like to avoid pickle if possible. It uses a couple of needless memory copies. Also if we don't compress (which happens sometimes) it's nice to be able to just pass along the memoryview directly. We could still do this when operating on columns (sliced memoryiews are just views).
yes I don't think you actually need pickle at all. (assume that you only have strings for object dtypes, FYI, you can do a pd.lib.infer_dtype to check this). A byte protocol is prob best.
If I put up a minimal draft of this would you have time to review/suggest tests?
Yeah, totally. When ARROW-596/597 are done (let's do this month), they'll go out in the 0.3 release (this month). Probably will update conda artifacts before then
Arrow is pretty similar to pickle, other than object (strings) in which case it's quite a bit faster. Interestingly, arrow was slower to serialize datetime64[ns]. Perhaps that could be optimized.
An important is that pickle can use pandas' RangeIndex to avoid serializing the entire index if possible. With a RangeIndex pickle outperformed arrow in most cases (other than objects) since it had less work to do. I opened https://issues.apache.org/jira/browse/ARROW-1593 about a simple solution dask could use. We could detect that we have a RangeIndex when serializing and pass the start, stop, step in our header and just not use preserve_index (once that's available in pa.serialize_pandas).
All the tests pass, other than the PeriodIndex test. This type isn't implemented in Arrow (though I think user-defined logical types are on the roadmap). The implementation is pretty clean. Once the metadata issues are fixed on Arrow's side, it'll be even simpler. I'm not concerned about maintenance burden.
I think using Arrow for serialization would be good to add as an option, and probably even the default in a future release..
No need to apologize for new issues, only way to make the software better! We should try to get an ASV setup rolling so that we can have microbenchmarks for all these cases to put some targets on the wall to optimize, then we can make sure that we don't have any perf regressions going forward. Maybe we can add these ASV benchmarks to the nightly runs on the pandas box at some point.
I suspect that the perf difference in float64 data largely has to do with the null bitmap construction -- pickle writes the internal blocks unmodified. This can be partially mitigated through parallel processing when you have many columns (https://issues.apache.org/jira/browse/ARROW-1594)
Not sure if you're using pickle with NumPy arrays anywhere, but if so I recommend you also take a look at using pyarrow.serialize and pyarrow.deserialize as an alternative for moving around ndarrays (or lists or dicts of ndarrays) in dask:
In [16]: import pyarrow as pa
In [17]: import numpy as np
In [18]: import pickle
In [19]: arr = np.random.randn(1000, 1000)
In [20]: %timeit rt = pickle.loads(pickle.dumps(arr))
2.85 ms ± 155 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [21]: %timeit rt = pa.deserialize(pa.serialize(arr).to_buffer())
651 µs ± 30.1 mus per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Might seem crazy to beat pickle by 4x with an ndarray, but avoiding extra copies will do that
We should try to get an ASV setup rolling so that we can have microbenchmarks for all these cases to put some targets on the wall to optimize, then we can make sure that we don't have any perf regressions going forward. Maybe we can add these ASV benchmarks to the nightly runs on the pandas box at some point.
I'm happy to do that. I'll also update the benchmarks to have versions with and without nulls.
I believe the numpy code is also using pickle. I'll benchmark that as well.
For NumPy we're actually aiming for zero-copy. We serialize the metadata and then just pass down the .data memoryview unmodified (most of the time). People using dask.array tend to be particularly performance conscious; the current system I'm working on has 24Gb/s interconnect. Our current bottleneck is actually Tornado, which does a couple of copies internally.
First if Pandas objects support pickle protocol 5, which it sounds like they may already do ( pandas-dev/pandas#34244 ), and Dask supports pickle protocol 5, which PR ( #3784 ) implements, then it should be possible to get efficient serialization of Pandas objects just using pickle protocol 5.
Second if Pandas objects simply consist of other objects that are already serializable with Dask (like NumPy arrays), then it should be possible to traverse objects and serialize everything we can. In fact this is how we added SciPy sparse matrix serialization recently.
First if Pandas objects support pickle protocol 5, which it sounds like they may already do ( pandas-dev/pandas#34244 ), and Dask supports pickle protocol 5, which PR ( #3784 ) implements, then it should be possible to get efficient serialization of Pandas objects just using pickle protocol 5.
FYI this is now in Distributed 2.17.0+. Would be interesting to see what sort of mileage people get out of this 🙂
As of Dask + Distributed 2.21.0, we now support pickle protocol 5 on older versions of Python with the pickle5 backport package. This extracts the NumPy arrays used under-the-hood and preserves their type, which should allow for efficient compression. Here's a quick example:
Activity
wesm commentedon Nov 1, 2016
I've spent a lot of time lately working on DataFrame serialization with as little overhead as possible. I'm happy to assist as best I can with development to support this use case and benchmarking to support the decision making. I recently did some rudimentary benchmarking on uncompressed numeric data, see slide 24 in
http://www.slideshare.net/wesm/python-data-wrangling-preparing-for-the-future
the benchmarking code (which I can clean up and publish if so desired) looks like:
Here, on the read path, this is all zero-copy up until converting to pandas.DataFrame, which introduces some overhead. This overhead could be reduced through the slightly messy process of deserializing directly into a 2D ndarray and then constructing pandas's internal BlockManager data structure. this will become much more favorable with pandas 2.0 (can be fully zero-copy if we use bitmaps for all null handling, see discussion in wesm/pandas2#46).
For compressible data you can obviously add buffer-level compression -- see https://issues.apache.org/jira/browse/ARROW-300. So in other words, if you have an array with a null bitmap buffer and a contiguous buffer, these buffers would be compressed independently on going into the IPC blob.
Dictionary encoding isn't yet implemented, but it's provided for in the IPC metadata.
Maybe this is pie in the sky, but at some point it may be in dask's interests to adopt a language independent serialization protocol so there is the option to run task worker code written in C++, Julia, or other languages.
mrocklin commentedon Nov 1, 2016
Hi @wesm . Thanks for the read and write functions. These should be easy to benchmark and easy to incorporate as an optional dependency (hooray for Arrow being on conda-forge by the way). @jreback any interest in trying these out in the wild? You have some communication heavy computations.
Zero copy sounds nice. So too does compression on different components. It'll be nice to try some workloads in the wild though to see how much impact these have. That might help us to prioritize or deprioritize them. I have not yet run into workloads that are limited by single memory copies. Network bandwidth is more commonly a larger issue for me, at least on Amazon's hardware.
It's decently possible and there is some slow momentum towards that direction. The custom serialization system is a step in that direction. There are other users clamoring for destination-specific serialization (e.g. gpu-gpu transfers) that would also benefit from this. Presumably you would say "OK, this data of type T has to move from worker A to B, what is the best serialization that we have for this case?" If there were a use case today the serialization work could happen in about a week. I put the cost of making a decent Julia/R/whatever worker/client at "a couple of weeks" by someone familiar with the networking stack in that language. See #586
jreback commentedon Nov 1, 2016
sure, i'll give this a go. IIRC you are going to have this settable via
dask.set_options()?(or someway completely hacky ok), just so I can go back and forth to try things out?
though since on win-64.... will wait for @wesm to kindly make packages available for
pyarrow:>mrocklin commentedon Nov 1, 2016
Ah, forgot about the Windows thing. You should wait to try this until I give you the go ahead. I'm fairly comfortable putting Arrow into an optional plugin after #606 gets merged. There will be a few of these for other formats as well.
mrocklin commentedon Nov 2, 2016
Initial timings comparing Pickle and Arrow. Corrections or counter-benchmarks welcome. Based on this very narrow benchmark custom serialization + blosc is winning out.
wesm commentedon Nov 2, 2016
How did you install pyarrow? I believe you are using a debug build (the conda-forge artifact is a debug build, I can fix this to allow you to do a more apples-to-apples comparison)
mrocklin commentedon Nov 2, 2016
I may have pip installed some thing in between
wesm commentedon Nov 2, 2016
Yes, you're on a debug build of pyarrow. I'll let you know when there's a new build available built with optimizations turned on (this build is
gcc -O0)wesm commentedon Nov 6, 2016
Digging into a memory leak bug (ARROW-362) but will get a release build up on conda-forge after that's fixed
wesm commentedon Nov 9, 2016
PyArrow release builds are up. You ideally should to disable multithreading in blosc to make the benchmarks comparable -- we could add multithreaded writes to Arrow, too, and get similar speedups
if you change the characteristics of the string data, you get different results:
there's some performance loss on the Arrow read path right now because we aren't constructing the precise pandas BlockManager. If we wanted to hyperoptimize for the pandas 1.x memory layout this would be a nice project for someone to tackle (multithreaded blockmanager read/write)
note you can also go through arrow's native IO subsystem and avoid extra pybytes interactions -- this lets you interact with memory in C/C++ with zero copy
YMMV, worth double checking my work in case I made some mistakes
mrocklin commentedon Nov 9, 2016
OK, there is a trivial implementation using Arrow on the Dask side here: #643
Once questions there get resolved we should have enough in place to start doing more integrative benchmarks.
In my mind there are two approaches, Blosc-and-custom-code or Arrow. Each has pros and cons.
The Blosc solution can be implemented today and is likely to be near optimal in speed, at least for numeric data. The main cons here are that we need to write code around the block manager etc. and we need to maintain this going forward. We're likely to miss things like new dtypes, categorical index, etc.. The good news here is that we've already done a lot of this a couple times before and can probably steal ideas from previous implementations in systems like partd.
The Arrow solution has two main things going for it:
However in the short term it is missing some things like per-column or dtype-specific compression, comprehensive support for the Pandas abstraction (see issues in #643, I'd love to be wrong about this) etc..
I would like to write up and tune some performance benchmarks for dask.dataframe sometime in the next month. Serialization and communication costs do contribute significantly in shuffling so this is likely to end up on my critical path soon-ish.
wesm commentedon Nov 9, 2016
It seems reasonable to have as many options available as possible (assuming the development burden is not too much), and as long as we have reproducible benchmarks to evaluate performance that can help drive performance engineering work. The Arrow<->Pandas conversion has a number of immaturities so it's not an turnkey solution at the moment -- it is an accessible codebase, though, so I invite others to get involved and I can help review patches and where help reach consensus w/ the other Arrow devs where design changes are needed (e.g. adding compression options).
jreback commentedon Jan 31, 2017
@mrocklin
I think its worth trying a hybrid approach here.
wholesale pickling of the frame
pickle in a dict-of-columns, about the same
but now can compress per-column
points of note:
pd.concat, a single copy) on the receiving side, would require that you also send the column order, so a bit more complexmrocklin commentedon Jan 31, 2017
I would like to avoid pickle if possible. It uses a couple of needless memory copies. Also if we don't compress (which happens sometimes) it's nice to be able to just pass along the memoryview directly. We could still do this when operating on columns (sliced memoryiews are just views).
Why would compressing columns individually be better than compressing several at once? I think that most of the fast compressors are purely local.
If I put up a minimal draft of this would you have time to review/suggest tests?
jreback commentedon Jan 31, 2017
This is a big assumption that only sometimes is True. IOW, you assume that 2 integer (or 2 float or whatever) are compressable just because they happen to be the same dtype. If they represent different data then they may or may not compress well as a 2-d array (not that actually know if blosc is smart about this, if it is, then what I am saying may be False!).
I think its safer/better to simply compress single columns (e.g. this is what a column-major storage system does, e.g. redshift).
yes I don't think you actually need pickle at all. (assume that you only have strings for object dtypes, FYI, you can do a
pd.lib.infer_dtypeto check this). A byte protocol is prob best.sure
19 remaining items
wesm commentedon Mar 3, 2017
@jreback I would advise adding such convenience functions in Arrow so we don't have to set up integration tests inside pandas just yet
jreback commentedon Mar 3, 2017
happy either way :> though I wouldn't find this a big burden on the current pandas code (nor testing).
Literally this would be a direct wrapper to call a single arrow function.
wesm commentedon Mar 3, 2017
Yeah, totally. When ARROW-596/597 are done (let's do this month), they'll go out in the 0.3 release (this month). Probably will update conda artifacts before then
TomAugspurger commentedon Sep 21, 2017
I decided to pick this up again yesterday; sorry about the new JIRAs Wes :). My work is at master...TomAugspurger:arrow-serialization
Heres a notebook with some benchmarks at http://nbviewer.jupyter.org/gist/TomAugspurger/ab01751275b8f5262dabc7fd07a0f19f/serialization.ipynb (scroll down for the plot)
Arrow is pretty similar to pickle, other than
object(strings) in which case it's quite a bit faster. Interestingly, arrow was slower to serialize datetime64[ns]. Perhaps that could be optimized.An important is that pickle can use pandas' RangeIndex to avoid serializing the entire index if possible. With a
RangeIndexpickle outperformed arrow in most cases (other than objects) since it had less work to do. I opened https://issues.apache.org/jira/browse/ARROW-1593 about a simple solution dask could use. We could detect that we have aRangeIndexwhen serializing and pass thestart,stop,stepin our header and just not usepreserve_index(once that's available inpa.serialize_pandas).All the tests pass, other than the
PeriodIndextest. This type isn't implemented in Arrow (though I think user-defined logical types are on the roadmap). The implementation is pretty clean. Once the metadata issues are fixed on Arrow's side, it'll be even simpler. I'm not concerned about maintenance burden.I think using Arrow for serialization would be good to add as an option, and probably even the default in a future release..
wesm commentedon Sep 21, 2017
No need to apologize for new issues, only way to make the software better! We should try to get an ASV setup rolling so that we can have microbenchmarks for all these cases to put some targets on the wall to optimize, then we can make sure that we don't have any perf regressions going forward. Maybe we can add these ASV benchmarks to the nightly runs on the pandas box at some point.
I suspect that the perf difference in float64 data largely has to do with the null bitmap construction -- pickle writes the internal blocks unmodified. This can be partially mitigated through parallel processing when you have many columns (https://issues.apache.org/jira/browse/ARROW-1594)
Not sure if you're using pickle with NumPy arrays anywhere, but if so I recommend you also take a look at using
pyarrow.serializeandpyarrow.deserializeas an alternative for moving around ndarrays (or lists or dicts of ndarrays) in dask:Might seem crazy to beat pickle by 4x with an ndarray, but avoiding extra copies will do that
TomAugspurger commentedon Sep 21, 2017
I'm happy to do that. I'll also update the benchmarks to have versions with and without nulls.
I believe the numpy code is also using pickle. I'll benchmark that as well.
mrocklin commentedon Sep 21, 2017
For NumPy we're actually aiming for zero-copy. We serialize the metadata and then just pass down the
.datamemoryview unmodified (most of the time). People using dask.array tend to be particularly performance conscious; the current system I'm working on has 24Gb/s interconnect. Our current bottleneck is actually Tornado, which does a couple of copies internally.You might be interested in looking at our test suite for numpy arrays here: https://github.com/dask/distributed/blob/master/distributed/protocol/tests/test_numpy.py#L30-L65
A few corner cases have bit us in the past that are now recorded there. They might be of general value.
mrocklin commentedon Sep 21, 2017
The numpy code is not using pickle.
TomAugspurger commentedon Sep 21, 2017
Ah, good to know (I just looked at the fallback for object types).
wesm commentedon Sep 21, 2017
@mrocklin cool, that's helpful, thanks. I opened https://issues.apache.org/jira/browse/ARROW-1596 so that we can expand the test suite based on this
Internally
pyarrow.deserializeis using the C API equivalentnp.frombufferso I would expect the performance to be the same thenjakirkham commentedon May 19, 2020
Adding a couple of thoughts here:
First if Pandas objects support pickle protocol 5, which it sounds like they may already do ( pandas-dev/pandas#34244 ), and Dask supports pickle protocol 5, which PR ( #3784 ) implements, then it should be possible to get efficient serialization of Pandas objects just using pickle protocol 5.
Second if Pandas objects simply consist of other objects that are already serializable with Dask (like NumPy arrays), then it should be possible to traverse objects and serialize everything we can. In fact this is how we added SciPy sparse matrix serialization recently.
cc @TomAugspurger (for awareness)
jakirkham commentedon May 27, 2020
FYI this is now in Distributed 2.17.0+. Would be interesting to see what sort of mileage people get out of this 🙂
jakirkham commentedon Jul 21, 2020
As of Dask + Distributed 2.21.0, we now support pickle protocol 5 on older versions of Python with the
pickle5backport package. This extracts the NumPy arrays used under-the-hood and preserves their type, which should allow for efficient compression. Here's a quick example:jakirkham commentedon Jul 21, 2020
Closing as this seems to be resolved with pickle protocol 5, but please feel free to reopen if I've missed something.