1BRC in C++11 #135
kajott
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A friend dropped the link to 1BRC in our Mattermost chat, and I couldn't resist writing an implementation for it:
https://gist.github.com/kajott/8a7c3f56a9a035fdfe3b2c79e17c8eed
It's plain C++11 with moderate use of the standard library. No external libraries.
It uses more or less the same techniques as Danny van Kooten's C implementation:
read()
ing chunks of the input file, it'smmap()
ed in its entirety.strtod()
/atof()
are very slow! Since the values are only using (at most) one decimal digit anyway, a fast custom parser for those can be used, and all math can be integer.std::unordered_map
is also slow, so a custom hash map implementation is used. It's using a simple rotate-and-XOR or rotate-and-add hash function, and can use a configurable number of steps of quadratic probing before falling back to a linked list for each bucket.malloc()
, each thread gets its own heap for run-time allocations (such as the key strings of the hash map, and the extra objects in the linked list) with a simple bump allocator.Performance should be in the same ballpark as most other implementations; I get 6.7 seconds on an i7-1185 notebook CPU (4 cores + HT, running at ~4 GHz) with 8 threads.
Beta Was this translation helpful? Give feedback.
All reactions