Re: Aliasing performance problem.

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

 



?ukasz Lew wrote:

I have a container similar to this one.

template <typename Nat, typename Elt>
class NatMap {
 public:
  Elt& operator[] (Nat nat) {
    return tab [nat.GetRaw()];
  }
 private:
  Elt tab [Nat::kBound];
};

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()];
  }
 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)

The first thing you should do is fix the aliasing bug, and then repeat the
measurement.  It's hard for gcc to do a decent job optimizing code that
is incorrect.

Andrew.

[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