This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Aliasing performance problem.
- From: Åukasz Lew <lukasz dot lew at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sun, 1 Nov 2009 21:14:05 +0100
- Subject: Re: Aliasing performance problem.
- References: <c55009e70911011035o1f206eadq36ade953b48543a@mail.gmail.com> <auto-000020973049@sci.utah.edu> <c55009e70911011153w2d7806d6p700eb65ebe9b3b11@mail.gmail.com>
On Sun, Nov 1, 2009 at 20:17, tom fogal <tfogal@alumni.unh.edu> wrote:
> Åukasz Lew <lukasz.lew@gmail.com> 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
>