Meeting 2 August 2018

Follow-up: Four Habit-Forming Tips to Faster C++

Post by KDAB

“Cache member-variables and reference-parameters”

The original version in the article doesn’t even work without aliasing:

1template<typename T>
2class complex {
3    complex& operator*=(const complex<T>& a) {
4        real = real * a.real - imag * a.imag;
5        // BUG: real is changed, then used in subsequent calculation
6        imag = real * a.imag + imag * a.real;
7        return *this;
8    }
9};

The recommended version from the article:

 1template<typename T>
 2class complex {
 3    complex& operator*=(const complex<T>& a) {
 4        T a_real = a.real, a_imag = a.imag;
 5        T t_real = real, t_imag = imag;
 6        real = t_real * a_real - t_imag * a_imag;
 7        imag = t_real * a_imag + t_imag * a_real;
 8        return *this;
 9    }
10};

Proposed “economy” version:

 1template<typename T>
 2class complex {
 3    complex& operator*=(const complex<T>& a) {
 4        const auto real_this{real}; // modified by our code
 5        const auto real_a{a.real}; // may be modified if aliased
 6        real = real * a.real - imag * a.imag;
 7        imag = real_this * a.imag + imag * real_a;
 8        return *this;
 9    }
10};

Proposed “easy” version:

1template<typename T>
2class complex {
3    complex& operator*=(complex<T> a) {
4        const auto self{*this};
5        real = self.real * a.real - self.imag * a.imag;
6        imag = self.real * a.imag + self.imag * a.real;
7        return *this;
8    }
9};

C++ Quiz by Shafik Yaghmour via Twitter

C:

1malloc(0)

C++:

1new int[0]

What’s the behavior of these expressions?

  1. Both return NULL/nullptr
  2. Both return an address that’s invalid to dereference
  3. Behavior can vary
  4. Undefined behavior

In C99 and C11 malloc(0) is allowed to return either NULL or pointer that is not valid to dereference:

In C++ we get back an array with zero elements:

Allocating 0 bytes

|

Metashell GUI

  • Code (MIT)
  • Metashell “The goal of this project is to provide an interactive template metaprogramming shell.” (GPL-3.0)

Ericsson CodeCompass

CodeCompass is a software comprehension tool for large scale software written in C/C++ and Java

Expect the Expected, by Andrei Alexandrescu

CppCast: Parallel Ranges with Chris DiBella

Pacific++ 2017: Toby Allsopp - An Introduction to the Proposed Coroutine Support for C++

Qt and the Coroutines TS

Exploiting Coroutines to Attack the “Killer Nanoseconds”

In this work, we compare and contrast the state-of-the-art approaches to reduce CPU stalls due to cache misses for pointer-intensive data structures. We present an in-depth experimental evaluation and a detailed analysis using four popular data structures: hash table, binary search, Masstree, and Bw-tree. Our focus is on understanding the practicality of using coroutines to improve throughput of such data structures.

Herb Sutter: How to Adopt Modern C++17 into Your C++ Code

Unity (“jumbo”) builds

AKA: amalgamated, or Single Compilation Unit (SCU) builds.

const auto* versus const auto for Pointer Types

Article

Twitter