From 82e372e95efe748c908e1889a3ab0d14273592a4 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Mon, 31 Aug 2026 09:22:54 +0800 Subject: [PATCH] Reduce direct RTTI to a single reflection direct_rtti added three conventions to carry proxy_cast, so every proxiable pointer paid three function pointers of metadata and every cast went through one of them. The typeid reflection that the same skill installs already records the contained type, and once that type is known to match the requested one the cast is a static_cast on the pointer storage, so the indirection buys nothing. direct_rtti now adds one reflection. direct_rtti_reflector inherits proxy_typeid_reflector for the type identity and reuses the accessor generated for proxy_cast_dispatch, so the five proxy_cast overloads and their qualifier mapping stay in one place. proxy_cast_accessor_impl gained a private invoke_cast that tests the reflected type and then invokes the same dispatch through an erased context with the contained type known statically. The indirect path keeps its conventions and compiles to the same code as before. The metadata of a facade built from direct_rtti alone drops from 40 bytes to 16, and the lvalue, pointer and rvalue casts compile to 27, 29 and 34 instructions in place of 46, 37 and 66. No indirect call is left on the success path. The rvalue form keeps one on the type mismatch branch, where the proxy is reset through the erased destructor. --- include/proxy/v4/detail/skills.h | 46 ++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/include/proxy/v4/detail/skills.h b/include/proxy/v4/detail/skills.h index adee855..c927bf3 100644 --- a/include/proxy/v4/detail/skills.h +++ b/include/proxy/v4/detail/skills.h @@ -163,7 +163,7 @@ struct proxy_cast_accessor_impl { .is_ref = true, .is_const = std::is_const_v, .result_ptr = &result}; - invoke(static_cast(self), ctx); + invoke_cast(static_cast(self), ctx); if (result == nullptr) [[unlikely]] { PRO4D_THROW(bad_proxy_cast{}); } @@ -174,7 +174,7 @@ struct proxy_cast_accessor_impl { .is_ref = false, .is_const = false, .result_ptr = &result}; - invoke(static_cast(self), ctx); + invoke_cast(static_cast(self), ctx); if (!result.has_value()) [[unlikely]] { PRO4D_THROW(bad_proxy_cast{}); } @@ -190,9 +190,32 @@ struct proxy_cast_accessor_impl { .is_ref = true, .is_const = std::is_const_v, .result_ptr = &result}; - invoke(*self, ctx); + invoke_cast(*self, ctx); return static_cast(result); } + +private: + template + static void invoke_cast(Self self, const proxy_cast_context& cast_ctx) { + if constexpr (specialization_of, proxy>) { + using F = std::remove_cvref_t::facade_type; + constexpr bool is_rv = + overload_traits::this_qualifier == qualifier_type::rv; + if (proxy_typeid(self) == *cast_ctx.type_ptr) [[likely]] { + erased_context ctx{proxy_helper::get_ptr(self)}; + if constexpr (is_rv) { + proxy_helper::meta_resetting_guard guard{self}; + invoke>(ctx, cast_ctx); + } else { + invoke>(ctx, cast_ctx); + } + } else if constexpr (is_rv) { + self.reset(); + } + } else { + invoke(static_cast(self), cast_ctx); + } + } }; #define PRO4D_DEF_PROXY_CAST_ACCESSOR(oq, pq, ne, ...) \ @@ -242,6 +265,17 @@ struct proxy_typeid_reflector { const std::type_info* info; }; + +struct direct_rtti_reflector : proxy_typeid_reflector { + using proxy_typeid_reflector::proxy_typeid_reflector; + + template + struct PRO4D_ENFORCE_EBO accessor + : proxy_typeid_reflector::accessor, + proxy_cast_dispatch::accessor< + Self, proxy_cast_dispatch, void(proxy_cast_context) &, + void(proxy_cast_context) const&, void(proxy_cast_context) &&> {}; +}; #endif // __cpp_rtti >= 199711L } // namespace detail @@ -270,11 +304,7 @@ using indirect_rtti = FB::template add_indirect_convention< template using direct_rtti = - FB::template add_direct_convention:: - template add_direct_reflection; + FB::template add_direct_reflection; template using rtti = indirect_rtti;