Commit 6b17defc authored by Nathan Bronson's avatar Nathan Bronson Committed by facebook-github-bot-9

fix AtomicUnorderedInsertMap load factor computation

Summary: AtomicUnorderedInsertMap's constructor takes a maxLoadFactor argument,
which is given its default argument of 0.8f in all of our code.
The clipping function that was supposed to prevent load factors
greater than one was inverted, resulting in all of our code using a
maxLoadFactor of 1 instead of 0.8.  This diff fixes the computation,
as well as changing all of the use sites so that the actual memory
allocated by existing clients does not change.

Reviewed By: yfeldblum

Differential Revision: D2575238

fb-gh-sync-id: bb9dd8de53114236b259631d175d62af098391cf
parent bc857f03
...@@ -210,7 +210,7 @@ struct AtomicUnorderedInsertMap { ...@@ -210,7 +210,7 @@ struct AtomicUnorderedInsertMap {
const Allocator& alloc = Allocator()) const Allocator& alloc = Allocator())
: allocator_(alloc) : allocator_(alloc)
{ {
size_t capacity = maxSize / std::max(1.0f, maxLoadFactor) + 128; size_t capacity = maxSize / std::min(1.0f, maxLoadFactor) + 128;
size_t avail = size_t{1} << (8 * sizeof(IndexType) - 2); size_t avail = size_t{1} << (8 * sizeof(IndexType) - 2);
if (capacity > avail && maxSize < avail) { if (capacity > avail && maxSize < avail) {
// we'll do our best // we'll do our best
......
...@@ -134,6 +134,26 @@ TYPED_TEST(AtomicUnorderedInsertMapTest, basic) { ...@@ -134,6 +134,26 @@ TYPED_TEST(AtomicUnorderedInsertMapTest, basic) {
EXPECT_TRUE(a != b); EXPECT_TRUE(a != b);
} }
TEST(AtomicUnorderedInsertMap, load_factor) {
AtomicUnorderedInsertMap<int, bool> m(5000, 0.5f);
// we should be able to put in much more than 5000 things because of
// our load factor request
for (int i = 0; i < 10000; ++i) {
m.emplace(i, true);
}
}
TEST(AtomicUnorderedInsertMap, capacity_exceeded) {
AtomicUnorderedInsertMap<int, bool> m(5000, 1.0f);
EXPECT_THROW({
for (int i = 0; i < 6000; ++i) {
m.emplace(i, false);
}
}, std::bad_alloc);
}
TYPED_TEST(AtomicUnorderedInsertMapTest, value_mutation) { TYPED_TEST(AtomicUnorderedInsertMapTest, value_mutation) {
UIM<int, MutableAtom<int>, TypeParam> m(100); UIM<int, MutableAtom<int>, TypeParam> m(100);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment