[SERVER-52941] Break checked_cast.h assert_util.h cycles, optimize its implementation Created: 19/Nov/20  Updated: 29/Oct/23  Resolved: 08/Dec/20

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 4.9.0

Type: Improvement Priority: Major - P3
Reporter: Billy Donahue Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-52904 Improve implementation of mongo::Status Closed
Backwards Compatibility: Fully Compatible
Sprint: Service arch 2020-12-14
Participants:

 Description   

Only needs invariant.h, should not include assert_util.h.

base/checked_cast.h relies on invariant() to perform its checks and needs no other assert_util.h declarations. The errant assert_util.h dependency makes checked_cast.h inaccessible to any header that assert_util.h depends on (we need it for status.h and can't currently have it). This is an unnecessary restriction. It can be whitelisted to use the invariant.h header just like string_data.h and the small set of other assert_util.h dependencies.

As for the content of checked_cast.h:

 
template <typename T, typename U>
T checked_cast(const U& u);
template <typename T, typename U>
T checked_cast(U& u);
 
template <typename T, typename U>
std::shared_ptr<T> checked_pointer_cast(const std::shared_ptr<U>& u);

We should provide && overloads for these. A checked_cast of an rvalue should be a move, not a copy. Casting a shared_ptr&& should not increment its refcount!
In C++20 the underlying std::static_pointer_cast and std::dynamic_pointer_cast are getting && argument overloads to avoid this cost. There's no reason we shouldn't be ready for that.

The implementation of these functions is a very C++98 dispatch to a static member function of a struct template specialization, keyed off of bool kDebugBuild. Super old school. This is silly in C++17 where we have if constexpr and can write that kind of dispatching very naturally in a single function body.

There's some urgency to this as it blocks an improvement to status.h.



 Comments   
Comment by Githook User [ 04/Dec/20 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-52941 checked_cast.h include invariant.h, `&&`-awareness
Branch: master
https://github.com/mongodb/mongo/commit/1e86b5adfa3654a3c3860587dbdc087bb63779e0

Comment by Billy Donahue [ 29/Nov/20 ]

CR https://mongodbcr.appspot.com/720550001

Generated at Thu Feb 08 05:29:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.