This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

implementation & optimization of std::function with and without allocator


Hello All,

Consider the below toy example (I'm using Boehm GC 7.2d as provided on
Debian/Sid/x86-64):

   // filetestclogc.cc
   #include <algorithm>
   #include <memory>
   #include <functional>
   #include <cstdio>
   #include <gc/gc_allocator.h>

   gc_allocator<int> mygcalloc;

   extern "C" void apply_to_each(int lo, int hi, 
                                 const std::function<void(int)>&f)
   {
      for (int i=lo; i<hi; i++) f(i);
   }

   extern "C" void test_gc_closure(int mx)
   {
     std::function<void(int)> f (
   #if 0 /* we should accept this but we don't yet */
                              std::allocator_arg, mygcalloc,
   #endif
                              [&](int i) {printf("i=%d\n", i);});
     apply_to_each(0, mx, f);
   }  


First, my understanding of the C++11 standard thru
http://en.cppreference.com/w/cpp/utility/functional/function/function 
(which I know is imperfect, but I find it much more readable than
n3337.pdf draft of C++11 ....)
is that in principle we should compile this code with the #if 0 replaced
by #if 1 (but we don't yet, our <functional> header says "TODO: needs
allocator_arg_t"). Perhaps the type of mygcalloc is wrong...
(I don't understand at all the issues of accepting allocators for
std::function)

Then, I am surprised (and a bit disappointed) that (using GCC 4.8.2 from
Debian) with

   g++-4.8  -Wall -std=c++11 -O3 -S \
           -fdump-tree-phiopt -fverbose-asm testclogc.cc

we are not inlining all the lambda stuff. I was expecting we'll do the
inlining, since similar experiments with std::array etc... are very well
optimized, like

    #include <cstdio>
    #include <array>
    #include <functional>
    #include <algorithm>

    int sum (const std::array<int,10>&a)
    {
      int s=0;
      std::for_each(a.begin(), a.end(), 
                    [&](int i) {printf("i=%d\n", i); s += i;});
      return s;
    }   



I am not at all expert on C++ optimization, so probably I am very naive.
If someone had time to explain what passes are optimizing this sum
function with g++ -std=c++11 -O3 I would be very happy (the handling of
lambda-s is really mysterious to me)... 


Cheers.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]