Increase tuple performance

According to boost :: tuple documentation , accessing a single tuple element has the same performance as accessing a member variable. For example, given the following declaration:

tuple<A, B, C> t1(A(), B(), C()); struct T { A a; B b; C c; } T t2; 

These two operators should have equal (or with a slight difference) performance:

 t1.get<2>(); t2.c; 

I looked at the sources of boost :: tuple and, if I understood them correctly (I'm not sure what I did), the get<N> function actually performs this action:

 C get<2>(tuple<A, B, C>& t) { return t.tail.tail.head; //Generally: return t.tail. <<N times>> .head; } 

This is more like searching in a linked list than direct access, and as I understand it, has the complexity O (N) instead of O (1) expected from accessing a member. From my past experience with amplification, I assume that I was wrong; but what is my mistake? How does get work?

+6
c ++ boost templates boost-tuples
source share
2 answers

You are right about list-like performance. However, it can be resolved at compile time and therefore boils down to O (1) at runtime. (Given a reasonably good optimizing compiler.)

+6
source share

Remember that in C ++, the dot operator is not a pointer to a pointer; it is a calculation of forward bias. The general answer: yes, i1.i2.i3.in for all n is a constant-time operation that can be computed at compile time.

If you want to know a little about the internal components of the compiler for this, without going deeply, look at LLVM getelementptr http://llvm.org/docs/LangRef.html#i_getelementptr This is exactly like a C ++ compiler such as CLANG will target LLVM when compiling a structure reference.

+3
source share

All Articles