Skip to content

v5.4.0

Latest

Choose a tag to compare

@martinus martinus released this 03 Dec 09:47

A new value container that grows with realloc, a clear() that costs what the map holds, and a C++20 module that exports the whole API. Same hash values and same iteration order as 5.3.3, and index_format_id is unchanged, so an index saved with 5.3.x loads in 5.4.0.

realloc_map and realloc_set (#391)

The new optional header realloc_vector.h has a value container that grows its block with realloc instead of allocating a new block and moving everything over. With glibc a large block grows by mremap, so the pages move instead of being faulted in again. realloc_map and realloc_set put it in the container slot. It is for maps that are built without reserve().

Build + destroy against the same map on std::vector, below 1 is faster (Ryzen 9 7950X, clang, glibc's default malloc settings):

entries 16 byte pair 72 byte pair
250000 0.97 0.41
1M 0.92 0.54
4M 0.82 0.61
16M 0.85 0.65

Peak memory goes down to 0.76-0.83, because the old and the new block never exist at the same time. Lookups and iteration run the same code as on a std::vector. Only a trivially relocatable type is grown with realloc (a std::pair of such types counts, and you can specialize the trait for your own type); everything else grows like a std::vector. It calls malloc itself and takes no allocator, and I have only measured it with glibc on Linux. See realloc_map and realloc_set.

clear(clear_mode::sparse) (#381)

clear() resets every group of the index, so it costs the largest size the map ever had: 834 us for a map that once held 4M entries and now holds 16. clear(clear_mode::sparse) empties each entry's slot instead when the map holds fewer than one entry per 16 groups, which is 5-10000x faster for a few entries in a large table. Just past that threshold, with 1 KB keys, it can be up to 2.2x slower. It hashes every stored key, so don't call it after moving keys out. Plain clear() is unchanged and never hashes.

Smaller changes

  • The C++20 module exports map_view, set_view, segmented_vector, default_segment_size_bytes, clear_mode, trust and verify_level. Before, a module user could not load a map, use a view or call clear(clear_mode::sparse). CI now runs a module test that uses all of them (#407).
  • segmented_map and segmented_set are now a plain map and set with a segmented_vector in the container slot, the same way realloc_map works. The code that runs is the same, only the type's spelled-out name changes. Code that only uses the aliases does not notice (#399).
  • segmented_vector's copy constructors leaked the segments they had already filled when an element's copy threw. Fixed (#401).
  • The insert's append_value is no longer flatten. With gcc it inlined the value's constructor, the container's growth and the allocator into every insert, so some maps took minutes to compile: segmented_map on boost interprocess's allocator went from 66.4 s to 1.8 s for one try_emplace, map<int, std::regex> from 21.2 s to 2.7 s. Unfortunately it costs gcc a bit at run time: in the benchmark suite, builds run 4.8-6.4% more instructions, churn 2.9-3.5% and insert-erase 1.8-3.4%, while find and iterate are unchanged. In the caller loops it goes the other way, five of them run fewer instructions. Clang is identical everywhere (#395).
  • 32 bit MinGW targeting Windows XP compiles again, and CI now builds and tests 32 bit MinGW (#393).
  • Older clang and libc++ compile again (#394, thanks @AmyrAhmady!). CI now also builds with gcc 7 and clang 5.
  • The README is rewritten, and the other docs were checked against the code. design.md has a new figure that follows one lookup through the index (#406).

All numbers and scripts are in notes/index-design.md. The inline namespace is v5_4_0.