Limit the intern! macro to strings and intern the string contents in addition to the reference.

This commit is contained in:
Adam Reichold 2022-04-03 20:58:51 +02:00
parent 125bf416a2
commit f777372eed
2 changed files with 8 additions and 11 deletions

View File

@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added methods on `InterpreterConfig` to run Python scripts using the configured executable. [#2092](https://github.com/PyO3/pyo3/pull/2092) - Added methods on `InterpreterConfig` to run Python scripts using the configured executable. [#2092](https://github.com/PyO3/pyo3/pull/2092)
- Added FFI definitions for `PyType_FromModuleAndSpec`, `PyType_GetModule`, `PyType_GetModuleState` and `PyModule_AddType`. [#2250](https://github.com/PyO3/pyo3/pull/2250) - Added FFI definitions for `PyType_FromModuleAndSpec`, `PyType_GetModule`, `PyType_GetModuleState` and `PyModule_AddType`. [#2250](https://github.com/PyO3/pyo3/pull/2250)
- Add `PyString::intern` to enable usage of the Python's built-in string interning. [#2268](https://github.com/PyO3/pyo3/pull/2268) - Add `PyString::intern` to enable usage of the Python's built-in string interning. [#2268](https://github.com/PyO3/pyo3/pull/2268)
- Add `intern!` macro which can be used to amortize the cost of creating Python objects by storing them inside a `GILOnceCell`. [#2269](https://github.com/PyO3/pyo3/pull/2269) - Add `intern!` macro which can be used to amortize the cost of creating Python strings by storing them inside a `GILOnceCell`. [#2269](https://github.com/PyO3/pyo3/pull/2269)
### Changed ### Changed

View File

@ -111,12 +111,11 @@ impl<T> GILOnceCell<T> {
} }
} }
/// Converts `value` into a Python object and stores it in static storage. The same Python object /// Interns `text` as a Python string and stores a reference to it in static storage.
/// is returned on each invocation.
/// ///
/// Because it is stored in a static, this object's destructor will not run. /// A reference to the same Python string is returned on each invocation.
/// ///
/// # Example: Using `intern!` to avoid needlessly recreating the same object /// # Example: Using `intern!` to avoid needlessly recreating the same Python string
/// ///
/// ``` /// ```
/// use pyo3::intern; /// use pyo3::intern;
@ -126,7 +125,7 @@ impl<T> GILOnceCell<T> {
/// fn create_dict(py: Python<'_>) -> PyResult<&PyDict> { /// fn create_dict(py: Python<'_>) -> PyResult<&PyDict> {
/// let dict = PyDict::new(py); /// let dict = PyDict::new(py);
/// // 👇 A new `PyString` is created /// // 👇 A new `PyString` is created
/// // for every call of this function /// // for every call of this function.
/// dict.set_item("foo", 42)?; /// dict.set_item("foo", 42)?;
/// Ok(dict) /// Ok(dict)
/// } /// }
@ -142,14 +141,12 @@ impl<T> GILOnceCell<T> {
/// ``` /// ```
#[macro_export] #[macro_export]
macro_rules! intern { macro_rules! intern {
($py: expr, $value: expr) => {{ ($py: expr, $text: literal) => {{
static INTERNED: $crate::once_cell::GILOnceCell<$crate::PyObject> = static INTERNED: $crate::once_cell::GILOnceCell<$crate::Py<$crate::types::PyString>> =
$crate::once_cell::GILOnceCell::new(); $crate::once_cell::GILOnceCell::new();
INTERNED INTERNED
.get_or_init($py, || { .get_or_init($py, || $crate::types::PyString::intern($py, $text).into())
$crate::conversion::ToPyObject::to_object($value, $py)
})
.as_ref($py) .as_ref($py)
}}; }};
} }