Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-52941

Break checked_cast.h assert_util.h cycles, optimize its implementation

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.9.0
    • Affects Version/s: None
    • Component/s: Internal Code
    • None
    • Fully Compatible
    • Service arch 2020-12-14

      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.

            Assignee:
            billy.donahue@mongodb.com Billy Donahue
            Reporter:
            billy.donahue@mongodb.com Billy Donahue
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: