News for hackers. Stuff that matters.
Re:C++: Associative map containers with compile-time lookup by hogliux on Tuesday October 23, 2018 @06:20AM (#18282172)
Yes with the optional run-time lookup there is an extra two machine instructions (a cmp and jne) needed to check if this is the first time accessing the value. It is exactly as fast as a global variable if you don't need the optional run-time lookup.

As stated in the talk, the main use case for a map like this is to cache objects which are expensive to load/compute (again something like `getFont` comes to mind). In these use-cases you would likely also need a check like this anyway: a global pointer object which you need to check if it's a nullptr or not before using it.

Re:C++: Associative map containers with compile-time lookup by quotemstr on Tuesday October 23, 2018 @06:35AM (#18282222)
Not exactly as fast. Without -Bsymbolic, a global variabile with global symbol visibility gets its own GOT entry, meaning access to your global goes through an indirection table that supports ELF symbol interposition. (Which, IMHO, was a bad idea, but you can't unbreak an egg.) With a data structure like this (or with a conventional "struct globals"), there's one symbol at the ELF level to look up instead of one per variable, so your code might end up doing fewer GOT lookups.

The last paragraph was just arcana though. You should be passing -fvisibility=hidden and explicitly exporting from your DSO only the symbols you might legitimately expect some other DSO to use, obviating the issue.

Re:C++: Associative map containers with compile-time lookup by hogliux on Tuesday October 23, 2018 @06:41AM (#18282247)
Thanks, I'll have a look at this!
Re:C++: Associative map containers with compile-time lookup by jstimpfle on Tuesday October 23, 2018 @08:10AM (#18282647)
Look at the approach from my other comment. Requires only 1 symbol.
Re:C++: Associative map containers with compile-time lookup by hogliux on Tuesday October 23, 2018 @08:16AM (#18282671)
Both your approach and the `semi::static_map` will generate the same type of load instruction. In both cases, the compiler knows the offset of the values in memory at compile-time. The number of symbols is not really important here.