HyukjinKwon opened a new pull request, #811:
URL: https://github.com/apache/spark-connect-rust/pull/811
### What changes were proposed in this pull request?
A follow-up of #778 (SPARK-61855), from a round-2 review of the drop-in's
Rust protobuf runtime (`pyspark.sql.connect.proto`) against upb (protobuf
7.36.2). Each item was probed on both clients.
**1. Large messages no longer stop the other Python threads and signals.**
- Parsing, copying (the constructor, `CopyFrom`, `copy.deepcopy`) and
merging held the GIL to the end. With 1M strings, another Python thread waited
about 1.2 s at a parse and 1.8 s at a copy, where upb's waits 45 to 100 ms. A
signal waited as long: a `SIGALRM` handler ran 1.16 s late.
- These operations now count a step of an interrupt budget per message, with
no borrow held at the checkpoints. So every 1,024 messages another thread takes
the GIL, and a signal raises its handler's exception, as `KeyboardInterrupt`
does.
- They are also faster:
- fields are stored sparsely;
- messages are made of their cached class;
- encoding runs in two passes;
- repeated fields are decoded with their container and class cached;
- `_execute_command` decodes and encodes without the GIL.
| 1M strings | master: time / longest pause of another thread | this PR |
upb |
|---|---|---|---|
| parse | 1.5 s / 1.24 s | 0.53 s / 78 ms | 52 ms / 45 ms |
| copy (constructor) | 2.07 s / 1.76 s | 0.45 s / 76 ms | 104 ms / 96 ms |
| `CopyFrom` | 2.1 s / 1.8 s | 0.45 s / 79 ms | 100 ms / 90 ms |
| `SerializeToString` | 170 ms / 165 ms | 85 ms / 79 ms | 55 ms / 50 ms |
| parse of an `ExecutePlanResponse` | 1.5 s / 1.2 s | 0.51 s / 78 ms | 52 ms
/ 45 ms |
The ML results that the vendored `pyspark.ml` parses, such as
`StringIndexerModel.labels` and `CountVectorizerModel.vocabulary`, no longer
pause the program for seconds.
**2. upb's container and message methods.**
- `RepeatedScalarContainer.__array__`, of upb's dtypes:
- int32 of an enum, `S` of bytes and `str` of strings;
- `numpy.asarray` of a float field gives float32, not float64;
- it also takes a given dtype, and raises upb's errors;
- before, `numpy.asarray` went through the generic sequence conversion,
which gave float64 or int64 and was about 100x slower than upb's.
- A map of messages is a `MessageMapContainer`, a subclass of the map
container, with upb's `get_or_create`.
- `RepeatedCompositeContainer.append` returns the element, as upb's.
- `HasField`, `ClearField` and `WhichOneof` take bytes and bytearray names.
- `Extensions` is not in `dir()` of a message that declares no extension
ranges.
- The classes have upb's `<FIELD>_FIELD_NUMBER` constants, such as
`Expression.Literal.STRING_FIELD_NUMBER`.
- A parse that fails keeps what it read before the failure, as upb's.
**3. PySpark's pickles of nested messages load.** upb reduces a message of a
nested type (`Expression.Literal`) to protobuf's `_InternalConstructMessage` of
the full name of its type, which reads protobuf's default symbol database. The
runtime now registers the classes of its own files there, so these pickles load
as messages of the runtime. A plugin's classes are not registered, so
protobuf's lookups of a plugin's file keep giving protobuf's classes, as #785
documents. That also holds when the package installs again.
`docs/compatibility.md` describes these, with the remaining differences:
- the runtime makes a Python object of each message that it parses or
copies, where upb makes one when the message is read;
- `SerializeToString` and the release of a message hold the GIL to the end:
0.07 s for a million messages.
### Why are the changes needed?
A program that parsed or copied a large message, such as an ML result of a
large vocabulary, stopped its other threads and its signal handlers for
seconds, where PySpark's stops them for less than 0.1 s. Code that used upb's
`__array__`, `get_or_create`, the return value of `append`, bytes field names
or the field-number constants failed or behaved otherwise, and PySpark's
pickles of nested messages did not load.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- large messages yield the GIL and signals every 1,024 messages;
- the containers and messages have upb's methods and constants above;
- PySpark's pickles of nested messages load.
### How was this patch tested?
- **New tests in `test_proto_rust_offline.py`.**
- `test_python_thread_runs_before_operations_on_large_messages_finish`,
for each of parse, constructor, `CopyFrom`, `MergeFrom` and deepcopy, with and
without a signal.
- `test_arrays_of_repeated_scalar_fields_are_upbs`.
- `test_messages_that_fail_to_parse_keep_what_they_read_as_upbs`.
- `test_pickles_of_nested_messages_load_as_upbs`.
-
`test_installs_register_the_classes_of_the_protos_only_in_protobufs_database`,
which fails without the restriction to the runtime's own files.
- Cases of `get_or_create`, `append`, `Extensions`, bytes names and field
numbers.
- **Probes of both clients:** the thread pauses and the signal delays of
each operation, the `__array__` dtypes and errors, and the other methods.
- **After the rebase on #785**, with #785's tests:
- `test_proto_rust_offline.py`, including its plugin fixture, threads and
the pure-Python `protobuf` subprocess;
- the plugin-plan test of `test_dropin_offline.py`;
- the Rust tests of `pbmsg/pool.rs` and `spark-connect/src/plan.rs`.
- **Python.** The offline suite of `coverage.yml` on pandas 2 and 3.
- **Rust.**
- `cargo test -p apache-spark-connect -p apache-spark-connect-core
--release` (1644 passed);
- `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs
-p apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0,
`audit_no_stubs.sh` and `scripts/tests`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]