Opened 10 years ago

Closed 4 years ago

#7960 closed Feature Requests (obsolete)

Optimization for move_assign and assign

Reported by: Antony Polukhin Owned by: ebf
Milestone: To Be Determined Component: variant
Version: Boost 1.53.0 Severity: Optimization
Keywords: variant optimization assignment move Cc:

Description

Current implementation of assignment and move assignment creates a temp variable if direct_assigner or direct_mover fail. We can avoid it, by checking if the type of variable to assign exactly matches one of the variant types. If it matches, we can do assign without creation of temporary variable.

Here is some pseudo-code to show it:

    template <typename T>
    void move_assign(T&& rhs)
    {
        // If direct T-to-T move assignment is not possible...
        detail::variant::direct_mover<T> direct_move(rhs);
        if (this->apply_visitor(direct_move) == false)
        {
            if (is_in_varaint_types<T>::value)
            {
                // ... but T is in varaint types, then destroy 
                // content of storage and assign
                move_assigner visitor(*this, type_to_int<T>::value);
                visitor.internal_visit(rhs, 0);
            } 
            else 
            {
                // ...then convert rhs to variant and assign:
                //
                // While potentially inefficient, the following construction of a
                // variant allows T as any type convertible to one of the bounded
                // types without excessive code redundancy.
                //
                variant temp( detail::variant::move(rhs) );
                variant_assign( detail::variant::move(temp) );
            }
        }
    }

Change History (4)

comment:1 by Antony Polukhin, 9 years ago

(In [86134]) Removed duplicate code in assign<->move_assign, added const modifiers (refs #7960)

comment:2 by Antony Polukhin, 9 years ago

(In [86158]) More noexcept modifiers, especially for some of boost::get(T*) and assign/move_assign helpers (refs #7960)

comment:3 by Antony Polukhin, 9 years ago

(In [86650]) Merge from trunk:

  • dropped support of antique compilers
  • fixed issue with ambiguity in swap (fixes #2839)
  • added conditional noexcepts to move assignments, default and move constructors (fixes #7911)
  • experimental variadic templates support (refs #9163)
  • bunch of size optimizations for assignments and move assignments (refs #7960)
  • minor changes and size optimizations

comment:4 by Antony Polukhin, 4 years ago

Resolution: obsolete
Status: newclosed

Closing as the performance is OK now.

Note: See TracTickets for help on using tickets.