I forgot to CC the list. Sorry. ------- Forwarded Message From: tom fogal <tfogal@xxxxxxxxxxxxxx> To: Å?ukasz Lew <lukasz.lew@xxxxxxxxx> Subject: Re: Aliasing performance problem. In-Reply-To: Your message of "Sun, 01 Nov 2009 19:35:37 +0100." <c55009e70911011035o1f206eadq36ade953b48543a@xxxxxxxxxxxxxx> References: <c55009e70911011035o1f206eadq36ade953b48543a@xxxxxxxxxxxxxx> Date: Sun, 01 Nov 2009 12:17:08 -0700 Å?ukasz Lew <lukasz.lew@xxxxxxxxx> writes: > I wanted to drop the requirement for Elt to have a default constructor: > > template <typename Nat, typename Elt> > class NatMap { > public: > Elt& operator[] (Nat nat) { > return ((Elt*)tab) [nat.GetRaw()]; > } use reinterpret_cast here. How big is a `Nat'? Could you instead pass it by reference? > private: > char tab [Nat::kBound * sizeof(Elt)]; > }; > > I use g++-4.3 and this code works 25% slower in my application than > the previous one. Unfortunately the slowdown does not manifest in a > synthetic benchmark. I guess it is something about compiler > optimizations, aliasing, aligning, or similar stuff. > > Just now I tried new g++-4.4 and it gave me a following warning for > the latter code: > dereferencing pointer '<anonymous>' does break strict-aliasing rules > > What should I do to get my performance back? (while not needing the > default constructor) first, compile with -fno-strict-aliasing to get your correctness back. Then -- does it help if you force inlining of the method? You might also consider playing with the attributes on the function: http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Function-Attributes.html e.g. `hot', `pure' or `const' if applicable, `nothrow' though I bet it won't help and I'm not sure if that's equivalent to `throw()', the latter generally being a bad idea. If that doesn't work, I would say compile it both ways and look at the generated asm. > If this is lack optimizations because of aliasing info, this has to > be doable somehow as STL have the same problem, doesn'it ? The STL `Container' concept requires the contained type to be `DefaultConstructible', which has the semantics you would think. I'm going off the SGI STL docs, so that knowledge is a bit dated, though I would imagine still generally accurate. HTH, - -tom ------- End of Forwarded Message