From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 5BE813856240; Wed, 3 Aug 2022 12:30:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5BE813856240 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-10930] libstdc++: Optimize std::any_cast by replacing indirect call X-Act-Checkin: gcc X-Git-Author: Tim Adye X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 45a31caac6dce9c08fcbdb2436d8ec6dca4e5f3c X-Git-Newrev: d1b51c15312940779b1288b59973d88b9592dd23 Message-Id: <20220803123047.5BE813856240@sourceware.org> Date: Wed, 3 Aug 2022 12:30:47 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Aug 2022 12:30:47 -0000 https://gcc.gnu.org/g:d1b51c15312940779b1288b59973d88b9592dd23 commit r10-10930-gd1b51c15312940779b1288b59973d88b9592dd23 Author: Tim Adye Date: Fri Jun 4 15:59:38 2021 +0100 libstdc++: Optimize std::any_cast by replacing indirect call This significantly improves the performance of std::any_cast, by avoiding an indirect call to the _S_manage function through a function pointer. Before we make that indirect call we've already established that the contained value has the expected type, which means we also know the manager type, and so can call one of its members directly. We also know the precise type in the any::emplace functions, because we've just constructed that type, so we can use the new member there too. That doesn't seem to affect performance, but we might as well use the new _S_access function anyway. Signed-off-by: Tim Adye Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/std/any (any::_Manager::_S_access): New static function to access the contained value. (any::emplace, __any_caster): Use _S_access member of the manager type. (cherry picked from commit f6bb145c0bff19767931d37733be11c8acc6fa00) Diff: --- libstdc++-v3/include/std/any | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any index 1428858d600..de9569b6d40 100644 --- a/libstdc++-v3/include/std/any +++ b/libstdc++-v3/include/std/any @@ -263,9 +263,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _VTp = decay_t<_Tp>; __do_emplace<_VTp>(std::forward<_Args>(__args)...); - any::_Arg __arg; - this->_M_manager(any::_Op_access, this, &__arg); - return *static_cast<_VTp*>(__arg._M_obj); + return *any::_Manager<_VTp>::_S_access(_M_storage); } /// Emplace with an object created from @p __il and @p __args as @@ -276,9 +274,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { using _VTp = decay_t<_Tp>; __do_emplace<_VTp, _Up>(__il, std::forward<_Args>(__args)...); - any::_Arg __arg; - this->_M_manager(any::_Op_access, this, &__arg); - return *static_cast<_VTp*>(__arg._M_obj); + return *any::_Manager<_VTp>::_S_access(_M_storage); } // modifiers @@ -384,6 +380,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void* __addr = &__storage._M_buffer; ::new (__addr) _Tp(std::forward<_Args>(__args)...); } + + static _Tp* + _S_access(const _Storage& __storage) + { + // The contained object is in __storage._M_buffer + const void* __addr = &__storage._M_buffer; + return static_cast<_Tp*>(const_cast(__addr)); + } }; // Manage external contained object. @@ -405,6 +409,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __storage._M_ptr = new _Tp(std::forward<_Args>(__args)...); } + static _Tp* + _S_access(const _Storage& __storage) + { + // The contained object is in *__storage._M_ptr + return static_cast<_Tp*>(__storage._M_ptr); + } }; }; @@ -516,9 +526,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif ) { - any::_Arg __arg; - __any->_M_manager(any::_Op_access, __any, &__arg); - return __arg._M_obj; + return any::_Manager<_Up>::_S_access(__any->_M_storage); } return nullptr; }