Skip to content

Commit 877d028

Browse files
committed
[PY] Drop FFI String cache when pickling
Pickled FFI strings should only persist their Python string payload, avoiding serialization of the hidden cached FFI backing object.
1 parent 5b7c178 commit 877d028

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

python/tvm_ffi/cython/string.pxi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class String(str, PyNativeObject):
5757
val._tvm_ffi_cached_object = None
5858
return val
5959

60+
def __reduce_ex__(self, protocol):
61+
return (type(self), (str(self),))
62+
6063
# pylint: disable=no-self-argument
6164
def __from_tvm_ffi_object__(cls, obj: Any) -> "String":
6265
"""Construct a ``String`` from an FFI object (internal)."""

tests/python/test_string.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pickle
1919

2020
import tvm_ffi
21+
from tvm_ffi.core import TypeSchema, _to_py_class_value
2122

2223

2324
def test_string() -> None:
@@ -34,6 +35,15 @@ def test_string() -> None:
3435
s4 = pickle.loads(pickle.dumps(s))
3536
assert s4 == "hello"
3637

38+
cached = _to_py_class_value(TypeSchema("str").convert("x" * 200))
39+
assert isinstance(cached, tvm_ffi.core.String)
40+
assert cached._tvm_ffi_cached_object is not None
41+
42+
cached_roundtrip = pickle.loads(pickle.dumps(cached))
43+
assert isinstance(cached_roundtrip, tvm_ffi.core.String)
44+
assert cached_roundtrip == cached
45+
assert cached_roundtrip._tvm_ffi_cached_object is None
46+
3747

3848
def test_bytes() -> None:
3949
fecho = tvm_ffi.get_global_func("testing.echo")

0 commit comments

Comments
 (0)