Subtle Waves Template

Download Report

Transcript Subtle Waves Template

Compiler Support for Profiling
C++ Template Metaprograms
József Mihalicza, Norbert Pataki, Zoltán Porkoláb
Eötvös Loránd University
Faculty of Informatics
Dept. Of Programming Languages and Compilers
Outline

Templates in C++

Template metaprograms

Efficiency problems with C++ templates

Earlier efforts / Related works

Our solution

Examples
SPLST’11
C++ templates

Parametric polymorphism

Fundamental tools for generic programming

Unconstrained

Implemented by instantiation

Typical solution: multiply header files

Examples: standard library, inlc. STL
SPLST’11
Template metaprograms

Unruh 1994: prime numbers

Has been proved to be Turing-complete

Functional programming paradigm

Recursion + pattern matching

Referential transparency

No assignment

(Almost) no i/o
SPLST’11
Template metaprograms 2
template <int N>
struct Factorial
{
enum { value = Factorial<N-1>::value * N };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
int main()
{
int fact5 = Factorial<5>::value;
}
SPLST’11
Template metaprogram usage

Expression templates (blitz++, …)

Parser generators (boost::spirit, …)

Static interface checking (boost::concept, …)

Configuration management (boost::math, …)

Active libraries (…)

DSL integration (Porkolab-Sinkovics,GPCE2010)
SPLST’11
Efficiency issues

Recursive header inclusions

Data structures (recursive templates)

Imitating data manipulations

Complex, unclear syntax

Compilers was not optimized for TMPs

Hard to predict compilation time and memory
usage
SPLST’11
Earlier efforts
Veldhuizen (1996- )


Gurtovoy – Abrahams (2004)


Measuring full programs to identify idioms
Porkolab-Mihalicza


Debugging (GPCE 2006) + Profiling (Splst 2007)
Steven Watanabe (2008)



Ad-hoc measurement, non-standard tools
Boost library extension
Compilers was not optimized for TMPs
SPLST’11
Measuring compilation units
Advantages


Easy, platform and compiler independent

Get the big picture, understand behaviour

Statistically correct
Drawbacks


Artificial problems

Not revealing details in complex programs

Overheads (code generation, preprocessor)
SPLST’11
Preprocessor overhead
SPLST’11
Instrumentation framework
Code instrumentation
Code instrumentation 2
Warning generation
Overhead
Deviation of
overhead
Modified compiler
Summary






We have to measure template metaprograms
(and perhaps all large projects with many templates)
Measuring compilation units are not feasible in real-world
industrial projects
Preprocessing step has a significant overhead
Instrumentation has a linear distortion
Modification of compiler can eliminate warning overhead
Template metaprogram profiling is still learned
Thank you for your attention!
Questions?
József Mihalicza, Norbert Pataki, Zoltán Porkoláb
[email protected], [email protected], [email protected]