[PY] Support pickling ffi.String without cached FFI object#642
[PY] Support pickling ffi.String without cached FFI object#642Ubospica wants to merge 2 commits into
Conversation
Pickled FFI strings should only persist their Python string payload, avoiding serialization of the hidden cached FFI backing object.
There was a problem hiding this comment.
Code Review
This pull request implements __reduce_ex__ for the String class in Cython to prevent the serialization of the cached FFI object during pickling, and adds corresponding unit tests to verify this behavior. The reviewer suggests extending this fix to the Bytes class, which shares the same caching behavior, by implementing __reduce_ex__ and adding a corresponding test case.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Extend the pickle behavior to FFI Bytes so cached backing objects are not serialized, matching String.
|
Addressed the Gemini comments in db208dc:
Local validation:
|
|
Superseded by #643, which is based on the latest and includes the Gemini Bytes review fixes without the dirty merge state. |
|
Closing in favor of #643. |
## Summary - Add pickle reductions for `tvm_ffi.core.String` and `tvm_ffi.core.Bytes` so only the Python payload is serialized. - Ensure unpickled values drop the hidden `_tvm_ffi_cached_object` backing object. - Add regression coverage for cached FFI String/Bytes returned from `testing.echo`. ## Notes - This is a clean branch based on the latest `apache/main` and supersedes #642, which became dirty after main advanced. - `Bytes.__slots__` is intentionally unchanged because Python does not support non-empty `__slots__` on `bytes` subclasses. ## Test plan - `uv pip install --reinstall --verbose . && .venv/bin/pytest -vvs tests/python/test_string.py` - `PYTHONPATH=/raid/user_data/yixind/miniforge3/lib/python3.12/site-packages pre-commit run --files python/tvm_ffi/cython/string.pxi tests/python/test_string.py`
Summary
tvm_ffi.core.Stringso only the Python string payload is serialized._tvm_ffi_cached_objecton unpickle, sinceStringis effectivelystrwith optional FFI backing.