Skip to content

Commit ba27193

Browse files
kalman5meta-codesync[bot]
authored andcommitted
F14: copies can miss keys that overflowed from empty chunks
Summary: **Benefit.** Copying an F14 map or set could produce a copy in which `find` and `contains` report a key as missing although the copy holds it (`size()` and iteration still see it). This fixes it. **Why.** For tables that copy item by item (`F14ValueMap`/`F14ValueSet` of types that are not trivially copyable, and every `F14NodeMap`/`F14NodeSet`), `directBuildFrom` walks the chunks from the top down and stops once every item is copied, i.e. at the lowest occupied chunk. Chunks below it are never visited, so their outbound overflow counts stay zero in the copy. When such a chunk is empty but an item that overflowed out of it lives in a higher chunk, `find` in the copy stops at the empty chunk and misses the item. The loop now also copies the overflow counts of the chunks below. Trivially copyable value tables (the `memcpy` path) and `F14Vector*` were not affected. **Risk.** Only the copy path changes: one header update for each chunk below the lowest occupied one, usually none or a few. Reviewed By: yfeldblum Differential Revision: D134571954 fbshipit-source-id: 530da44d5c2a8c2be80e64b4915fb3692b505874
1 parent 1705be5 commit ba27193

3 files changed

Lines changed: 144 additions & 5 deletions

File tree

‎folly/container/detail/F14Table.h‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,13 +2250,15 @@ class F14Table : public Policy {
22502250
}
22512251
} else {
22522252
// Happy path, no rehash but pack items toward bottom of chunk
2253-
// and use copy constructor. Don't try to optimize by using
2254-
// lastOccupiedChunk() because there may be higher unoccupied chunks
2255-
// with the overflow bit set.
2253+
// and use copy constructor. Visit every chunk, not just the occupied
2254+
// ones: unoccupied chunks above and below them may still count items
2255+
// that overflowed out of them, and find() stops at a chunk whose count
2256+
// is zero.
22562257
auto* srcChunk =
22572258
Chunk::chunkRawAt(src.chunksToAddress(), chunkCount() - 1);
22582259
Chunk* dstChunk = Chunk::chunkRawAt(chunksToAddress(), chunkCount() - 1);
2259-
do {
2260+
for (std::size_t chunksLeft = chunkCount(); chunksLeft > 0;
2261+
--chunksLeft) {
22602262
dstChunk->copyOverflowInfoFrom(*srcChunk);
22612263

22622264
auto iter = srcChunk->occupiedIter();
@@ -2280,7 +2282,8 @@ class F14Table : public Policy {
22802282

22812283
srcChunk = Chunk::prevChunkRaw(srcChunk);
22822284
dstChunk = Chunk::prevChunkRaw(dstChunk);
2283-
} while (size() != src.size());
2285+
}
2286+
FOLLY_SAFE_DCHECK(size() == src.size(), "");
22842287

22852288
// reset doesn't care about packedBegin, so we don't fix it until the end
22862289
if constexpr (kEnableItemIteration) {

‎folly/container/test/F14MapTest.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,74 @@ TEST(F14FastMap, rehash) {
10761076
runRehash<F14VectorMap<std::string, std::string>>();
10771077
}
10781078

1079+
namespace {
1080+
struct IdentityAvalanchingHasher {
1081+
// this is a lie for testing purposes: it makes the low bits of the key pick
1082+
// the chunk directly
1083+
using folly_is_avalanching = std::true_type;
1084+
std::size_t operator()(uint64_t key) const { return key; }
1085+
};
1086+
1087+
template <typename M>
1088+
void runCopyFindsKeyOverflowedFromEmptyChunk() {
1089+
// Four chunks for every policy: with an identity hash, multiples of 4 share
1090+
// chunk 0, so once it is full (12 or 14 slots) the rest overflow out of it.
1091+
M map;
1092+
map.reserve(32);
1093+
for (uint64_t key = 0; key < 60; key += 4) {
1094+
map[key];
1095+
}
1096+
// Chunk 0 is now empty but still counts the overflowed key 56.
1097+
for (uint64_t key = 0; key < 56; key += 4) {
1098+
map.erase(key);
1099+
}
1100+
for (uint64_t key = 1; key < 32; ++key) {
1101+
if (key % 4 != 0) {
1102+
map[key];
1103+
}
1104+
}
1105+
#if FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
1106+
auto stats = F14TableStats::compute(map);
1107+
ASSERT_EQ(stats.chunkCount, std::size_t{4});
1108+
if (!FOLLY_F14_PERTURB_INSERTION_ORDER) {
1109+
// The layout the test relies on: one chunk is empty while it still counts
1110+
// the overflowed key. Debug builds randomise placement, so it is only
1111+
// guaranteed without that.
1112+
ASSERT_GE(stats.chunkOccupancyHisto.at(0), 1);
1113+
ASSERT_GE(stats.chunkOutboundOverflowHisto.size(), std::size_t{2});
1114+
}
1115+
#endif
1116+
1117+
M copy{map};
1118+
EXPECT_EQ(copy.size(), map.size());
1119+
#if FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
1120+
auto copyStats = F14TableStats::compute(copy);
1121+
if (copyStats.chunkCount == stats.chunkCount) {
1122+
EXPECT_EQ(
1123+
copyStats.chunkOutboundOverflowHisto, stats.chunkOutboundOverflowHisto);
1124+
}
1125+
#endif
1126+
for (auto const& entry : map) {
1127+
EXPECT_TRUE(copy.contains(entry.first)) << "key " << entry.first;
1128+
}
1129+
}
1130+
} // namespace
1131+
1132+
TEST(F14ValueMap, copyFindsKeyOverflowedFromEmptyChunk) {
1133+
runCopyFindsKeyOverflowedFromEmptyChunk<
1134+
F14ValueMap<uint64_t, std::string, IdentityAvalanchingHasher>>();
1135+
}
1136+
1137+
TEST(F14NodeMap, copyFindsKeyOverflowedFromEmptyChunk) {
1138+
runCopyFindsKeyOverflowedFromEmptyChunk<
1139+
F14NodeMap<uint64_t, std::string, IdentityAvalanchingHasher>>();
1140+
}
1141+
1142+
TEST(F14VectorMap, copyFindsKeyOverflowedFromEmptyChunk) {
1143+
runCopyFindsKeyOverflowedFromEmptyChunk<
1144+
F14VectorMap<uint64_t, std::string, IdentityAvalanchingHasher>>();
1145+
}
1146+
10791147
template <typename T>
10801148
void runPrehash() {
10811149
T h;

‎folly/container/test/F14SetTest.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,74 @@ TEST(F14VectorSet, rehash) {
10251025
runRehash<F14VectorSet<std::string>>();
10261026
}
10271027

1028+
namespace {
1029+
struct IdentityAvalanchingHasher {
1030+
// this is a lie for testing purposes: it makes the low bits of the key pick
1031+
// the chunk directly
1032+
using folly_is_avalanching = std::true_type;
1033+
std::size_t operator()(uint64_t key) const { return key; }
1034+
};
1035+
1036+
template <typename S>
1037+
void runCopyFindsKeyOverflowedFromEmptyChunk() {
1038+
// Four chunks for every policy: with an identity hash, multiples of 4 share
1039+
// chunk 0, so once it is full (12 or 14 slots) the rest overflow out of it.
1040+
S set;
1041+
set.reserve(32);
1042+
for (uint64_t key = 0; key < 60; key += 4) {
1043+
set.insert(key);
1044+
}
1045+
// Chunk 0 is now empty but still counts the overflowed key 56.
1046+
for (uint64_t key = 0; key < 56; key += 4) {
1047+
set.erase(key);
1048+
}
1049+
for (uint64_t key = 1; key < 32; ++key) {
1050+
if (key % 4 != 0) {
1051+
set.insert(key);
1052+
}
1053+
}
1054+
#if FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
1055+
auto stats = F14TableStats::compute(set);
1056+
ASSERT_EQ(stats.chunkCount, std::size_t{4});
1057+
if (!FOLLY_F14_PERTURB_INSERTION_ORDER) {
1058+
// The layout the test relies on: one chunk is empty while it still counts
1059+
// the overflowed key. Debug builds randomise placement, so it is only
1060+
// guaranteed without that.
1061+
ASSERT_GE(stats.chunkOccupancyHisto.at(0), 1);
1062+
ASSERT_GE(stats.chunkOutboundOverflowHisto.size(), std::size_t{2});
1063+
}
1064+
#endif
1065+
1066+
S copy{set};
1067+
EXPECT_EQ(copy.size(), set.size());
1068+
#if FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
1069+
auto copyStats = F14TableStats::compute(copy);
1070+
if (copyStats.chunkCount == stats.chunkCount) {
1071+
EXPECT_EQ(
1072+
copyStats.chunkOutboundOverflowHisto, stats.chunkOutboundOverflowHisto);
1073+
}
1074+
#endif
1075+
for (auto key : set) {
1076+
EXPECT_TRUE(copy.contains(key)) << "key " << key;
1077+
}
1078+
}
1079+
} // namespace
1080+
1081+
TEST(F14ValueSet, copyFindsKeyOverflowedFromEmptyChunk) {
1082+
runCopyFindsKeyOverflowedFromEmptyChunk<
1083+
F14ValueSet<uint64_t, IdentityAvalanchingHasher>>();
1084+
}
1085+
1086+
TEST(F14NodeSet, copyFindsKeyOverflowedFromEmptyChunk) {
1087+
runCopyFindsKeyOverflowedFromEmptyChunk<
1088+
F14NodeSet<uint64_t, IdentityAvalanchingHasher>>();
1089+
}
1090+
1091+
TEST(F14VectorSet, copyFindsKeyOverflowedFromEmptyChunk) {
1092+
runCopyFindsKeyOverflowedFromEmptyChunk<
1093+
F14VectorSet<uint64_t, IdentityAvalanchingHasher>>();
1094+
}
1095+
10281096
TEST(F14ValueSet, random) {
10291097
runRandom<F14ValueSet<uint64_t>>();
10301098
}

0 commit comments

Comments
 (0)