Articles & Books

Thoughts on C++'s future and the pointer

I've written down some thoughts on the future of C++ and the pointer:

C++ future and the pointer

From the Article:

The last weeks after Meeting C++ 2013 I've been thinking a lot about C++, and also a little bit about pointers. While C++11 brought only little changes for pointers (nullptr f.e.), the semantics and usage of pointers in C++ has changed over the last years.

Quick Q: What is this "C++ memory model" you speak of? -- StackOverflow

Quick A: First read Stroustrup's FAQ answer, before reading the SO answers which are also illuminating. For a deep answer and to really grok this topic, check out Sutter's slides from his three-hour deep-dive talk.

A 2011-vintage "new classic" from SO:

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

... So, what I basically want to know is, C++ programmers used to develop multi-threaded applications even before, so how does it matter if its POSIX threads, or Windows threads, or C++11 threads? What are the benefits? I want to understand the low-level details.

I also get this feeling that the C++11 memory model is somehow related to C++11 multi-threading support, as I often see these two together. If it is, how exactly? Why should they be related?

As I don't know how internals of multi-threading works, and what memory model means in general, please help me understand these concepts. grin

Data Locality -- Bob Nystrom

data-locality.PNGA nice refresher on data locality, and coding techniques to improve it for substantial performance gains.

Data Locality

by Bob Nystrom

From the article:

Sure, we can process data faster than ever, but we can’t get that data faster. ...

When I started working on this chapter, I spent some time putting together little game-like programs that would trigger best case and worst case cache usage. I wanted benchmarks that would thrash the cache so I could see first-hand how much bloodshed it causes.

When I got some stuff working, I was surprised. I knew it was a big deal, but there’s nothing quite like seeing it with your own eyes. I wrote two programs that did the exact same computation. The only difference was how many cache misses they caused. The slow one was fifty times slower than the other.

Nugget: C++1y Automatic Type Deduction -- Tony DaSilva

A short nugget by Tony DaSilva:

C++1y Automatic Type Deduction

by Tony DaSilva

From the article:

In addition to this convenient usage, employing auto in conjunction with the new (and initially weird) function-trailing-return-type syntax is useful for defining function templates that manipulate multiple parameterized types (see the third entry in the list of function definitions below).

Type Erasure, Part 3 -- Andrzej Krzemieński

In part 3, Andrzej turns to some practical use cases for type erasure.

Type Erasure, Part 3

by Andrzej Krzemieński

From the article:

Who needs type erasure?

... Here is what I learned from my experience. There are a couple of trade-offs to be made when using or not type erasure: run-time efficiency vs compilation time, run-time efficiency vs binary size. The choice is not obvious. Even in programs that need to be fast, not every part of the program needs to be fast. Some portions, like the interaction with the user, can be slow, and for these parts you can apply different trade-offs.

static_assert: Better template error messages -- Michael Price

This article could be subtitled, and ends with, "Why we need Concepts."

Here's a quick take on how far you can get with just <type_traits> and static_assert, and why you might want to do that.

static_assert: Better template error messages

by Michael Price

From the article:

Templates are one of the most powerful features of C++, and provide the language with a serious advantage over many of its brethren.  But templates have serious drawbacks as well, one of which is the incredibly verbose and dense error messages that are provided should you fail to provide the right kind of parameter to a templated entity.

Type Erasure, Part 2 -- Andrzej Krzemieński

In part 1, Andrzej explained what "type erasure" is all about. But how do you create custom type-erased interface?

Type Erasure, Part 2

by Andrzej Krzemieński

From the article:

While there are many ways to erase a type, I will use name type erasure to denote the usage of std::function-like value-semantic interface. This convention appears to be widely accepted in C++ community.

... Using std::function is easy, but this is because someöne has made an effort to implement it for us. So let’s try to see how much effort it is to write our own type-erased interface. We will be using countdown counters: something that (1) can be decremented and (2) tested if the count reached zero.

The Cost of Dynamic (Virtual Calls) vs. Static (CRTP) Dispatch in C++ -- Eli Bendersky

eli-bendersky.PNGA nice dive into performance costs on at least one compiler, and on the difficulties of doing meaningful performance measurements on modern hardware. Be sure to read the short comment thread too.

The Cost of Dynamic (Virtual Calls) vs. Static (CRTP) Dispatch in C++

by Eli Bendersky

From the article:

A couple of years ago I wrote an article about the Curiously Recurring Template Pattern in C++, focusing on the motivation behind it and how to implement it.

That article mentioned runtime performance as the main reason for employing CRTP instead of the more traditional runtime polymorphism (dispatch via virtual functions). While some rationale for the cost of virtual calls was given, I didn’t go too deep into it. Today I want to fix that by carefully analyzing the performance of virtual calls as opposed to the static calls made possible by CRTP.

Mandatory precaution about benchmarks

Benchmarking in 2013 is really hard. ...

Using STL Vectors, and Efficient Vectors of Vectors -- Thomas Young

thomas-young.jpegThese are also the first two articles in a new blog by the creator of the PathEngine SDK.

Note: One or two of the points can be controversial, but the content is interesting and informative especially as an experience report.

Using STL Vectors

Efficient Vectors of Vectors

by Thomas Young

From the articles:

The stuff we do with vectors can be broadly categorised into two main use cases:

  • the construction of (potentially complex) preprocess objects, and
  • run-time buffers for queries, where buffer size requirements are fundamentally dynamic in nature

Preprocess objects include things like the pathfinding visibility graph. These objects can take time to build, but this is something that can be done by the game content pipeline, with preprocess data saved and loaded back from persistence by the game runtime.