diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 1cd9af0bd..21e966cfe 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -226,12 +226,14 @@ extern "C" inline void pybind11_meta_dealloc(PyObject *obj) { local_internals.registered_types_cpp.erase(tinfo->cpptype); } else { internals.registered_types_cpp.erase(tindex); +#if PYBIND11_INTERNALS_VERSION >= 12 internals.registered_types_cpp_fast.erase(tinfo->cpptype); for (const std::type_info *alias : tinfo->alias_chain) { auto num_erased = internals.registered_types_cpp_fast.erase(alias); (void) num_erased; assert(num_erased > 0); } +#endif } internals.registered_types_py.erase(tinfo->type); diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 11a2ee4c9..534751153 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -39,11 +39,11 @@ /// further ABI-incompatible changes may be made before the ABI is officially /// changed to the new version. #ifndef PYBIND11_INTERNALS_VERSION -# define PYBIND11_INTERNALS_VERSION 12 +# define PYBIND11_INTERNALS_VERSION 11 #endif -#if PYBIND11_INTERNALS_VERSION < 12 -# error "PYBIND11_INTERNALS_VERSION 12 is the minimum for all platforms for pybind11v3." +#if PYBIND11_INTERNALS_VERSION < 11 +# error "PYBIND11_INTERNALS_VERSION 11 is the minimum for all platforms for pybind11v3." #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -242,12 +242,14 @@ struct internals { pymutex mutex; pymutex exception_translator_mutex; #endif +#if PYBIND11_INTERNALS_VERSION >= 12 // non-normative but fast "hint" for registered_types_cpp. Meant // to be used as the first level of a two-level lookup: successful // lookups are correct, but unsuccessful lookups need to try // registered_types_cpp and then backfill this map if they find // anything. fast_type_map registered_types_cpp_fast; +#endif // std::type_index -> pybind11's type information type_map registered_types_cpp; @@ -273,6 +275,9 @@ struct internals { PyObject *instance_base = nullptr; // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined: thread_specific_storage tstate; +#if PYBIND11_INTERNALS_VERSION <= 11 + thread_specific_storage loader_life_support_tls; // OBSOLETE (PR #5830) +#endif // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined: PyInterpreterState *istate = nullptr; @@ -353,6 +358,7 @@ struct type_info { void *(*module_local_load)(PyObject *, const type_info *) = nullptr; holder_enum_t holder_enum_v = holder_enum_t::undefined; +#if PYBIND11_INTERNALS_VERSION >= 12 // When a type appears in multiple DSOs, // internals::registered_types_cpp_fast will have multiple distinct // keys (the std::type_info from each DSO) mapped to the same @@ -363,6 +369,7 @@ struct type_info { // nb_alias_chain` added in // https://github.com/wjakob/nanobind/commit/b515b1f7f2f4ecc0357818e6201c94a9f4cbfdc2 std::forward_list alias_chain; +#endif /* A simple type never occurs as a (direct or indirect) parent * of a class that makes use of multiple inheritance. diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 21b7f0950..b0c59e113 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -227,26 +227,32 @@ inline detail::type_info *get_global_type_info_lock_held(const std::type_info &t // next time. detail::type_info *type_info = nullptr; auto &internals = get_internals(); +#if PYBIND11_INTERNALS_VERSION >= 12 auto &fast_types = internals.registered_types_cpp_fast; +#endif auto &types = internals.registered_types_cpp; +#if PYBIND11_INTERNALS_VERSION >= 12 auto fast_it = fast_types.find(&tp); if (fast_it != fast_types.end()) { -#ifndef NDEBUG +# ifndef NDEBUG auto types_it = types.find(std::type_index(tp)); assert(types_it != types.end()); assert(types_it->second == fast_it->second); -#endif +# endif return fast_it->second; } +#endif // PYBIND11_INTERNALS_VERSION >= 12 auto it = types.find(std::type_index(tp)); if (it != types.end()) { +#if PYBIND11_INTERNALS_VERSION >= 12 // We found the type in the slow map but not the fast one, so // some other DSO added it (otherwise it would be in the fast // map under &tp) and therefore we must be an alias. Record // that. it->second->alias_chain.push_front(&tp); fast_types.emplace(&tp, it->second); +#endif type_info = it->second; } return type_info; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 8bd62c85c..91b38d91e 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1692,7 +1692,9 @@ protected: local_internals.registered_types_cpp[rec.type] = tinfo; } else { internals.registered_types_cpp[tindex] = tinfo; +#if PYBIND11_INTERNALS_VERSION >= 12 internals.registered_types_cpp_fast[rec.type] = tinfo; +#endif } PYBIND11_WARNING_PUSH @@ -2199,7 +2201,9 @@ public: type_info *const val = internals.registered_types_cpp[std::type_index(typeid(type))]; internals.registered_types_cpp[std::type_index(typeid(type_alias))] = val; +#if PYBIND11_INTERNALS_VERSION >= 12 internals.registered_types_cpp_fast[&typeid(type_alias)] = val; +#endif } }); }