Repository navigation
[POC][PYTHON] Replace the bundled cloudpickle with cloudpickle-rust - #60120
Closed
HyukjinKwon wants to merge 2 commits into
Closed
HyukjinKwon wants to merge 2 commits into
HyukjinKwon wants to merge 2 commits into
Conversation
Replace the copy of cloudpickle 3.1.2 bundled as pyspark.cloudpickle with cloudpickle-rust (https://github.com/HyukjinKwon/cloudpickle-rust), a Rust implementation of cloudpickle, which becomes a required dependency. pyspark.cloudpickle stays as a thin module on top of cloudpickle_rust: - pyspark.cloudpickle.cloudpickle and cloudpickle_fast are aliases of the cloudpickle_rust modules, so that payloads pickled by previous versions, e.g. UDFs sent by an older Spark Connect client, still unpickle. - Its Pickler keeps the co_filename of the code pickled by value, which cloudpickle-rust (like unreleased cloudpickle 3.2) replaces with "<dynamic-code>"; Spark shows it in UDF tracebacks and UDF profilers. Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This is a POC that replaces the copy of cloudpickle 3.1.2 bundled as
pyspark.cloudpicklewith cloudpickle-rust 0.1.0, a Rust (PyO3) implementation of cloudpickle with the same API, producing the same payloads except for the module of the reconstructors (cloudpickle_rust.cloudpickle).pyspark.cloudpicklestays as a thin module on top ofcloudpickle_rust, so PySpark itself and third-party code keep importing it as before:pyspark.cloudpickle.cloudpickleandpyspark.cloudpickle.cloudpickle_fastare aliases of thecloudpickle_rustmodules. Payloads pickled by previous PySpark versions referencepyspark.cloudpickle.cloudpickle.<reconstructor>, for example the UDFs sent by an older Spark Connect client (thepyspark-connect-old-clientjob), so they still unpickle.Picklerkeeps the originalco_filenameof the code pickled by value. cloudpickle-rust, like upstream cloudpickle master (cloudpipe/cloudpickle#560, not released yet), replaces it with"<dynamic-code>". That would remove the file names from the tracebacks of Python UDFs, break the perf profiler output, and make the memory profiler fail withMEMORY_PROFILE_INVALID_SOURCE(20 tests intest_udf_profilerfailed without it). The override is a dispatch table of the same native type as cloudpickle-rust's, so only code objects go through Python.cloudpickle-rust>=0.1.0becomes a required dependency ofpysparkandpyspark-client. It is added to thepyproject.tomldependency groups that the CI images install, todev/requirements.txt, to the workflows that pip install packages directly (Scala tests that run Python UDFs, Maven, macOS, the Spark 4.0 client against the master server), to the release image, and to the Kubernetes PySpark image.LICENSE,LICENSE-binary,licenses*/LICENSE-cloudpickle.txt), and its ruff, mypy, coverage and RAT exclusions.Why are the changes needed?
To see if cloudpickle-rust can replace the bundled cloudpickle. It is 1.4x to 1.7x faster to pickle dynamic functions, classes and their instances (see its benchmarks), passes the upstream cloudpickle test suite with the same results, and supports free-threaded CPython without re-enabling the GIL.
Does this PR introduce any user-facing change?
Yes. PySpark requires cloudpickle-rust, which is a native extension. The bundled copy is imported by executors from
pyspark.zip, but cloudpickle-rust has to be installed in the Python environments of the driver and of every executor (cluster images,--archivesvirtual environments, and so on). The Python functions and classes pickled by value, such as UDFs and RDD closures, now referencecloudpickle_rust.cloudpickle, so they cannot be unpickled where cloudpickle-rust is not installed, for example by a Spark Connect server of a version without this change.How was this patch tested?
By the CI of this PR, which runs all the tests because
pyproject.tomlchanged.Locally on macOS arm64 with CPython 3.14:
pyspark.tests.test_serializers,test_worker,test_rdd,test_broadcast,test_profiler,test_memory_profiler,pyspark.sql.tests.test_udf_profiler, and the doctests ofpyspark.serializersandpyspark.accumulators.TypeVars, namedtuples, dict views and module references.CloudPicklersubclasses such as_InProcessPicklerstill get theirreducer_overridecalled.dev/lint-python, and ruff.Not covered yet, and needed before this could go in:
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
This pull request and its description were written by Isaac.