From 2096e30bca88554f42460de0dafdbffb4297fb5c Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Sat, 25 Jun 2022 07:04:38 +0100 Subject: [PATCH] opt: fix generic code bloat in LazyStaticType::get_or_init --- src/type_object.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/type_object.rs b/src/type_object.rs index d0693df9..35f6d05c 100644 --- a/src/type_object.rs +++ b/src/type_object.rs @@ -105,7 +105,15 @@ impl LazyStaticType { } pub fn get_or_init(&self, py: Python<'_>) -> *mut ffi::PyTypeObject { - let type_object = *self.value.get_or_init(py, || create_type_object::(py)); + fn inner() -> *mut ffi::PyTypeObject { + // Safety: `py` is held by the caller of `get_or_init`. + let py = unsafe { Python::assume_gil_acquired() }; + create_type_object::(py) + } + + // Uses explicit GILOnceCell::get_or_init:: *mut ffi::PyTypeObject> monomorphization + // so that only this one monomorphization is instantiated (instead of one closure monormization for each T). + let type_object = *self.value.get_or_init:: *mut ffi::PyTypeObject>(py, inner::); self.ensure_init(py, type_object, T::NAME, &T::for_all_items); type_object }