ISO/ IEC JTC1/SC22/WG21 N1247

                                           Doc No. :  WG21 N1247
                                           Date:  17 April, 2000
                                           Project: Programming Language C++
                                           Reply to: Seiji Hayashida

Programmer-directed Optimizations (Performance Group)

Programmer-directed Optimizations

1. Current approaches

The most popular and easy way to improve performance is to use compiler's 
optimize options. Compilers support their options for optimization. And some 
compilers have optimizing option that gives high priority to code-size or 

Other approaches are roughly classified into two categories.

(1) ristrictions (subsetting)

   Turn off some functions which affect performance.

(2) extensions

   Add new directives or compiler options to generate more effective code. 

These might be implemented as '#pragma directive' or compiler options. 

2. ristrictions

In general, exception handling and RTTI both have some overhead. For users 
who do not use these functions, it is preferable to turn off them using 
compiler options. Programmers can get better performance by using these 
compilers options.

NOTE: When exceptions are disabled, the specifications of the standard 
libraries will be different.

Some compilers support options for subsetting. One of the subsets of the ISO 
C++ standard is Embedded C++(EC++). EC++ excludes the following features from 
the ISO C++ standard.

 - exceptions
 - templates
 - multiple inheritance
 - namespaces

EC++ also excludes some library features. The EC++ specification is summarized 
by the Embedded C++ Technical Committee (
Since some compiler venders already support EC++, it is possible to get 
better performance by writing programs within this specification.

3. extensions

- memory allocation
Sometimes, memory access time depends on the address of the memory. It is 
important to allocate data and text to appropriate addresses (memory sections), 
depending on a target CPU.
Some compilers have propriety function for specifying address of data. Many 
compilers use #pragma for this function, but each compiler has own syntax.

Although memory alignment is also important for memory access time, it may 
waste memory. So, a compiler (linker) must allocate each object with minimum 
loss, automatically. Or, programmer must specify the best address manually.

- Initialization of variables

In C++, constructors are always called to initialize variables of class type. 
If a global object is initialized to zero by a constructor, the constructor 
call may be purely overhead. Because global objects are supposed to be 
initialized to zero by default.

- Other directives for optimization

* Convert virtual calls to non-virtual calls

If a virtual function is not overridden by its derived classes, virtual 
function calls to it can be converted into non-virtual calls.
A directive like 'final' in Java which specifies such information can 
encourage compilers to generate better code. 

* restrict

A new keyword "restrict" has been added to the ISO C(ISO/IEC 9899:1999) for 
aiding optimization under the direction of users. If a C++ compiler supports 
"restrict", the compiler can produce better code.