-
Notifications
You must be signed in to change notification settings - Fork 763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make GILOnceCell::get_or_try_init_type_ref
public
#4542
Conversation
1e65734
to
5b16662
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change
PyModule::import
suggestion toPython::import
instead
Why? I don't have a strong opinion either way, but slightly prefer PyModule::import
.
src/sync.rs
Outdated
/// # }); | ||
/// ``` | ||
/// | ||
pub fn get_or_try_init_type_ref<'py>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can use a better name. Maybe just "import"? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but then maybe it should be implemented for GILOnceCell<Py<T>> where T: PyTypeInfo
instead of GILOnceCell<Py<PyType>>
? Otherwise, I would call it import_type
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be fine with that. I think you want T: PyTypeCheck
rather than T: PyTypeInfo
, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, done in 79568f6!
Since |
I prefer |
From a documentation perspective I would prefer that people visit Also, using "constructor" syntax also makes it clear that when they use it, they're getting a module (rather than something arbitrary like |
245dae4
to
79568f6
Compare
@davidhewitt @mejrs Fair enough, I reverted the guide back to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late review. I have a couple of nits about the documentation.
src/sync.rs
Outdated
impl<T> GILOnceCell<Py<T>> | ||
where | ||
T: PyTypeCheck, | ||
{ | ||
/// Get a reference to the contained Python type, initializing it if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify that "it" refers to the GILOnceCell, not the class in it
/// Get a reference to the contained Python type, initializing it if needed. | |
/// Get a reference to the contained Python type, initializing the cell if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! bcb883c
[`PyModule::import`] introduces an overhead each time it's called. To avoid this in functions that are called multiple times, | ||
using a [`GILOnceCell`]({{#PYO3_DOCS_URL}}/pyo3/sync/struct.GILOnceCell.html) is recommended. Specifically, for importing types, | ||
[`GILOnceCell::import`]({{#PYO3_DOCS_URL}}/pyo3/sync/struct.GILOnceCell.html#method.import) is provided | ||
(check out the [example]({{#PYO3_DOCS_URL}}/pyo3/sync/struct.GILOnceCell.html#example-using-giloncecell-to-avoid-the-overhead-of-importing-a-class-multiple-times)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is not the right place for this.
- I would like to keep this section of the guide as concise as possible.
GILOnceCell::import
does not work in the context of the example becausesum
is not a class, which would be confusing.- Please avoid terminology like "overhead" or "is recommended". Just say what it does, for example, "if you want to import a class, you can store a reference to it with
GILOnceCell::import
".
I think such a footnote would fit better on https://pyo3.rs/main/doc/pyo3/types/struct.pymodule#method.import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, just made those changes in 46b18cc.
However, since I changed the implementation from PyType
to T: PyTypeCheck
, this does work:
#[pyfunction]
fn sum<'py>(py: Python<'py>, l: Py<PyList>) -> PyResult<Bound<'py, PyAny>> {
static SUM: GILOnceCell<Py<PyAny>> = GILOnceCell::new();
SUM.import(py, "builtins", "sum")?
.call1((l,))
}
I guess this raises some questions. Is there any benefit in storing functions such as sum
in the GILOnceCell
, too? If not, should we prevent it (maybe by changing it back to PyType
)? And if yes, should we be explicit in the docs that this is not only for importing classes?
src/sync.rs
Outdated
/// # Example: Using `GILOnceCell` to avoid the overhead of importing a class multiple times | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to just say what the example does, this makes for nicer URLs as well.
/// # Example: Using `GILOnceCell` to avoid the overhead of importing a class multiple times | |
/// | |
/// # Example: Using `GILOnceCell` to store a class in a static variable. | |
/// | |
/// `GILOnceCell` can be used to avoid importing a class multiple times |
(this also explicitly mentions "static", which is important. We don't want people to have local (or worse, const
) GILOnceCells).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: 5d95fba
Co-authored-by: Bruno Kolenbrander <59372212+mejrs@users.noreply.github.com>
Co-authored-by: Bruno Kolenbrander <59372212+mejrs@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Resolves #4516.
GILOnceCell::get_or_try_init_type_ref
toGILOnceCell::import
and make it public API, adding an example for it.GILOnceCell::import
.Python::eval_bound
(which is deprecated) toPython::eval
.Python::run_bound
(which is deprecated) toPython::run
, also adding missing link.