Skip to content
Snippets Groups Projects
Forked from Core Modules / dune-common
Source project has a limited visibility.
  • Steffen Müthing's avatar
    b4328de2
    [FieldVector] Change FieldVector default constructor to default-initialize data · b4328de2
    Steffen Müthing authored
    The default constructor of the FieldVector has intentionally avoided calling a
    constructor for the contained data to eliminate the potential overhead of that
    initialization.
    During implementation of a new creation mode of the BCRSMatrix, it was discovered
    that this behavior is problematic due to its inconsistency: If the data type
    contained in the FieldVector is a non-trivial type, a default-constructed
    FieldVector will be fully initialized. On the other hand, with a primitive data
    type it will be in an uninitialized state, potentially causing undefined behavior.
    
    As this behavior might be very unintuitive for users and made implementing the new,
    more efficient creation method for the BCRS matrix very difficult to implement, this
    patch changes the FieldVector default constructor to always default-construct its
    contents regardless of its type. While this change does introduce a possible
    performance regression (for primitive types, the compiler now has to zero out the
    memory), the overhead has been measured to be minimal.
    
    The actual implementation prefers to use the new C++11 unified initialization
    syntax and falls back to std::fill for older compilers. The distinction was made
    because newer compilers generate much more efficient code when using unified
    initialization, and for user-defined types, this approach avoids first calling the
    default constructors and then the copy assignment operator.
    b4328de2
    History
    [FieldVector] Change FieldVector default constructor to default-initialize data
    Steffen Müthing authored
    The default constructor of the FieldVector has intentionally avoided calling a
    constructor for the contained data to eliminate the potential overhead of that
    initialization.
    During implementation of a new creation mode of the BCRSMatrix, it was discovered
    that this behavior is problematic due to its inconsistency: If the data type
    contained in the FieldVector is a non-trivial type, a default-constructed
    FieldVector will be fully initialized. On the other hand, with a primitive data
    type it will be in an uninitialized state, potentially causing undefined behavior.
    
    As this behavior might be very unintuitive for users and made implementing the new,
    more efficient creation method for the BCRS matrix very difficult to implement, this
    patch changes the FieldVector default constructor to always default-construct its
    contents regardless of its type. While this change does introduce a possible
    performance regression (for primitive types, the compiler now has to zero out the
    memory), the overhead has been measured to be minimal.
    
    The actual implementation prefers to use the new C++11 unified initialization
    syntax and falls back to std::fill for older compilers. The distinction was made
    because newer compilers generate much more efficient code when using unified
    initialization, and for user-defined types, this approach avoids first calling the
    default constructors and then the copy assignment operator.