Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions src/support/small_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <optional>
#include <set>
#include <unordered_set>

Expand Down Expand Up @@ -67,16 +68,17 @@ struct UnorderedFixedStorage : public FixedStorageBase<T, N> {
return InsertResult::NoError;
}

void erase(const T& x) {
size_t erase(const T& x) {
for (size_t i = 0; i < this->used; i++) {
if (this->storage[i] == x) {
// We found the item; erase it by moving the final item to replace it
// and truncating the size.
this->used--;
this->storage[i] = this->storage[this->used];
return;
return 1;
}
}
return 0;
}
};

Expand Down Expand Up @@ -113,17 +115,18 @@ struct OrderedFixedStorage : public FixedStorageBase<T, N> {
return InsertResult::NoError;
}

void erase(const T& x) {
size_t erase(const T& x) {
for (size_t i = 0; i < this->used; i++) {
if (this->storage[i] == x) {
// We found the item; move things backwards and shrink.
for (size_t j = i + 1; j < this->used; j++) {
this->storage[j - 1] = this->storage[j];
}
this->used--;
return;
return 1;
}
}
return 0;
}
};

Expand All @@ -132,16 +135,20 @@ class SmallSetBase {
// fixed-space storage
FixedStorage fixed;

// flexible additional storage
FlexibleSet flexible;
// Flexible additional storage. We use std::optional here to reduce the
// overhead in the common case, where the flexible storage is never needed.
// With std::optional we just have a pointer taking up space, as opposed to a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rereading this now, this doesn't seem true at least on linux, as sizeof tells me that the full storage is used. That is, the gnu C++ STL seems to implement an optional as enough space for the item, and a tag whether it is present or not.

Given that, I think we might have been wrong in our previous discussion here. That is, using std::optional here increases memory size, so it may have downsides, and we should probably be more careful.

If our goal is to avoid a problem with the MSVC STL, then a workaround specific to there might make more sense actually.

What I'd suggest immediately though is to perhaps go look at what LLVM's SmallSet does, as a lot of thought has gone into that. We should probably do what they are doing.

Copy link
Author

@Squareys Squareys Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, LLVM's SmallSet doesn't seem to address the heap allocation on MSVC.

I believe the measurements you had made were on Linux? It seemed benefits from using avoiding the std::set construction outweighed the size increase there, too? (At least when used in EffectsAnalyzer, and way less significant than on windows... I suppose it's not making heap allocations in the STL implementation there).

Effectively, we could, of course, avoid the entire PR, if we'd use another STL, as you had suggested in this comment.

// full std::set which is larger. Also, on MSVC's STL at least the std::set
// constructor does some work, which we want to avoid.
std::optional<FlexibleSet> flexible;

bool usingFixed() const {
// If the flexible storage contains something, then we are using it.
// If the flexible optional has a value, we are using it.
// Otherwise we use the fixed storage. Note that if we grow and shrink then
// we will stay in flexible mode until we reach a size of zero, at which
// point we return to fixed mode. This is intentional, to avoid a lot of
// movement in switching between fixed and flexible mode.
return flexible.empty();
return !flexible || flexible->empty();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test failure was caused by this condition being faulty.

}

public:
Expand All @@ -154,7 +161,7 @@ class SmallSetBase {

SmallSetBase() {}
SmallSetBase(std::initializer_list<T> init) {
for (T item : init) {
for (const T& item : init) {
insert(item);
}
}
Expand All @@ -165,23 +172,23 @@ class SmallSetBase {
// We need to add an item but no fixed storage remains to grow. Switch
// to flexible.
assert(fixed.used == N);
assert(flexible.empty());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing this assert, because it would have been updated to !flexible || flexible->empty(), i.e. equivalent to usingFixed(), which is tested in the condition above. Happy to put it back in, if you believe it's still useful.

flexible.insert(fixed.storage.begin(),
fixed.storage.begin() + fixed.used);
flexible.insert(x);
flexible = FlexibleSet{};
flexible->insert(fixed.storage.begin(),
fixed.storage.begin() + fixed.used);
flexible->insert(x);
assert(!usingFixed());
fixed.used = 0;
}
} else {
flexible.insert(x);
flexible->insert(x);
}
}

void erase(const T& x) {
size_t erase(const T& x) {
if (usingFixed()) {
fixed.erase(x);
return fixed.erase(x);
} else {
flexible.erase(x);
return flexible->erase(x);
}
}

Expand All @@ -195,23 +202,25 @@ class SmallSetBase {
}
return 0;
} else {
return flexible.count(x);
return flexible->count(x);
}
}

size_t size() const {
if (usingFixed()) {
return fixed.used;
} else {
return flexible.size();
return flexible->size();
}
}

bool empty() const { return size() == 0; }

void clear() {
fixed.used = 0;
flexible.clear();
if (flexible) {
flexible->clear();
}
}

bool
Expand All @@ -228,7 +237,7 @@ class SmallSetBase {
other.fixed.storage.begin() + other.fixed.used,
[this](const T& x) { return count(x); });
} else {
return flexible == other.flexible;
return flexible.value() == other.flexible.value();
}
}

Expand Down Expand Up @@ -264,15 +273,15 @@ class SmallSetBase {
if (usingFixed) {
fixedIndex = 0;
} else {
flexibleIterator = parent->flexible.begin();
flexibleIterator = parent->flexible->begin();
}
}

void setEnd() {
if (usingFixed) {
fixedIndex = parent->size();
} else {
flexibleIterator = parent->flexible.end();
flexibleIterator = parent->flexible->end();
}
}

Expand Down