What's the point of C++ mutable in your C++ compiler?

"Jeff Keasler" <jeff@svn.net>
18 Oct 2003 15:47:35 -0400

          From comp.compilers

Related articles
What's the point of C++ mutable in your C++ compiler? jeff@svn.net (Jeff Keasler) (2003-10-18)
| List of all articles for this month |

From: "Jeff Keasler" <jeff@svn.net>
Newsgroups: comp.compilers
Date: 18 Oct 2003 15:47:35 -0400
Organization: Posted via Supernews, http://www.supernews.com
Keywords: C++, optimize, question
Posted-Date: 18 Oct 2003 15:47:35 EDT


As the code fragments below indicate, the fact that const cannot be
optimized away is a major performance problem in C++. The first and
second example below should compile to the same highly optimized code,
but they don't because the second example has a const member "length"
that is not be treated as const.

If I had wanted "length" to be treated as mutable, I would have
declared it as mutable. But I don't want it to be treated as mutable.
I want it to be treated as const so the compiler can do a good job of

SO my question is, since mutable and const are available in C++, why
not add a compiler switch that treats them correctly, and leaves
behind the old C baggage of being able to cast away const. DO you
realize how much faster 90% of all C++ scientific software would run
if you did this? It would be mind boggling!!!

-Jeff Keasler
:Lawrence Livermore National Laboratory


Example #1

void junk(int * const data, int a, int b, int length)
              int i, j ;

              for (i=0; i<a; ++i)
                    for (j=0; j<b; ++j)
                          data[length*i + j] = 0 ;

icc -Kc++ -O2 -S -c rel3.c (inner loop)

                          movl $0, (%ecx,%ebx,4)
                          addl $1, %ebx
                          cmpl %esi, %ebx
                          jl ..B1.9 # Prob 80%


Example #2

class relation {
                int const length ;
                int const size ;
                int * const data ;

                relation(int len, int sz) : length(len), size(sz), data(new
int[len*sz]) { }
                ~relation() { delete [] data ; }

                inline int &operator() (int srcIdx, int destIdx) const
                        { return data[length*srcIdx + destIdx] ; }
} ;

void junk(class relation &foo, int a, int b)
                int i, j ;

                /*ie class relation foo(a, b) ; */

                for (i=0; i<a; ++i)
                      for (j=0; j<b; ++j)
                            foo(i, j) = 0 ;

icc -Kc++ -O2 -S -c rel2.c (inner loop)
(BTW, I tried restrict, which of course did not help.
  That's yet another story though *sigh*...)

                          movl -12(%ebp), %eax
                          movl (%edx), %esi
                          imull %eax, %esi
                          movl %ecx, -4(%ebp)
                          xorl %edi, %edi
                          addl %ecx, %esi
                          movl 8(%edx), %ecx
                          movl %edi, (%ecx,%esi,4)
                          movl (%edx), %esi
                          imull %eax, %esi
                          movl 8(%edx), %ecx
                          addl %ebx, %esi
                          movl %edi, (%ecx,%esi,4)
                          movl (%edx), %esi
                          imull %eax, %esi
                          movl 8(%edx), %ecx
                          addl $4, %ebx
                          addl -32(%ebp), %esi
                          addl $4, -32(%ebp)
                          movl %edi, (%ecx,%esi,4)
                          movl -8(%ebp), %esi
                          movl (%edx), %ecx
                          imull %eax, %ecx
                          movl 8(%edx), %eax
                          addl %esi, %ecx
                          movl %edi, (%eax,%ecx,4)
                          movl -4(%ebp), %ecx
                          movl -16(%ebp), %eax
                          addl $4, %esi
                          addl $4, %ecx
                          movl %esi, -8(%ebp)
                          cmpl %eax, %ecx
                          jle ..B1.6


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.