HyukjinKwon opened a new pull request, #820:
URL: https://github.com/apache/spark-connect-rust/pull/820
### What changes were proposed in this pull request?
A follow-up of #811 (SPARK-61918), from a round-3 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. Maps look their keys up in constant time.**
- `MapContainer::find` scanned the entries, so a large map was quadratic and
held the GIL. At 30k entries, the parse, the constructor, `update`, `MergeFrom`
and `==` each took about 1.4 s, with every other thread paused that long. A
1M-entry map did not finish in 300 s.
- Maps now keep their entries in a new `Entries`: a Vec in insertion order,
which json_format and iteration keep reading, plus a hash table of their
positions. A removal leaves a hole, and the holes are dropped all at once when
they outnumber the entries, so deleting keys stays fast.
- The hash table is hashbrown's, which holds no copy of the keys. hashbrown
0.17 is a new direct dependency of `pyspark-rs`, but already in the build and
the wheel through Arrow and indexmap, so `LICENSE-binary` is unchanged.
| 30k entries | before: time / longest pause of another thread | this PR |
upb |
|---|---|---|---|
| parse | 1.50 s / 1.50 s | 0.023 s / 6 ms | 2 to 29 ms |
| constructor | 1.37 s / 1.36 s | 0.024 s / 6 ms | |
| `update` | 1.37 s / 1.37 s | 0.023 s / 6 ms | |
| `MergeFrom` | 1.32 s / 1.32 s | 0.014 s / 5 ms | |
| `==` | 1.54 s | 0.003 s | |
**2. One interrupt budget covers each operation.**
- #811's budget was per call. So a constructor given a list of many
messages, such as `Literal.Array(elements=<1M>)`, started a fresh budget per
element and never yielded: another thread waited 1.4 s, and a signal handler
ran 1.35 s late. It also raised and swallowed an `AttributeError` per element
through `hasattr("keys")`.
- One budget is now threaded through the constructor (`init_from_mapping`,
`set_fields_of`, `message_from_value`), `update_from`, map updates, merges and
copies, `extend` of a repeated message field, and the deepcopy of a repeated
field. It counts a step per message, per value and per map entry.
- A value of the field's message type is taken before any `hasattr` check.
- For 1M messages, the constructor of a list now takes 0.73 s, with another
thread paused at most 92 ms and a SIGALRM handled at once. upb's takes 0.52 to
0.75 s and holds the GIL throughout.
- `==` holds the GIL, as upb's does. It now compares only the fields that
either message sets, in place: 0.05 s for 1M messages, where upb takes 0.14 s
and #811 took 0.32 s.
**3. `UnknownFieldSet` of a message of the runtime is protobuf's.**
- `google.protobuf.unknown_fields.UnknownFieldSet(msg)` and
`text_format.MessageToString(msg, print_unknown_fields=True)` raised
`TypeError: Expected a message object`.
- `install()` now wraps `UnknownFieldSet`. For a message of the runtime, it
builds protobuf's own set from the message's unknown bytes (the new
`_pyspark._proto_unknown_fields`), so the fields, groups, order and errors are
upb's. Any other argument goes to protobuf's class as is, `isinstance` works,
and installing again wraps protobuf's class, not the wrapper.
**4. Docs.** The figures of `docs/compatibility.md` are corrected:
- 0.3 s for the parse of a million messages, where upb takes 0.05 s;
- 0.07, 0.05 and 0.07 s for `SerializeToString`, `==` and the release of a
message, where upb takes 0.04 s, 0.14 s and 2 ms;
- the exact list of operations that yield, and where upb itself yields;
- the wrapper's limits.
### Why are the changes needed?
A large map froze every Python thread for seconds or minutes, and a
constructor of many messages held them and signals as long as it ran. Code that
reads unknown fields failed on the messages of the runtime.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- maps are fast at any size;
- more operations yield to other threads and signals;
- `UnknownFieldSet` and `print_unknown_fields` work for the runtime's
messages.
Remaining differences, documented:
- A map copy or `MergeFrom` snapshots the source under one borrow: about
0.25 s for 1M entries, where upb's `MergeFrom` yields.
- `type(s) is unknown_fields.UnknownFieldSet` is False.
- A `from google.protobuf.unknown_fields import UnknownFieldSet` bound
before `pyspark.sql.connect.proto` is imported still refuses the runtime's
messages.
### How was this patch tested?
- **New tests in `test_proto_rust_offline.py`.**
-
`test_maps_keep_the_order_of_insertion_and_upbs_entries_through_removals`:
random operations against a dict model and upb.
- `test_operations_on_maps_of_many_keys_take_no_scan_of_their_keys`: 100k
keys.
- `test_unknown_field_sets_of_the_messages_are_upbs`, which also runs
under protobuf's pure-Python implementation.
- Six new cases of
`test_python_thread_runs_before_operations_on_large_messages_finish`: the
deepcopy of a container, the constructor of a list, `extend`, and a map's
parse, constructor and update.
- A Rust unit test of `Entries` against a list model.
- **Probes of both clients**: the timings above, and identical output for
the unknown fields' API, their errors, `isinstance` and text.
- **Python.**
- `test_proto_rust_offline.py` on pandas 2 and 3.
- The offline suite of `coverage.yml`.
- Live `test_dropin_e2e.py` and `test_arrow_e2e.py`.
- **Rust.** `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
--all-targets -- -D warnings`, ruff 0.14.0, `audit_no_stubs.sh` and
`scripts/tests`;
- `gen_binary_licenses.py --check`.
### 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]