CppCon 2018 has ended
Back To Schedule
Tuesday, September 25 • 15:15 - 15:45
A Semi Compile/Run-time Map with (Nearly) Zero Overhead Lookup

Sign up or log in to save this to your schedule, view media, leave feedback and see who's attending!

An associative map container is one of the most useful data-structures in any modern program. Given a key, it can look-up the corresponding value incurring some runtime overhead. Often, however, this overhead is not strictly necessary as the key originated (either directly or indirectly) from a value which was known to the compiler at compile-time - such as a literal. In this talk, I will present a semi compile-/run-time map which can overcome this overhead by using C++’s constexpr features.

Yet another constexpr hash map talk, I hear you say? This one is different! Most constexpr maps presented at previous conferences are either constexpr-only, i.e. the values cannot be changed at runtime, or, require that all possible keys of the map are defined in one place making it unsuitable for library code. In contrast, the map presented in this talk, only calculates the storage of the key’s associated value at compile-time, but loading/storing the value and adding key/value pairs still occurs at run-time.

It will be shown how such a map can be used to implement a super efficient cache. In its most simplest form, looking up the storage of a primitive type for a given compile-time key has the same overhead as accessing a global variable. It will be shown that the underlying principle is surprisingly easy (3 lines of code) and the difficulty actually lies in associating any compile-time identifier to a unique C++ type - especially string literals. I will show how this can be achieved using constexpr if and constexpr lambda expressions - both C++17 features.

avatar for Fabian Renn-Giles

Fabian Renn-Giles

Software engineer, Fielding DSP GmbH
Fabian is a freelance C++ programmer, entrepreneur and consultant in the audio software industry. Before this, he was staff engineer at ROLI Ltd. and the lead maintainer/developer of the JUCE C++ framework (www.juce.com) - an audio framework used by thousands of commercial audio software... Read More →

Tuesday September 25, 2018 15:15 - 15:45 PDT
Keystone (404)
  • Data Structures and Algorithms