This is the mail archive of the gcc-help@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]

c++0x tuple vs. pair


I vaguely thought tuples were supposed to be "just as good as pair, only
better!", but gcc-4.6 seems to miss some optimization opportunities with
tuples that it doesn't with pair.

E.g. the following:

---- start ----
   #include <tuple>
   #include <utility>

   struct A
   {
     virtual void f () const;
   };

   void tuple_test (const std::tuple<A> &t)
   {
     std::get<0>(t).f ();
   }

   void pair_test (const std::pair<A,A> &t)
   {
     t.first.f ();
   }
---- end ----

compiled with:

  g++-snapshot -std=c++0x -O2 -march=native -S x1.cc

results in the following assembly:

   tuple_test(std::tuple<A> const&):
           movq	(%rdi), %rax
           movq	(%rax), %rax
           jmp	*%rax

   pair_test(std::pair<A, A> const&):
           jmp	A::f() const

           .ident	"GCC: (Debian 20101114-1) 4.6.0 20101114 (experimental) [trunk revision 166728]"


Does this seem like something that _should_ work, or is there a
fundamental difference between tuples and pairs that makes the former
harder to optimize for?

It does do better in some other cases involving tuples; e.g.:

   void extern_tuple_test ()
   {
     extern const std::tuple<A> t;
     std::get<0>(t).f ();
   }

results in:

   extern_tuple_test():
           movl	$t, %edi
           jmp	A::f() const

Thanks,

-Miles

-- 
Love is a snowmobile racing across the tundra.  Suddenly it flips over,
pinning you underneath.  At night the ice weasels come.  --Nietzsche


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