The type-traits library has two main goals: correctness and optimization. Today, I write about optimization.
This post is the last post in my miniseries about the type-traits library. I have already written the following posts:
- The Type-Traits Library: Type Checks
- The Type-Traits Library: Type Comparisons
- The Type-Traits Library:
- The Type-Traits Library: Correctness
Before I write about optimization in C++, I want to tell a short anecdote. I often have the following conversation with my students in my classes:
- Me: Why do we have the feature ABC in C++?
- Student: I don’t know.
- Me: If you don’t have an answer, say performance. This always works in C++.
So, let me write about the type-traits library from the optimization perspective.
The idea is quite straightforward and used in the Standard Template Library (STL). If the elements of a range are simple enough, the algorithms of the STL like
std::copy, std::fill, or
std::equal are directly applied to memory. Instead of using std::copy to copy each element one by one, all is done in one big step. Internally, C functions like
memmove are used. The small difference between
memmove is that
memmove can deal with overlapping memory areas.
The implementations of the algorithm
std::copy, std::fill, or
std::equal use a simple strategy.
std::copy is like a wrapper. This wrapper checks if the elements are simple enough. If so, the wrapper will delegate the work to the optimized copy function. If not, the conservative copy algorithm is used. This conservative one copies each element after the other. The functions of the type-traits library are heavily used to make the right decision.
The graphic shows the general strategy:
That was the theory, but here is the practice. Which strategy is used by
std::fill assigns each element in the range a value. The listing shows a GCC-inspired implementation of std::fill.
Back to the code example. If the expression boolType() in line (1) is true, the optimized version of my::fill_impl in line 2 is used. This variant fills the memory of 100 million entries with the value 1. sizeof(char) is 1.
What about the performance of the program? I compiled the program without optimization to measure the non-optimized performance.
The optimized version in line (2) is about ten times faster. Interestingly, when I enable full optimization, both variants are equally fast because the compiler generates the same code for both variants. Also, the generic version (line (3)) uses
fillGCC.cpp with maximum optimization on Compiler Explorer.
I presented an old GCC implementation
std::fill, because the newer ones are not so easy to read. Here are the essential parts of the GCC 6 implementation.
The GCC 6 implementation uses SFINAE. The full specialization of the function template
__builtin_memset. The crucial part in this implementation is line (1):
__gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type. Let me rewrite this expression in a human-readable way and use the official names.
The expression checks first if the template parameter TP is a byte:
std::is_byte<T>::value. If this expression evaluates to
true thanks to
std::enable_if from the type-traits library SFINAE kicks in. SFINAE stands for Substitution Failure Is Not An Error and applies during overload resolution of a function template. If substituting the template parameter fails, the specialization is discarded from the overload set, but this failure causes no compiler error. This means in this concrete case: When the condition
std::is_byte<T>::value returns false, this full specialization is discarded, and another version of
__fill_a is used.
First, I take a Christmas break of two weeks. My next post will be published on 10th January 2022. I will write about
constexpr functions because they have much in common with templates and become more potent with C++20.
Second, for a long time, I would like to improve my professional teaching of C++. Therefore, I plan to start a mentoring program for C++. Soon I will publish more details about my idea.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,