Tuesday, August 9, 2022

Watch out for MSVC's std::map's move constructor

move constructor

 The other day I found out that MSVC's std::map move constructor doesn't have noexcept:

map(map&& _Right) : _Mybase(_STD move(_Right)) {}

This means that it can throw when moved and it's not eligible for move_if_noexcept which is used in vector reallocation's. I filed a report but it got rejected. Take care when you want map's to be moveable (which can be a factor compared to copying all nodes).

Friday, July 15, 2022

Watch out for vector one argument constructor

vector constructor

 Aggregate initialization brought us many good things. There is though one nasty pitfall with std::vector. std::vector can be initialized with a size_t to specify its initial size. There is a potential ambiguity since the vector also accepts size_t as its element type:


#include <cassert>
#include <vector>

int main()
{
   std::vector<size_t>  vec0(5);   // vector size is set
   std::vector<size_t>  vec1{5};   // vector size isn't set directly but vector is initialized with 1 element

   assert(vec0.size() == 5);
   assert(vec1.size() == 1);

   std::vector<size_t>  vec2{vec0.size()};  // even worse: vectors do not have the same size now

   return 0;
} 

This is especially a problem with 32 bits code where size_t is an int typedef. A solution is that the C++ committee resolves the ambiguity e.g. by introducing a 2 argument overload for vector constructor (with an extra enum). Until then it's watching out 

Reference:

Wednesday, July 6, 2022

Boost include spam

#include

 Boost libraries are a great addition to C++. However anyone working with Boost lirbaries know that they explode your compile times. For example include adjacency_list for Boost.Graph lead to the following spam:


graph\adjacency_list.hpp
 unordered_set.hpp
  unordered/unordered_set.hpp
   core/explicit_operator_bool.hpp
   functional/hash.hpp
    container_hash/hash.hpp
   move/move.hpp
    move/detail/config_begin.hpp
    move/utility.hpp
     move/detail/config_begin.hpp
     move/detail/config_end.hpp
    move/algorithm.hpp
     move/detail/config_begin.hpp
     move/detail/config_end.hpp
    move/detail/config_end.hpp
   unordered/detail/set.hpp
    unordered/detail/implementation.hpp
     assert.hpp
      C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
     core/allocator_traits.hpp
      core/allocator_access.hpp
       core/pointer_traits.hpp
     core/bit.hpp
      cstdint.hpp
     swap.hpp
     type_traits/is_base_of.hpp
     type_traits/is_nothrow_move_assignable.hpp
      type_traits/has_trivial_move_assign.hpp
      type_traits/enable_if.hpp
     type_traits/is_nothrow_move_constructible.hpp
     type_traits/is_nothrow_swappable.hpp
     type_traits/make_void.hpp
     unordered/detail/fwd.hpp
      predef.h
       predef/language.h
        predef/language/stdc.h
         predef/version_number.h
         predef/make.h
          predef/detail/test.h
        predef/language/stdcpp.h
         predef/make.h
        predef/language/objc.h
         predef/make.h
        predef/language/cuda.h
         predef/make.h
       predef/architecture.h
        predef/architecture/alpha.h
         predef/make.h
        predef/architecture/arm.h
         predef/make.h
        predef/architecture/blackfin.h
         predef/make.h
        predef/architecture/convex.h
         predef/make.h
        predef/architecture/e2k.h
         predef/make.h
        predef/architecture/ia64.h
         predef/make.h
        predef/architecture/loongarch.h
         predef/make.h
        predef/architecture/m68k.h
         predef/make.h
        predef/architecture/mips.h
         predef/make.h
        predef/architecture/parisc.h
         predef/make.h
        predef/architecture/ppc.h
         predef/make.h
        predef/architecture/ptx.h
         predef/make.h
        predef/architecture/pyramid.h
         predef/make.h
        predef/architecture/riscv.h
         predef/make.h
        predef/architecture/rs6k.h
         predef/make.h
        predef/architecture/sparc.h
         predef/make.h
        predef/architecture/superh.h
         predef/make.h
        predef/architecture/sys370.h
         predef/make.h
        predef/architecture/sys390.h
         predef/make.h
        predef/architecture/x86.h
         predef/architecture/x86/32.h
          predef/make.h
          predef/architecture/x86.h
           predef/architecture/x86/32.h
           predef/architecture/x86/64.h
            predef/make.h
            predef/architecture/x86.h
             predef/architecture/x86/32.h
             predef/architecture/x86/64.h
         predef/architecture/x86/64.h
        predef/architecture/z.h
         predef/make.h
       predef/compiler.h
        predef/compiler/borland.h
         predef/make.h
        predef/compiler/clang.h
         predef/make.h
        predef/compiler/comeau.h
         predef/make.h
        predef/compiler/compaq.h
         predef/make.h
        predef/compiler/diab.h
         predef/make.h
        predef/compiler/digitalmars.h
         predef/make.h
        predef/compiler/dignus.h
         predef/make.h
        predef/compiler/edg.h
         predef/make.h
        predef/compiler/ekopath.h
         predef/make.h
        predef/compiler/gcc_xml.h
         predef/make.h
        predef/compiler/gcc.h
         predef/compiler/clang.h
         predef/make.h
        predef/compiler/greenhills.h
         predef/make.h
        predef/compiler/hp_acc.h
         predef/make.h
        predef/compiler/iar.h
         predef/make.h
        predef/compiler/ibm.h
         predef/make.h
        predef/compiler/intel.h
         predef/make.h
        predef/compiler/kai.h
         predef/make.h
        predef/compiler/llvm.h
         predef/compiler/clang.h
         predef/make.h
        predef/compiler/metaware.h
         predef/make.h
        predef/compiler/metrowerks.h
         predef/make.h
        predef/compiler/microtec.h
         predef/make.h
        predef/compiler/mpw.h
         predef/make.h
        predef/compiler/nvcc.h
         predef/make.h
        predef/compiler/palm.h
         predef/make.h
        predef/compiler/pgi.h
         predef/make.h
        predef/compiler/sgi_mipspro.h
         predef/make.h
        predef/compiler/sunpro.h
         predef/make.h
        predef/compiler/tendra.h
         predef/make.h
        predef/compiler/visualc.h
         predef/compiler/clang.h
         predef/make.h
         predef/detail/comp_detected.h
        predef/compiler/watcom.h
         predef/make.h
       predef/library.h
        predef/library/c.h
         predef/library/c/_prefix.h
          predef/detail/_cassert.h
           C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\cassert
            C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
         predef/library/c/cloudabi.h
          predef/make.h
         predef/library/c/gnu.h
          predef/make.h
         predef/library/c/uc.h
          predef/make.h
         predef/library/c/vms.h
          predef/make.h
         predef/library/c/zos.h
          predef/make.h
        predef/library/std.h
         predef/library/std/_prefix.h
          predef/detail/_exception.h
         predef/library/std/cxx.h
          predef/make.h
         predef/library/std/dinkumware.h
          predef/make.h
         predef/library/std/libcomo.h
          predef/make.h
         predef/library/std/modena.h
          predef/make.h
         predef/library/std/msl.h
          predef/make.h
         predef/library/std/roguewave.h
          predef/make.h
         predef/library/std/sgi.h
          predef/make.h
         predef/library/std/stdcpp3.h
          predef/make.h
         predef/library/std/stlport.h
          predef/make.h
         predef/library/std/vacpp.h
          predef/make.h
       predef/os.h
        predef/os/aix.h
         predef/make.h
        predef/os/amigaos.h
         predef/make.h
        predef/os/beos.h
         predef/make.h
        predef/os/bsd.h
         predef/os/macos.h
          predef/os/ios.h
           predef/make.h
          predef/make.h
         predef/make.h
         predef/os/bsd/bsdi.h
          predef/os/bsd.h
           predef/os/bsd/bsdi.h
           predef/os/bsd/dragonfly.h
            predef/os/bsd.h
             predef/os/bsd/bsdi.h
             predef/os/bsd/dragonfly.h
             predef/os/bsd/free.h
              predef/os/bsd.h
               predef/os/bsd/bsdi.h
               predef/os/bsd/dragonfly.h
               predef/os/bsd/free.h
               predef/os/bsd/open.h
                predef/os/bsd.h
                 predef/os/bsd/bsdi.h
                 predef/os/bsd/dragonfly.h
                 predef/os/bsd/free.h
                 predef/os/bsd/open.h
                 predef/os/bsd/net.h
                  predef/os/bsd.h
                   predef/os/bsd/bsdi.h
                   predef/os/bsd/dragonfly.h
                   predef/os/bsd/free.h
                   predef/os/bsd/open.h
                   predef/os/bsd/net.h
               predef/os/bsd/net.h
             predef/os/bsd/open.h
             predef/os/bsd/net.h
           predef/os/bsd/free.h
           predef/os/bsd/open.h
           predef/os/bsd/net.h
         predef/os/bsd/dragonfly.h
         predef/os/bsd/free.h
         predef/os/bsd/open.h
         predef/os/bsd/net.h
         predef/os/bsd/bsdi.h
         predef/os/bsd/dragonfly.h
         predef/os/bsd/free.h
         predef/os/bsd/open.h
         predef/os/bsd/net.h
        predef/os/cygwin.h
         predef/make.h
        predef/os/haiku.h
         predef/make.h
        predef/os/hpux.h
         predef/make.h
        predef/os/irix.h
         predef/make.h
        predef/os/ios.h
        predef/os/linux.h
         predef/make.h
        predef/os/macos.h
        predef/os/os400.h
         predef/make.h
        predef/os/qnxnto.h
         predef/make.h
        predef/os/solaris.h
         predef/make.h
        predef/os/unix.h
         predef/make.h
        predef/os/vms.h
         predef/make.h
        predef/os/windows.h
         predef/make.h
         predef/detail/os_detected.h
       predef/other.h
        predef/other/endian.h
         predef/make.h
         predef/library/c/gnu.h
         predef/os/macos.h
         predef/os/bsd.h
          predef/os/bsd/bsdi.h
          predef/os/bsd/dragonfly.h
          predef/os/bsd/free.h
          predef/os/bsd/open.h
          predef/os/bsd/net.h
         predef/platform/android.h
          predef/make.h
         predef/architecture.h
        predef/other/wordsize.h
         predef/architecture.h
         predef/make.h
        predef/other/workaround.h
       predef/platform.h
        predef/platform/android.h
        predef/platform/cloudabi.h
         predef/make.h
        predef/platform/mingw.h
         predef/make.h
        predef/platform/mingw32.h
         predef/make.h
        predef/platform/mingw64.h
         predef/make.h
        predef/platform/windows_uwp.h
         predef/make.h
         predef/os/windows.h
         C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\ntverp.h
          C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\ntverp.ver
         predef/detail/platform_detected.h
        predef/platform/windows_desktop.h
         predef/make.h
         predef/os/windows.h
         predef/platform/windows_uwp.h
        predef/platform/windows_phone.h
         predef/make.h
         predef/os/windows.h
         predef/platform/windows_uwp.h
        predef/platform/windows_server.h
         predef/make.h
         predef/os/windows.h
         predef/platform/windows_uwp.h
        predef/platform/windows_store.h
         predef/make.h
         predef/os/windows.h
         predef/platform/windows_uwp.h
        predef/platform/windows_system.h
         predef/make.h
         predef/os/windows.h
         predef/platform/windows_uwp.h
        predef/platform/windows_runtime.h
         predef/make.h
         predef/os/windows.h
         predef/platform/windows_phone.h
         predef/platform/windows_store.h
        predef/platform/ios.h
         predef/os/ios.h
       predef/hardware.h
        predef/hardware/simd.h
         predef/hardware/simd/x86.h
          predef/hardware/simd/x86/versions.h
         predef/hardware/simd/x86_amd.h
          predef/hardware/simd/x86_amd/versions.h
         predef/hardware/simd/arm.h
          predef/hardware/simd/arm/versions.h
         predef/hardware/simd/ppc.h
          predef/hardware/simd/ppc/versions.h
       predef/version.h
     utility/addressof.hpp
    unordered/unordered_set_fwd.hpp
     functional/hash_fwd.hpp
 scoped_ptr.hpp
  smart_ptr/scoped_ptr.hpp
   assert.hpp
    C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
   checked_delete.hpp
   smart_ptr/detail/sp_disable_deprecated.hpp
   smart_ptr/detail/operator_bool.hpp
 graph/graph_traits.hpp
  pending/property.hpp
   type_traits.hpp
    type_traits/common_type.hpp
     type_traits/decay.hpp
      type_traits/remove_bounds.hpp
       type_traits/remove_extent.hpp
     type_traits/detail/mp_defer.hpp
    type_traits/conjunction.hpp
    type_traits/copy_cv.hpp
    type_traits/copy_cv_ref.hpp
     type_traits/copy_reference.hpp
    type_traits/disjunction.hpp
    type_traits/extent.hpp
    type_traits/floating_point_promotion.hpp
    type_traits/has_bit_and.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_bit_and_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_bit_or.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_bit_or_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_bit_xor.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_bit_xor_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_complement.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_dereference.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_divides.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_divides_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_equal_to.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_greater.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_greater_equal.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_left_shift.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_left_shift_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_less.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_less_equal.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_logical_and.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_logical_not.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_logical_or.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_minus.hpp
    type_traits/has_minus_assign.hpp
    type_traits/has_modulus.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_modulus_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_multiplies.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_multiplies_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_negate.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_new_operator.hpp
    type_traits/has_not_equal_to.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_nothrow_destructor.hpp
     type_traits/has_trivial_destructor.hpp
    type_traits/has_plus.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_plus_assign.hpp
    type_traits/has_post_decrement.hpp
     type_traits/detail/has_postfix_operator.hpp
    type_traits/has_post_increment.hpp
     type_traits/detail/has_postfix_operator.hpp
    type_traits/has_pre_decrement.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_pre_increment.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_right_shift.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_right_shift_assign.hpp
     type_traits/detail/has_binary_operator.hpp
    type_traits/has_trivial_move_constructor.hpp
    type_traits/has_unary_minus.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_unary_plus.hpp
     type_traits/detail/has_prefix_operator.hpp
    type_traits/has_virtual_destructor.hpp
    type_traits/is_abstract.hpp
    type_traits/is_complex.hpp
     C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\complex
      C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\ymath.h
    type_traits/is_compound.hpp
    type_traits/is_copy_assignable.hpp
     type_traits/is_noncopyable.hpp
    type_traits/is_float.hpp
    type_traits/is_list_constructible.hpp
    type_traits/is_member_object_pointer.hpp
    type_traits/is_object.hpp
    type_traits/is_scoped_enum.hpp
     type_traits/negation.hpp
    type_traits/is_signed.hpp
    type_traits/is_stateless.hpp
    type_traits/is_trivially_copyable.hpp
    type_traits/is_union.hpp
    type_traits/is_unscoped_enum.hpp
    type_traits/is_unsigned.hpp
    type_traits/is_virtual_base_of.hpp
    type_traits/make_signed.hpp
    type_traits/make_unsigned.hpp
    type_traits/rank.hpp
    type_traits/remove_all_extents.hpp
    type_traits/remove_cv_ref.hpp
    type_traits/type_identity.hpp
    type_traits/integral_promotion.hpp
    type_traits/promote.hpp
   pending/detail/property.hpp
    type_traits/same_traits.hpp
 graph/graph_mutability_traits.hpp
 graph/graph_selectors.hpp
 property_map/property_map.hpp
  assert.hpp
   C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
  concept_archetype.hpp
  property_map/vector_property_map.hpp
   property_map/property_map.hpp
   smart_ptr/shared_ptr.hpp
    smart_ptr/detail/shared_count.hpp
     smart_ptr/bad_weak_ptr.hpp
     smart_ptr/detail/sp_counted_base.hpp
      smart_ptr/detail/sp_has_gcc_intrinsics.hpp
      smart_ptr/detail/sp_has_sync_intrinsics.hpp
      smart_ptr/detail/sp_counted_base_std_atomic.hpp
       smart_ptr/detail/sp_typeinfo_.hpp
     smart_ptr/detail/sp_counted_impl.hpp
     cstdint.hpp
    assert.hpp
     C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
    smart_ptr/detail/spinlock_pool.hpp
     smart_ptr/detail/spinlock.hpp
      smart_ptr/detail/spinlock_std_atomic.hpp
       smart_ptr/detail/yield_k.hpp
        smart_ptr/detail/sp_thread_pause.hpp
        smart_ptr/detail/sp_thread_sleep.hpp
         smart_ptr/detail/sp_win32_sleep.hpp
    smart_ptr/detail/operator_bool.hpp
    smart_ptr/detail/local_sp_deleter.hpp
     smart_ptr/detail/local_counted_base.hpp
 graph/detail/edge.hpp
  functional/hash.hpp
   container_hash/hash.hpp
 graph/properties.hpp
  assert.hpp
   C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
  graph/property_maps/constant_property_map.hpp
  graph/property_maps/null_property_map.hpp
 graph/named_graph.hpp
  functional/hash.hpp
   container_hash/hash.hpp
  optional.hpp
   optional/optional.hpp
    assert.hpp
     C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
    optional/bad_optional_access.hpp
    none.hpp
     none_t.hpp
    utility/compare_pointees.hpp
    utility/result_of.hpp
     utility/detail/result_of_variadic.hpp
    optional/optional_fwd.hpp
    optional/detail/optional_config.hpp
    optional/detail/optional_factory_support.hpp
    optional/detail/optional_aligned_storage.hpp
    optional/detail/optional_trivially_copyable_base.hpp
    optional/detail/optional_reference_spec.hpp
    optional/detail/optional_relops.hpp
    optional/detail/optional_swap.hpp
  pending/container_traits.hpp
   next_prior.hpp
    iterator/advance.hpp
   unordered_map.hpp
    unordered/unordered_map.hpp
     functional/hash.hpp
      container_hash/hash.hpp
     unordered/detail/map.hpp
      unordered/unordered_map_fwd.hpp
       functional/hash_fwd.hpp
   C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include\unordered_set
 graph/detail/adjacency_list.hpp
  range/irange.hpp
   assert.hpp
    C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
   range/iterator_range.hpp
    range/iterator_range_core.hpp
     assert.hpp
      C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
     range/functions.hpp
      range/begin.hpp
       range/config.hpp
       range/iterator.hpp
        range/range_fwd.hpp
        range/mutable_iterator.hpp
         range/detail/extract_optional_type.hpp
         range/detail/msvc_has_iterator_workaround.hpp
        range/const_iterator.hpp
      range/end.hpp
       range/detail/implementation_help.hpp
        range/detail/common.hpp
         range/detail/sfinae.hpp
      range/size.hpp
       range/size_type.hpp
        range/difference_type.hpp
         range/has_range_iterator.hpp
        range/concepts.hpp
         iterator/iterator_concepts.hpp
          concept/detail/concept_def.hpp
          concept/detail/concept_undef.hpp
         range/value_type.hpp
         range/detail/misc_concept.hpp
       range/detail/has_member_size.hpp
        cstdint.hpp
       assert.hpp
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\assert.h
       cstdint.hpp
      range/distance.hpp
       iterator/distance.hpp
      range/empty.hpp
      range/rbegin.hpp
       range/reverse_iterator.hpp
      range/rend.hpp
     range/algorithm/equal.hpp
     range/detail/safe_bool.hpp
    range/iterator_range_io.hpp
  graph/graph_concepts.hpp
   graph/numeric_values.hpp
   graph/buffer_concepts.hpp
    typeof/typeof.hpp
     typeof/message.hpp
     typeof/decltype.hpp
    concept/detail/concept_def.hpp
    concept/detail/concept_undef.hpp
   concept/detail/concept_def.hpp
   concept/detail/concept_undef.hpp
  graph/detail/adj_list_edge_iterator.hpp
  graph/adjacency_iterator.hpp
  assert.hpp

 ...about 600+ files

Thursday, April 21, 2022

Careful with mixing types in loop variables

Loop

 The other day I stumbled upon code which accidentally used a double as end condition:

#include <cstdint>
#include <cstdlib>

constexpr double g_dW = 10;

int main()
{
int res = 0;

for (int n = 0; n < g_dW; ++n)
{
res += std::rand(); // use rand just to have a valid loop
}

return res;
}

One would think that the optimizer Visual Studio 2019 16.13 is smart enough to use integer comparison but this is not the case. VS2019 issues a relative expensive integer to double conversion and comparison:

   
    for (int n = 0; n < g_dW; ++n)
00007FF7C9A01011  movsd       xmm6,mmword ptr [__real@4024000000000000 (07FF7C9A02240h)]  
00007FF7C9A01019  mov         edi,ebx  
00007FF7C9A0101B  nop         dword ptr [rax+rax]  
    {
        res += std::rand();
00007FF7C9A01020  call        qword ptr [__imp_rand (07FF7C9A02188h)]  
00007FF7C9A01026  inc         edi  
00007FF7C9A01028  add         ebx,eax  
00007FF7C9A0102A  movd        xmm0,edi  
00007FF7C9A0102E  cvtdq2pd    xmm0,xmm0  
00007FF7C9A01032  comisd      xmm6,xmm0  
00007FF7C9A01036  ja          main+20h (07FF7C9A01020h)  
    }
    
    return res;
 

The solution is simple to use an integer as block condition:

 constexpr int g_dW = 10;

Therefore watch out that VS2019 does not optimize things automatically.

 

Saturday, March 26, 2022

C++ library requests

Libraries

  Anyone who does serious application development would notice that standard C++ lacks extensive library support on the level of e.g. Java or .NET. For example many applications need the following:

  • GUI
  • graphics
  • diagrams and chart plots
  • object persistence
  • basic cryptography
  • graph algorithms
  • networking
  • database
  • XML; JSON
  • basic linear algebra

 There are 3th party libraries who fulfill this gap but it's always more difficult to get that up and running than if it would be stock present. It would therefore hard to recommend to use C++ for new application development.

Boost

  Boost fulfill some of these gaps. Their quality of libraries vary; some of them are even abandoned and not maintained anymore. Still I would like the following libraries to be incorporated in std as well:

  • call_traits
  • circular buffer
  • container
  • date-time (somewhat in C++20 in chrono)
  • graph
  • ios_state_saver
  • iterator_range (somewhat in C++20 in ranges)
  • serialization (but without the implicit by ref / by value assumptions)
  • signal / slot
  • string_algo
  • tribool
  • ublas 
  • unit

Sunday, February 20, 2022

Windows spotlight not working

Windows spotlight

 Windows spotlight is responsible for those nice aesthetic pictures on the logon screen. Somehow it was broken on my machine and I was deprived of this feature. Even when one selected the option (under 'settings', 'personalization' 'lock screen'), it would jump to normal picture mode. 

 Google didn't help at first (their search results become worse and worse over the years) until I got the tip from 'Software Test Tips': some necessary background apps where disabled. After enabling the app 'settings' (under 'settings'; 'privacy'; 'background apps') rebooting and waiting some time it works now.

Wednesday, February 16, 2022

Watch out for virtual function invocation

virtual function

 In some heavily invoked code the following construct was used:

interface ITable
{
   virtual std::string GetData() const = 0;
};

class Table : public ITable
{
public:
   virtual std::string GetData() const override
   {
      return std::string{"1"};
   }
};

class TableCt : public Table
{
public:
   long GetDataCt() const
   {
      const std::string strData = GetData();  // watch out
      return std::stol(strData);
   }
};

Client code invokes 'GetDataCt', e.g.:

void f()
{
    TableCt tbl;
    tbl.GetDataCt();  
}

in the real case TableCt wasn't the final class but GetData will not be overriden anywhere else. The code is functional correct but it takes a performance hit since base class function 'GetData' is invoked through vtable:

    TableCt tbl;
    tbl.GetDataCt();
00007FF653F710BC  lea         rdx,[rsp+30h]  
00007FF653F710C1  lea         rcx,[tbl]  
00007FF653F710C6  call        rax  

  On closer thought this is logical since the compiler must deal with 'GetData' being overwritten by a derived class. Therefore explicitly calling the base class function is needed to tell the compiler that it must use the base class:

class TableCt : public Table
{
   long GetDataCt() const
   {
      return std::stol(__super::GetData());
   }
};

 Using the keyword final could also do the trick too although it depends then on the cleverness of the compiler.

Sunday, February 13, 2022

Careful with profiling

Profiling

 The other day I was profiling a SSE optimized distance function and the more optimized form was 3 times as slow as the basic variant. The code was a bit like this (skipping the SSE variant):

template <typename T>
class Point
{
public:
   constexpr      Point     (T x, T y);

   constexpr T    GetX      () const;
   constexpr T    GetY      () const;

private:
   T              m_x;
   T              m_y;
};

template <typename T>
constexpr Point<T>::Point(T x, T y)
:  m_x(x)
,  m_y(y)
{
}

template <typename T>
constexpr T Point<T>::GetX() const
{
   return m_x;
}

template <typename T>
constexpr T Point<T>::GetY() const
{
   return m_y;
}

// explicit (DLL) instantiation
template class __declspec(dllexport) Point<double>;

double DistSqr(const Point<double>& rpt1, const Point<double>& rpt2)
{
    const double dx = rpt1.GetX() - rpt2.GetX();
    const double dy = rpt1.GetY() - rpt2.GetY();
   
    return (dx * dx) + (dy * dy);
}

It turned out that exported functions take a major performance hit; much larger than the two multiplications in 'DistSqr' function. The explicit exported template instantiation exports all functions; even constexpr and inline functions.The reason that calling exported function is slower:

  • invocation is a call instead of one simple memory read instruction
  • it suppresses other optimizations
  • just plain more instructions needed to transform data from 'Point' class to function

Removing the export the function was 3 times faster than without the exported attribute. The accessor functions 'GetX' are then inlined.

Lessons learned:

  • DLL and call invocations can harm performance
  • inspect the assembly

Note: accessor functions like 'GetX'  are prescribed by OOP but be aware of their potential performance cost.



Saturday, February 12, 2022

Mapping enums to enums

Mapping enums to enums

 There is sometimes the need to translate one enumerate value to another. For example let's have the case of enum 'A' and 'B':

enum A
{
   eA0 = 0,
   eA1,
   eA2,
};

enum B
{
   eB0 = 0,
   eB1,
   eB2,
};

A simple translation without asserting on non existing values could work as follow:

B Translate(A eA)
{
    B e = eB0;

    switch (eA)
    {
    case eA0: e = eB0;  break;
    case eA1: e = eB1;  break;
    case eA2: e = eB2;  break;
    }

    return e;
}

This is a straightforward one to one mapping. With Compiler explorer one can see that the Visual Studio 2019 compiler translates this in a bunch of compare and jump statements:

B Translate(A) PROC
        test    ecx, ecx
        je      SHORT $LN4@Translate
        sub     ecx, 1
        je      SHORT $LN5@Translate
        cmp     ecx, 1
        jne     SHORT $LN4@Translate
        mov     eax, 2
        ret     0
$LN5@Translate:
        mov     eax, 1
        ret     0
$LN4@Translate:
        xor     eax, eax
        ret     0
B Translate(A) ENDP   

In this case the value are exact the same so a more optimal solution would be that it just uses the value:

B Translate2(A eA)
{
    return static_cast<B>(eA);
}

In Compiler explorer it can be seen now that this is denser and faster:

B Translate2(EA) PROC
        mov     eax, ecx
        ret     0
EB Translate2(EA) ENDP  

It seems that Visual Studio doesn't recognize this optimization by itself.

More robust

Above cast can be dangerous if values do not correspond. One can trap for this and for accidental changes by adding some static_asserts

enum A
{
   eAbegin = 0,
   eA0     = ABegin,
   eA1,
   eA2,
   eAend
};

B Translate2(A eA)
{
    static_assert(eAbegin == eBbegin);
    static_assert(eAend == eBend);

    // or (verbose and requires updating after adding an enum):
    static_assert(eA0 == eB0);
    static_assert(eA1 == eB1);
    static_assert(eA2 == eB2);
    
    return static_cast<B>(eA);
}
 

Thursday, January 6, 2022

auto considered harmful

auto

  auto was once introduced to circumvent complex type deductions inside template functions. Lately however many C++ guru's think that one should use it as much as possible since the compiler always deduces the correct type. 

Counter arguments are as follow:

  1. the deduced type may be not what you want
  2. auto makes code harder to read.
  3.  introduces silent bug

unwanted deduction

 The canonical example of incorrect deduction is use in a for loop:

std::vector<std::string> vec;
for (auto a : vec)
{
}

  Here the variable 'a' will deduced to having type std::string so that a copy of the string is made on every loop. This is sub optimal and it would be better if the type was const std::string&. This is a known idiom and people are advised to use 'const auto&' but it illustrates that blindly using auto doesn't imply 'automatic' or 'best type' always.

 Another example is in the use of doubles vs floats. This can be important since there is a tradeoff between precision (double) and performance (float for GPU's).

readability

 This is a personal preference. Use of auto may reduce readability. Consider the following example:

auto a = GetPerson();

From these statement alone one cannot see if the return person is a (smart) pointer, a value or something else. This makes a difference; for example in case of using it in multi threaded environments. 

Consider the alternative where the type is made explicit:

std::shared_ptr<Person> ptr = GetPerson();

Its now clear that it was a pointer type and that the memory of returned object is taken care of. 

Consider another case:

Person* p = GetPerson();

  As a reader of the code it's clear now that one needs to question the ownership. Raw pointers are not recommended but sometimes they are unavoidable (e.g. external library like TensorRT).

 The readability can somewhat mitigated by use of Hungarian notation but in case of auto it's even more dangerous that the prefix will not reflect the (deduced) type.

bugs

 Previous topic already touches it. The use of auto may also coverup bugs. Recently I stumbled upon a case like this:

auto a = GetSerialize();

  It turned out that this the returned object was some kind of interface (again from TensorRT) which needed to be destroyed. This was completely unnoticed until it was written out:

IHostMemory* p = GetSerialize();

conclusion

  auto is a very good addition to the C++ language. It has its merit in original use case and with complex types where the actual type is not important. Iterators are a canonical example. As usual with any technique it can also hurt if you overuse it.

External links

C++ horrible aspects

C++ horrible aspects  Linus Torvalds described C++ as being a horrible language. While C++ has its dark corners I choose it any day ove...