abseil-cpp icon indicating copy to clipboard operation
abseil-cpp copied to clipboard

Test app aborts when std::string_view is used as the key for absl::flat_hash_map

Open BijanVan opened this issue 6 years ago • 4 comments

Test app aborts when std::string_view is used as the key for absl::flat_hash_map. The message is:

... abseil-cpp/absl/container/internal/raw_hash_set.h:1730: void absl::container_internal::raw_hash_set<absl::container_internal::FlatHashMapPolicy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, int>, absl::container_internal::StringHash, absl::container_internal::StringHashEq::Eq, std::__1::allocator<std::__1::pair<const std::__1::basic_string_view<char, std::__1::char_traits<char> >, int> > >::emplace_at(size_t, Args &&...) [Policy = absl::container_internal::FlatHashMapPolicy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, int>, Hash = absl::container_internal::StringHash, Eq = absl::container_internal::StringHashEq::Eq, Alloc = std::__1::allocator<std::__1::pair<const std::__1::basic_string_view<char, std::__1::char_traits<char> >, int> >, Args = <const std::__1::piecewise_construct_t &, std::__1::tuple<const char *&&>, std::__1::tuple<>>]: Assertion PolicyTraits::apply(FindElement{*this}, *iterator_at(i)) == iterator_at(i) && "constructed value does not match the lookup key"' failed. .... due to a fatal error condition: SIGABRT - Abort (abnormal termination) signal

Sample code: fails when absl::flat_hash_map<std::string_view, int> is used: https://github.com/BijanVan/ds/blob/master/cnc/test/umap_bench.cc#L52

succeeds when std::unordered_map<std::string_view, int> or tsl::robin_map<std::string_view, int> map_ is used: https://github.com/BijanVan/ds/blob/master/cnc/test/umap_bench.cc#L51

BijanVan avatar Apr 30 '20 21:04 BijanVan

I didn't try your code, but can your random string have a \0 in it? If so, the a string_view constructed from a std::string won't match a string_view constructed from std::string::c_str().

derekmauro avatar Apr 30 '20 22:04 derekmauro

it crashes without that c_str() too.

BijanVan avatar Apr 30 '20 22:04 BijanVan

It looks like you may have a few issues with dangling references. For example, in the MapBenchmark constructor, keys_ may reallocate and move your strings in memory. auto value = map_[key] may also be a dangling reference when you delete the key.

Either way, I doubt there is any issue with absl::flat_hash_map.

derekmauro avatar May 01 '20 02:05 derekmauro

That was correct, resizing the vector was the root of the issue. I changed the constructor to MapBenchmark() : rand_{}, keys_{INIT_SIZE}, map_{INIT_SIZE} {

and that resolved the issue. Still, it is interesting that std::unordered_map or tsl::robin_map did not crash but absl::flat_hash_map did which is a good thing to fail fast.

BijanVan avatar May 01 '20 05:05 BijanVan