Re: Aliasing performance problem.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Nov 1, 2009 at 20:17, tom fogal <tfogal@xxxxxxxxxxxxxx> wrote:
> Ł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?

Nat is just one int.

>
>>  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.
>:)

Adding -fno-strict-aliasing also reduces the loss of performance from
25% to 12%.


> 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.

None helps.

>
> If that doesn't work, I would say compile it both ways and look at the
> generated asm.

That's hard.
I use this class in plenty of places.
Hot spot of the program is many 100's of lines of C++.
Moreover one change in line somewhere in code changes a whole assembly
beyond analyzing.

>
>> 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.

I'm not sure you are correct here.
Maybe that's true if you want to use functions like resize?

>
> HTH,
>
> -tom
>


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux