-
Notifications
You must be signed in to change notification settings - Fork 827
Avoid SmallSetBase heap allocation in default constructor #4884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| #include <algorithm> | ||
| #include <array> | ||
| #include <cassert> | ||
| #include <optional> | ||
| #include <set> | ||
| #include <unordered_set> | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -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 | ||
| // 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(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test failure was caused by this condition being faulty. |
||
| } | ||
|
|
||
| public: | ||
|
|
@@ -154,7 +161,7 @@ class SmallSetBase { | |
|
|
||
| SmallSetBase() {} | ||
| SmallSetBase(std::initializer_list<T> init) { | ||
| for (T item : init) { | ||
| for (const T& item : init) { | ||
| insert(item); | ||
| } | ||
| } | ||
|
|
@@ -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()); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.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); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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
sizeoftells 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::optionalhere 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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.